agi_shi wrote:This only disregards the contacts in order for higher efficiency, it's not actually required for a trigger.
What you want to do is create a material contact callback that stores objects in a dictionary between object and time since contact. On every contact, check if the object is already stored -if not, then you have an onEnter() event. If it was already stored, then simply subtract the delta time from the time since contact. In some other per-frame method, add the delta time to the time since contact. If the time since contact is now greater than some constant (perhaps twice the physics step size), the object has left the trigger and you have an onExit() event and remove the object from the dictionary.
This is very good idea, I will add few ideas to agi_shi sugestion,
first I will clarify that since triggers are meant to be big volumnes, the option set as trigger play an importnat role when triggers are Big and have many bodies on fligh.
Secund the only reason I an refining your idea is because if does no consider if a body go to rest inside a trigger.
Plus I beleive your idea is correct but can be implemented much simpler without tracking time or contacts.
-Make a Material and call it trigger or whatever.
-asign a dictinonary or a Map to the Trigger body.
-add a Force and Torque callback to prosses the Trigger Bodies.
-in this funtion back tree things can happens
-1 body can enter the trigger,
-2 body can be in teh trigger,
-3 body can be out of eth trigger
basically the callback should looks like this
- Code: Select all
void TriggerForceAndTorqueCallback (const NewtonBody* body)
{
// find OnEnter Event, these are the bodes that enter in the Dictionary for the First Time
for (NewtonJoint* joint = NewtonBodyGetFirstContactJoint (body); joint; joint = NewtonBodyGetNextContactJoint (body, joint)) {
const NewtonBody* otherBody;
// get the othe Body from the joint
otherBody = (NewtonJointGetBody0 (joint) != body) ? NewtonJointGetBody0 (joint) : NewtonJointGetBody1 (joint) ;
// See if this Body is in the Dictionary (an STL map will do)
if (!Map.find (otherBody)) {
Map.Insert (otherBody);
// issue the OnEmnetdEvenet Here
OneEnter (otherBody);
} else {
// optinally you can issue a while in trigget here
}
}
// Now you chech for bodies hwo left the Trigger, These are bodies how are in the Map but tha tdo not has trigerJoints
for (node = Map.GetFist(); node; node = nextNodt)
node = NextNode;
NewtonJoint* joint;
// the Body is in the Trigger is it still have a contact Joint
for (joint = NewtonBodyGetFirstContactJoint (body); joint; joint = NewtonBodyGetNextContactJoint (body, joint)) {
if ((node->GetBody() == NewtonJointGetBody0 (joint)) || (node->GetBody() == NewtonJointGetBody1 (joint))) {
break;
}
if (joint) {
// the body left the trigger region, remove from the map and issue and OnExit event
Map.Remove (node);
OneEnter (node->GetBody());
}
}
}
}
As you can see some features are no really feature at all,
It is just diffrent flavors of the same thing but some people add all the stuff to make it looks as if it was bigger.