123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
-
- using System;
- using System.Diagnostics;
- using System.Collections.Generic;
- namespace Box2D.XNA
- {
-
-
-
-
-
- public class ContactEdge
- {
- public Body Other;
-
-
- for (int i = 0; i < _manifold._pointCount; ++i)
- {
- ManifoldPoint mp2 = _manifold._points[i];
- mp2.NormalImpulse = 0.0f;
- mp2.TangentImpulse = 0.0f;
- ContactID id2 = mp2.Id;
- bool found = false;
- for (int j = 0; j < oldManifold._pointCount; ++j)
- {
- ManifoldPoint mp1 = oldManifold._points[j];
- if (mp1.Id.Key == id2.Key)
- {
- mp2.NormalImpulse = mp1.NormalImpulse;
- mp2.TangentImpulse = mp1.TangentImpulse;
- found = true;
- break;
- }
- }
- if (found == false)
- {
- mp2.NormalImpulse = 0.0f;
- mp2.TangentImpulse = 0.0f;
- }
- _manifold._points[i] = mp2;
- }
- if (touching != wasTouching)
- {
- bodyA.SetAwake(true);
- bodyB.SetAwake(true);
- }
- }
- if (touching)
- {
- _flags |= ContactFlags.Touching;
- }
- else
- {
- _flags &= ~ContactFlags.Touching;
- }
- if (wasTouching == false && touching == true && null != listener)
- {
- listener.BeginContact(this);
- }
- if (wasTouching == true && touching == false && null != listener)
- {
- listener.EndContact(this);
- }
- if (sensor == false && null != listener)
- {
- listener.PreSolve(this, ref oldManifold);
- }
- }
- private static EdgeShape s_edge = new EdgeShape();
-
- internal void Evaluate(ref Manifold manifold, ref Transform xfA, ref Transform xfB)
- {
- switch (_type)
- {
- case ContactType.Polygon:
- Collision.CollidePolygons(ref manifold,
- (PolygonShape)_fixtureA.GetShape(), ref xfA,
- (PolygonShape)_fixtureB.GetShape(), ref xfB);
- break;
- case ContactType.PolygonAndCircle:
- Collision.CollidePolygonAndCircle(ref manifold,
- (PolygonShape)_fixtureA.GetShape(), ref xfA,
- (CircleShape) _fixtureB.GetShape(), ref xfB);
- break;
- case ContactType.EdgeAndCircle:
- Collision.CollideEdgeAndCircle(ref manifold,
- (EdgeShape) _fixtureA.GetShape(), ref xfA,
- (CircleShape)_fixtureB.GetShape(), ref xfB);
- break;
- case ContactType.EdgeAndPolygon:
- Collision.CollideEdgeAndPolygon(ref manifold,
- (EdgeShape) _fixtureA.GetShape(), ref xfA,
- (PolygonShape)_fixtureB.GetShape(), ref xfB);
- break;
- case ContactType.LoopAndCircle:
- var loop = (LoopShape)_fixtureA.GetShape();
- loop.GetChildEdge(ref s_edge, _indexA);
- Collision.CollideEdgeAndCircle(ref manifold, s_edge, ref xfA,
- (CircleShape)_fixtureB.GetShape(), ref xfB);
- break;
- case ContactType.LoopAndPolygon:
- var loop2 = (LoopShape)_fixtureA.GetShape();
- loop2.GetChildEdge(ref s_edge, _indexA);
- Collision.CollideEdgeAndPolygon(ref manifold, s_edge, ref xfA,
- (PolygonShape)_fixtureB.GetShape(), ref xfB);
- break;
- case ContactType.Circle:
- Collision.CollideCircles(ref manifold,
- (CircleShape)_fixtureA.GetShape(), ref xfA,
- (CircleShape)_fixtureB.GetShape(), ref xfB);
- break;
- }
- }
- internal enum ContactType
- {
- Polygon,
- PolygonAndCircle,
- Circle,
- EdgeAndPolygon,
- EdgeAndCircle,
- LoopAndPolygon,
- LoopAndCircle,
- }
-
- internal static ContactType[,] s_registers = new ContactType[,]
- {
- {
- ContactType.Circle,
- ContactType.EdgeAndCircle,
- ContactType.PolygonAndCircle,
- ContactType.LoopAndCircle,
- },
- {
- ContactType.EdgeAndCircle,
- ContactType.EdgeAndCircle,
- ContactType.EdgeAndPolygon,
- ContactType.EdgeAndPolygon,
- },
- {
- ContactType.PolygonAndCircle,
- ContactType.EdgeAndPolygon,
- ContactType.Polygon,
- ContactType.LoopAndPolygon,
- },
- {
- ContactType.LoopAndCircle,
- ContactType.LoopAndCircle,
- ContactType.LoopAndPolygon,
- ContactType.LoopAndPolygon,
- },
- };
- internal static Contact Create(Fixture fixtureA, int indexA, Fixture fixtureB, int indexB)
- {
- ShapeType type1 = fixtureA.ShapeType;
- ShapeType type2 = fixtureB.ShapeType;
- Debug.Assert(ShapeType.Unknown < type1 && type1 < ShapeType.TypeCount);
- Debug.Assert(ShapeType.Unknown < type2 && type2 < ShapeType.TypeCount);
- Contact c;
- var pool = fixtureA._body._world._contactPool;
- if (pool.Count > 0)
- {
- c = pool.Dequeue();
- if ((type1 >= type2 || (type1 == ShapeType.Edge && type2 == ShapeType.Polygon))
- &&
- !(type2 == ShapeType.Edge && type1 == ShapeType.Polygon))
- {
- c.Reset(fixtureA, indexA, fixtureB, indexB);
- }
- else
- {
- c.Reset(fixtureB, indexB, fixtureA, indexA);
- }
- }
- else
- {
-
- if ((type1 >= type2 || (type1 == ShapeType.Edge && type2 == ShapeType.Polygon))
- &&
- !(type2 == ShapeType.Edge && type1 == ShapeType.Polygon))
- {
- c = new Contact(fixtureA, indexA, fixtureB, indexB);
- }
- else
- {
- c = new Contact(fixtureB, indexB, fixtureA, indexA);
- }
- }
- c._type = Contact.s_registers[(int)type1, (int)type2];
- return c;
- }
- internal void Destroy()
- {
- _fixtureA._body._world._contactPool.Enqueue(this);
- Reset(null, 0, null, 0);
- }
- private ContactType _type;
- internal ContactFlags _flags;
-
- internal Contact _prev;
- internal Contact _next;
-
- internal ContactEdge _nodeA = new ContactEdge();
- internal ContactEdge _nodeB = new ContactEdge();
- internal Fixture _fixtureA;
- internal Fixture _fixtureB;
- internal int _indexA;
- internal int _indexB;
- internal Manifold _manifold;
- internal int _toiCount;
- };
- }
|