/// <summary> /// Sets up a contact event and then invokes it. /// </summary> private void SetupAndFireEvent(CollisionObject objectA, CollisionObject objectB, Vector3? position, Vector3? normal, ObjectTouchingFlags flags) { PonykartCollisionGroups groupA = objectA.GetCollisionGroup(); PonykartCollisionGroups groupB = objectB.GetCollisionGroup(); //int groupIDA = (int) groupA; //int groupIDB = (int) groupB; CollisionReportInfo info = new CollisionReportInfo { FirstGroup = groupA, SecondGroup = groupB, FirstObject = objectA, SecondObject = objectB, Position = position, Normal = normal, Flags = flags }; FireEvent(info); }
public static PonykartCollisionGroups GetCollisionGroup(CollisionObject obj) { return obj.GetCollisionGroup(); }
/// <summary> /// Fired every frame when an object is inside another object. /// </summary> bool ContactAdded(ManifoldPoint point, CollisionObject objectA, int partId0, int index0, CollisionObject objectB, int partId1, int index1) { // if one of the two objects is deactivated, we don't care if (!objectA.IsActive && !objectB.IsActive) return false; int objectACollisionGroup = (int) objectA.GetCollisionGroup(); int objectBCollisionGroup = (int) objectB.GetCollisionGroup(); // do we have any events that care about these groups? if not, then skip this collision pair if (reporters[objectACollisionGroup, objectBCollisionGroup] == null && reporters[objectBCollisionGroup, objectACollisionGroup] == null) return false; // when the actual bodies are touching and not just their AABB's if (point.Distance <= 0.05) { // get the lists HashSet<CollisionObject> objectAList = GetCollisionListForObject(objectA, CurrentlyCollidingWith), newObjectAList = GetCollisionListForObject(objectA, NewCollidingWith), objectBList = GetCollisionListForObject(objectB, CurrentlyCollidingWith), newObjectBList = GetCollisionListForObject(objectB, NewCollidingWith); // see if the other object is in there if (!objectAList.Contains(objectB) || !objectBList.Contains(objectA)) { /* * if it isn't, add it! this means we have a new collision and need to fire off an event! * okay now we need to get the point where it contacted! * Limitation with this system: if we're already colliding with B and then collide with it in a different place without * leaving the original place, we won't get another event. Why? Well because what if something's sliding along? * Don't need loads of events for that */ // make sure we add it to our collections! The hashset means we don't have to worry about duplicates objectAList.Add(objectB); objectBList.Add(objectA); newObjectAList.Add(objectB); newObjectBList.Add(objectA); // update the dictionaries (is this necessary?) CurrentlyCollidingWith[objectA] = objectAList; CurrentlyCollidingWith[objectB] = objectBList; NewCollidingWith[objectA] = newObjectAList; NewCollidingWith[objectB] = newObjectBList; Vector3 pos = point.PositionWorldOnA.MidPoint(point.PositionWorldOnB); Vector3 normal = point.NormalWorldOnB; // woop woop they started touching, so we fire off an event! SetupAndFireEvent(objectA, objectB, pos, normal, ObjectTouchingFlags.StartedTouching); } else { // already in the dictionary, no new collisions. Add it to the new dictionary anyway though, because if we don't then it thinks // they stopped colliding. Which we don't want! newObjectAList.Add(objectB); newObjectBList.Add(objectA); NewCollidingWith[objectA] = newObjectAList; NewCollidingWith[objectB] = newObjectBList; } } // This means they're still inside each other's AABB's, but they aren't actually touching //else { //} return false; }