[solved]Changing NewtonBody pointer

Report any bugs here and we'll post fixes

Moderators: Sascha Willems, Thomas

[solved]Changing NewtonBody pointer

Postby Bird » Fri Nov 08, 2019 6:49 am

Hi Julio,

I just updated to the latest version of Newton and I'm having some stability problems. When I create a new NewtonBody, I save it's pointer in my own graphics object so I can access it later.

Code: Select all
void NewtonBodyHandler::addBodyToEngine(RenderableNode& node)
{
   NewtonBody * const newtonBody = ops.createRigidBody(node, startPose, collision);
   NewtonBodySetUserData(newtonBody, node.get());
   node->setUserData(static_cast<NewtonBody*>(newtonBody));
}


Later when I try to use the NewtonBody pointer that I saved, any Newton function I call with it is corrupt.

Code: Select all
NewtonCollision * NewtonOps::createCollisionShape(RenderableNode& node)
{
   if (node->isInstance())
   {
      RenderableNode from = node->getInstancedFrom();
      
       NewtonBody * const newtonBody = static_cast<NewtonBody*>(from->getUserdata());
      
      return NewtonBodyGetCollision(newtonBody);
   }
}


If I debug the NewtonBodyGetCollision(newtonBody) call I can see the passed in NewtonBody pointer is filled with garbage. I can workaround the problem by iterating through the all the NewtonBodies but I'd rather not have to do that if possible

Code: Select all
// find the NewtonBody for the instanced from node the hard way
for (NewtonBody* body = NewtonWorldGetFirstBody(world); body; body = NewtonWorldGetNextBody(world, body))
{
   Renderable* const node = static_cast<Renderable*>(NewtonBodyGetUserData(body));
   RenderableNode renderable = node->shared_from_this();

   if (renderable->isInstance()) continue;
   if (renderable == from)
      return NewtonBodyGetCollision(body);
}


My same code worked with previous versions of Newton

Here'a little sim I made with Newton and NVIDIA's Optix7 ray tracing framework
https://www.youtube.com/watch?v=U0WJWECeZjA&feature=youtu.be
Attachments
corrupt_newton_body.jpg
corrupt_newton_body.jpg (248.56 KiB) Viewed 5251 times
Last edited by Bird on Fri Nov 08, 2019 6:08 pm, edited 1 time in total.
Bird
 
Posts: 623
Joined: Tue Nov 22, 2011 1:27 am

Re: Changing NewtonBody pointer

Postby Sweenie » Fri Nov 08, 2019 10:56 am

I just updated to the latest Newton version in my application(not the Unity plugin) and it's still working as normal for me. I do the same, store my render object in the body and the render object store a reference to the body, the same way you do.

Have you verified that the body ptr is valid when you call this line?
Code: Select all
NewtonBody * const newtonBody = ops.createRigidBody(node, startPose, collision);
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Changing NewtonBody pointer

Postby Bird » Fri Nov 08, 2019 1:31 pm

Yes, I've checked it immediately after calling ops.createRigidBody() and that pointer is valid.

Thanks for the info. I guess this must be on my end somewhere though I can't imagine where.
Bird
 
Posts: 623
Joined: Tue Nov 22, 2011 1:27 am

Re: [solved]Changing NewtonBody pointer

Postby Bird » Fri Nov 08, 2019 6:10 pm

d'uh. I was resetting the userdata to an OptixMesh* in the Optix module.

Thanks Sweenie for getting me to look more closely at my code!
Bird
 
Posts: 623
Joined: Tue Nov 22, 2011 1:27 am


Return to Bugs and Fixes

Who is online

Users browsing this forum: No registered users and 5 guests