A place to discuss everything related to Newton Dynamics.
Moderators: Sascha Willems, walaber
by MeltingPlastic » Fri Sep 07, 2018 12:59 pm
Hi Julio,
I am having trouble parsing the contact joints.
My goal is to send 3 signals when bodies 1. start colliding, 2. continue colliding. and 3. stop colliding.
At first I thought it would be possible to communicate with the main thread from within the Process Contacts Callback. but that required me putting a mutex lock whenever I access my own custom "contacts list" in the main thread. Putting a mutex in the callback gives me an race condition when I call to NewtonWaitForUpdateFinished() from the main thread.
So my second attempt is to simply parse all contacts on all bodes from the main thread only (and use the Process Contacts Callback for Material interactions only).
However when I have this code in the main thread:
- Code: Select all
NewtonJoint* curJoint = NewtonBodyGetFirstContactJoint(body);
while (curJoint)
{
URHO3D_LOGINFO("Joint!");
curJoint = NewtonBodyGetNextContactJoint(body, curJoint);
}
I get joints when the AABB boxes overlap and not when they actually collide.
Whats the best and fastest approach for this?
Hope that makes sense.
Trevor
Last edited by
MeltingPlastic on Fri Sep 07, 2018 4:54 pm, edited 2 times in total.
-
MeltingPlastic
-
- Posts: 237
- Joined: Fri Feb 07, 2014 11:30 pm
by Sweenie » Fri Sep 07, 2018 1:26 pm
ContactJoints can be active or inactive.
This is how Julio wrote the contact iterator in the Unity Plugin which suffered from the same problem you had with contact callbacks from other threads.
- Code: Select all
void* dNewtonWorld::GetNextContactJoint(dNewtonBody* const body, void* const contact) const
{
NewtonBody* const newtonBody = body->m_body;
for (NewtonJoint* contactJoint = NewtonBodyGetNextContactJoint(newtonBody, (NewtonJoint*)contact); contactJoint; contactJoint = NewtonBodyGetNextContactJoint(newtonBody, contactJoint)) {
if (NewtonJointIsActive(contactJoint)) {
return contactJoint;
}
}
return NULL;
}
void* dNewtonWorld::GetFirstContactJoint(dNewtonBody* const body) const
{
NewtonBody* const newtonBody = body->m_body;
for (NewtonJoint* contactJoint = NewtonBodyGetFirstContactJoint(body->m_body); contactJoint; contactJoint = NewtonBodyGetNextContactJoint(newtonBody, contactJoint)) {
if (NewtonJointIsActive(contactJoint)) {
return contactJoint;
}
}
return NULL;
}
-
Sweenie
-
- Posts: 498
- Joined: Mon Jan 24, 2005 7:59 am
- Location: Sweden
by MeltingPlastic » Fri Sep 07, 2018 2:02 pm
Thanks Sweenie!
That works. However now I sometimes miss collisions entirely if the the collision is very instantaneous. Such as a bouncing ball with high velocity.
I am parsing the contacts right after a call to NewtonWaitForUpdateToFinish().
Edit: If I lower the engine substeps from 16 to 1 then it seems to catch all collisions. I suppose I can just increase the iteration count instead of substeps.
-
MeltingPlastic
-
- Posts: 237
- Joined: Fri Feb 07, 2014 11:30 pm
by Julio Jerez » Fri Sep 07, 2018 2:50 pm
if you use NewtonWaitForUpdateToFinish(). every frame you defeat the purpose of multicore
you should be able to get the imnfropmatiopn form the contact callback, all the demos do it that way.
also why do you have to do 16 sub step? that does not seems right?
the demos do 2 which will be running at about 128 sim per seconds.
has you implemented frame interpolation? basically you run the graphics at a rate and the engine at a different rate.
them all you logic is implement in simulation time and you get the callback per sim steps..
-
Julio Jerez
- Moderator
-
- Posts: 12249
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
by MeltingPlastic » Fri Sep 07, 2018 3:13 pm
The Overall Engine has a constant update rate of 60hz. Rendering happens at 200hz and renders the result of the last update (currently no tweening but thats a todo).
I call NewtonWaitForUpdateToFinish() to at the end of each 60hz step. then I apply newton body transforms to the urho scene graph. then I call NewtonUpdateASync() at the end so that the newton simulation will run while renders are happening and other updates are run on the next step.
Usually I NewtonUpdateASync does not block unless I have alot of bodies present.
I was using 16 substeps for accuracy at one point - I play with that number alot..
Are you saying I can do without NewtonWaitForUpdateToFinish() altogether and just call NewtonUpdateASync() repeatedly?
Edit: Just tried that and it does work. But then I am never really sure if the simulation has finished one overall "step" for example how would I know how current the rigid body transforms are?
Double Edit: Just found NewtonWorldCriticalSectionLock() ! now I can sync data from the callback!
-
MeltingPlastic
-
- Posts: 237
- Joined: Fri Feb 07, 2014 11:30 pm
Return to General Discussion
Who is online
Users browsing this forum: No registered users and 50 guests