// Copyright (c) Wojciech Figat. All rights reserved.
using System;
using System.Collections;
using System.Collections.Generic;
namespace FlaxEngine
{
///
/// Contains a collision information passed to the OnCollisionEnter/OnCollisionExit events.
///
unsafe partial struct Collision : IEnumerable
{
private class ContactsEnumerator : IEnumerator
{
private Collision _c;
private int _index;
public ContactsEnumerator(ref Collision c)
{
_c = c;
_index = 0;
}
public bool MoveNext()
{
if (_index == _c.ContactsCount - 1)
return false;
_index++;
return true;
}
public void Reset()
{
_index = 0;
}
ContactPoint IEnumerator.Current
{
get
{
if (_index == _c.ContactsCount)
throw new InvalidOperationException("Enumeration ended.");
fixed (ContactPoint* ptr = &_c.Contacts0)
return ptr[_index];
}
}
public object Current
{
get
{
if (_index == _c.ContactsCount)
throw new InvalidOperationException("Enumeration ended.");
fixed (ContactPoint* ptr = &_c.Contacts0)
return ptr[_index];
}
}
public void Dispose()
{
_c = new Collision();
}
}
///
/// The contacts locations.
///
///
/// This property allocates an array of contact points.
///
public ContactPoint[] Contacts
{
get
{
var result = new ContactPoint[ContactsCount];
fixed (ContactPoint* ptr = &Contacts0)
{
for (int i = 0; i < ContactsCount; i++)
result[i] = ptr[i];
}
return result;
}
}
///
/// The relative linear velocity of the two colliding objects.
///
///
/// Can be used to detect stronger collisions.
///
public Vector3 RelativeVelocity
{
get
{
Vector3.Subtract(ref ThisVelocity, ref OtherVelocity, out var result);
return result;
}
}
///
/// The first collider (this instance). It may be null if this actor is not the (eg. ).
///
public Collider ThisCollider => ThisActor as Collider;
///
/// The second collider (other instance). It may be null if this actor is not the (eg. ).
///
public Collider OtherCollider => OtherActor as Collider;
///
IEnumerator IEnumerable.GetEnumerator()
{
return new ContactsEnumerator(ref this);
}
///
IEnumerator IEnumerable.GetEnumerator()
{
return new ContactsEnumerator(ref this);
}
}
}