protected void OnEvent(CCEventCustom eventC) { CCPhysicsContact contact = (CCPhysicsContact)eventC.UserData; // PhysicsContact) contact = dynamic_cast<PhysicsContact*>(event); if (contact == null) { return; } switch (contact.GetEventCode()) { case EventCode.BEGIN: { bool ret = true; if (onContactBegin != null && HitTest(contact.GetShapeA(), contact.GetShapeB())) { contact.GenerateContactData(); ret = onContactBegin(contact); } contact.SetResult(ret); break; } case EventCode.PRESOLVE: { bool ret = true; if (onContactPreSolve != null && HitTest(contact.GetShapeA(), contact.GetShapeB())) { CCPhysicsContactPreSolve solve = new CCPhysicsContactPreSolve(contact._contactInfo); contact.GenerateContactData(); ret = onContactPreSolve(contact, solve); } contact.SetResult(ret); break; } case EventCode.POSTSOLVE: { if (onContactPostSolve != null && HitTest(contact.GetShapeA(), contact.GetShapeB())) { CCPhysicsContactPostSolve solve = new CCPhysicsContactPostSolve(contact._contactInfo); onContactPostSolve(contact, solve); } break; } case EventCode.SEPERATE: { if (onContactSeperate != null && HitTest(contact.GetShapeA(), contact.GetShapeB())) { onContactSeperate(contact); } break; } default: break; } }
//public void Draw() //{ // if (DebugDrawMask != cpDrawFlags.None) // { // _debugDraw.Begin(); // _info.getSpace().DrawDebugData(); // _debugDraw.End(); // } //} //public virtual void DebugDraw() //{ // if (_debugDraw == null) // { // _debugDraw = new PhysicsDebugDraw(this); // } // if (_debugDraw != null && _bodies.Count > 0) // { // if (_debugDraw.Begin()) // { // if (DebugDrawMask == cpDrawFlags.ALL || DebugDrawMask == cpDrawFlags.Shape) // { // foreach (CCPhysicsBody body in _bodies) // { // if (!body.IsEnabled()) // { // continue; // } // foreach (CCPhysicsShape shape in body.GetShapes()) // { // _debugDraw.DrawShape(shape); // } // } // } // if (DebugDrawMask == cpDrawFlags.ALL || DebugDrawMask == cpDrawFlags.Joint) // { // foreach (CCPhysicsJoint joint in _joints) // { // _debugDraw.DrawJoint(joint); // } // } // _debugDraw.End(); // } // } //} public virtual bool CollisionBeginCallback(CCPhysicsContact contact) { bool ret = true; CCPhysicsShape shapeA = contact.GetShapeA(); CCPhysicsShape shapeB = contact.GetShapeB(); CCPhysicsBody bodyA = shapeA.GetBody(); CCPhysicsBody bodyB = shapeB.GetBody(); List<CCPhysicsJoint> jointsA = bodyA.GetJoints(); // check the joint is collision enable or not foreach (CCPhysicsJoint joint in jointsA) { if (!_joints.Exists(j => j == joint)) continue; if (!joint.IsCollisionEnabled()) { CCPhysicsBody body = joint.GetBodyA() == bodyA ? joint.GetBodyB() : joint.GetBodyA(); if (body == bodyB) { contact.SetNotificationEnable(false); return false; } } } // bitmask check if ((shapeA.GetCategoryBitmask() & shapeB.GetContactTestBitmask()) == 0 || (shapeA.GetContactTestBitmask() & shapeB.GetCategoryBitmask()) == 0) { contact.SetNotificationEnable(false); } if (shapeA.GetGroup() != 0 && shapeA.GetGroup() == shapeB.GetGroup()) { ret = shapeA.GetGroup() > 0; } else { if ((shapeA.GetCategoryBitmask() & shapeB.GetCollisionBitmask()) == 0 || (shapeB.GetCategoryBitmask() & shapeA.GetCollisionBitmask()) == 0) { ret = false; } } if (contact.IsNotificationEnabled()) { contact.SetEventCode(EventCode.BEGIN); contact.SetWorld(this); _scene.DispatchEvent(contact); } return ret ? contact.ResetResult() : false; }
//public void Draw() //{ // if (DebugDrawMask != cpDrawFlags.None) // { // _debugDraw.Begin(); // _info.getSpace().DrawDebugData(); // _debugDraw.End(); // } //} //public virtual void DebugDraw() //{ // if (_debugDraw == null) // { // _debugDraw = new PhysicsDebugDraw(this); // } // if (_debugDraw != null && _bodies.Count > 0) // { // if (_debugDraw.Begin()) // { // if (DebugDrawMask == cpDrawFlags.ALL || DebugDrawMask == cpDrawFlags.Shape) // { // foreach (CCPhysicsBody body in _bodies) // { // if (!body.IsEnabled()) // { // continue; // } // foreach (CCPhysicsShape shape in body.GetShapes()) // { // _debugDraw.DrawShape(shape); // } // } // } // if (DebugDrawMask == cpDrawFlags.ALL || DebugDrawMask == cpDrawFlags.Joint) // { // foreach (CCPhysicsJoint joint in _joints) // { // _debugDraw.DrawJoint(joint); // } // } // _debugDraw.End(); // } // } //} public virtual bool CollisionBeginCallback(CCPhysicsContact contact) { bool ret = true; CCPhysicsShape shapeA = contact.GetShapeA(); CCPhysicsShape shapeB = contact.GetShapeB(); CCPhysicsBody bodyA = shapeA.GetBody(); CCPhysicsBody bodyB = shapeB.GetBody(); List <CCPhysicsJoint> jointsA = bodyA.GetJoints(); // check the joint is collision enable or not foreach (CCPhysicsJoint joint in jointsA) { if (!_joints.Exists(j => j == joint)) { continue; } if (!joint.IsCollisionEnabled()) { CCPhysicsBody body = joint.GetBodyA() == bodyA?joint.GetBodyB() : joint.GetBodyA(); if (body == bodyB) { contact.SetNotificationEnable(false); return(false); } } } // bitmask check if ((shapeA.GetCategoryBitmask() & shapeB.GetContactTestBitmask()) == 0 || (shapeA.GetContactTestBitmask() & shapeB.GetCategoryBitmask()) == 0) { contact.SetNotificationEnable(false); } if (shapeA.GetGroup() != 0 && shapeA.GetGroup() == shapeB.GetGroup()) { ret = shapeA.GetGroup() > 0; } else { if ((shapeA.GetCategoryBitmask() & shapeB.GetCollisionBitmask()) == 0 || (shapeB.GetCategoryBitmask() & shapeA.GetCollisionBitmask()) == 0) { ret = false; } } if (contact.IsNotificationEnabled()) { contact.SetEventCode(EventCode.BEGIN); contact.SetWorld(this); _scene.DispatchEvent(contact); } return(ret ? contact.ResetResult() : false); }