Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //The code in the Update method of the level checking for collisions between enemies:
- foreach (Enemy e in Enemies)
- {
- foreach (Enemy e2 in Enemies)
- {
- if (e != e2)
- //The enemy's Test is its collider object containing the Shape used for SAT.
- if (e.Test.isColliding(e2.Test, ref MTV))
- e.Position += MTV;
- }
- }
- //The isColliding method inside the Collider class (this is a big one):
- public bool isColliding(Collider<T> target, ref Vector2 minTransVect)
- {
- //Get the normalized axes of each shape for projection.
- Vector2[] axes1 = BoundingShape.getAxes();
- Vector2[] axes2 = target.BoundingShape.getAxes();
- Vector2 smallAxis = Vector2.Zero;
- double overlap = 0;
- //Loop through this collider's shape's axes and check for overlap.
- for (int a = 0; a < axes1.Length; a++)
- {
- if (!Collision.overlap(BoundingShape.project(axes1[a]),
- target.BoundingShape.project(axes1[a])))
- {
- return false;
- }
- else
- {
- double o = Collision.getOverlap(BoundingShape.project(axes1[a]), target.BoundingShape.project(axes1[a]));
- if (overlap < o)
- {
- overlap = o;
- smallAxis = axes1[a];
- }
- }
- }
- //Loop through the target collider's shape's axes and check for overlap.
- for (int a = 0; a < axes2.Length; a++)
- {
- if (!Collision.overlap(BoundingShape.project(axes2[a]),
- target.BoundingShape.project(axes2[a])))
- {
- return false;
- }
- else
- {
- double o = Collision.getOverlap(BoundingShape.project(axes2[a]), target.BoundingShape.project(axes2[a]));
- if (overlap < o)
- {
- overlap = o;
- smallAxis = axes2[a];
- }
- }
- }
- Vector2 mtv = new Vector2(smallAxis.X, smallAxis.Y);
- mtv = Vector2.Multiply(mtv, (float)overlap);
- Vector2 dir = Vector2.Subtract(Position, target.Position);
- float test = Vector2.Dot(mtv, dir);
- if (test > 0)
- minTransVect = Vector2.Multiply(mtv, -1);
- else
- minTransVect = mtv;
- return true;
- }
- //The Collision class is a static class that handles the overlap and getOverlap methods:
- //For projections in Vector2 form, x is the min and y is the max.
- public static bool overlap(Vector2 proj1, Vector2 proj2)
- {
- float test1 = proj1.X - proj2.Y;
- float test2 = proj2.X - proj1.Y;
- if (test1 > 0 || test2 > 0)
- return false;
- return true;
- }
- //For projections in Vector2 form, x is the min and y is the max.
- public static double getOverlap(Vector2 proj1, Vector2 proj2)
- {
- float start = proj1.X > proj2.X ? proj1.X : proj2.X;
- float end = proj1.Y < proj2.Y ? proj1.Y : proj2.Y;
- return (end - start);
- }
- //Finally, here are relevant methods in the Shape class (which is typically parented by a Collider)
- //Gets the position in world space of a transformed vertex at index in the shape's list of vertices.
- public Vector2 getVertPos(int index)
- {
- if (TransformedVertices[index] != null)
- return Vector2.Add(TransformedVertices[index], Center);
- return Vector2.Zero;
- }
- //Return an array of the shape's normalized perpendicular axis.
- public Vector2[] getAxes()
- {
- Vector2[] axes = new Vector2[TransformedVertices.Count];
- for (int a = 0; a < axes.Length; a++)
- {
- Vector2 v1 = getVertPos(a);
- Vector2 v2 = getVertPos(a + 1 == axes.Length ? 0 : a + 1);
- Vector2 edge = Vector2.Subtract(v1, v2);
- Vector2 normal = new Vector2(-edge.Y, edge.X);
- normal.Normalize();
- axes[a] = normal;
- }
- return axes;
- }
- //Project the shape into 1-dimensional space along a specified axis.
- public Vector2 project(Vector2 axis)
- {
- float min = Vector2.Dot(getVertPos(0), axis);
- float max = min;
- for (int i = 0; i < TransformedVertices.Count; i++)
- {
- float c = Vector2.Dot(getVertPos(i), axis);
- if (c < min)
- min = c;
- else if (c > max)
- max = c;
- }
- return new Vector2(min, max);
- }
- public void Update()
- {
- if (!Circle && Rotation != lastRotation)
- {
- lastRotation = Rotation;
- Matrix rotate = Matrix.CreateRotationZ(Rotation);
- for (int v = 0; v < Vertices.Count; v++)
- {
- TransformedVertices[v] = Vector2.Transform(Vertices[v], rotate);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment