Beispiel #1
0
 public override bool Collides(Collider other)
 {
     if(other is SphereCollider)
     {
         SphereCollider collider = other as SphereCollider;
         return (Transform.Position - collider.Transform.Position).LengthSquared() <
             System.Math.Pow(Radius + collider.Radius, 2);
     }
     return base.Collides(other);
 }
Beispiel #2
0
 public override bool Collides(Collider other, out Vector3 normal)
 {
     if (other is SphereCollider)
     {
         SphereCollider collider = other as SphereCollider;
         if ((Transform.Position - collider.Transform.Position).LengthSquared() <
             System.Math.Pow(Radius + collider.Radius, 2))
         {
             normal = Vector3.Normalize(Transform.Position - collider.Transform.Position);
             return true;
         }
     }
     else if (other is BoxCollider)
         return other.Collides(this, out normal);
     return base.Collides(other, out normal);
 }
Beispiel #3
0
 public override bool Collides(Collider other, out Vector3 normal)
 {
     if (other is SphereCollider)
     {
         SphereCollider collider = other as SphereCollider;
         normal = Vector3.Zero; // no collision
         bool isColliding = false;
         // For each face
         for (int i = 0; i < 6; i++)
         {
             // For each triangle in the face
             for (int j = 0; j < 2; j++)
             {
                 // Extract the vertices and normal for this triangle
                 int baseIndex = i * 6 + j * 3;
                 Vector3 a = vertices[indices[baseIndex]] * Size;
                 Vector3 b = vertices[indices[baseIndex + 1]] * Size;
                 Vector3 c = vertices[indices[baseIndex + 2]] * Size;
                 Vector3 n = normals[i];
                 // How far is the sphere center from the plane?
                 float d = Math.Abs(Vector3.Dot(collider.Transform.Position - a, n));
                 // Check for closeness to plane
                 if (d < collider.Radius)
                 {
                     // Close enough, find point in plane
                     Vector3 pointOnPlane = collider.Transform.Position - n * d;
                     // Now, find areas of the three "inner" triangles
                     float area1 = Vector3.Dot(Vector3.Cross(b - a, pointOnPlane - a), n);
                     float area2 = Vector3.Dot(Vector3.Cross(c - b, pointOnPlane - b), n);
                     float area3 = Vector3.Dot(Vector3.Cross(a - c, pointOnPlane - c), n);
                     // No collision if either one is less than zero
                     if (!(area1 < 0 || area2 < 0 || area3 < 0))
                     {
                         normal += n;
                         j = 1; // skip second triangle, if necessary
                         if (i % 2 == 0) i += 1; // skip opposite side if necessary
                         isColliding = true;
                     }
                 }
             }
         }
         normal.Normalize();
         return isColliding;
     }
     return base.Collides(other, out normal);
 }
Beispiel #4
0
 public virtual bool Collides(Collider other)
 {
     return false;
 }
Beispiel #5
0
 public virtual bool Collides(Collider other, out Vector3 normal)
 {
     normal = Vector3.Zero;
     return false;
 }