A place to discuss everything related to Newton Dynamics.
Moderators: Sascha Willems, walaber
by arkdemon » Sat Feb 08, 2014 1:54 pm
Hello everyone. I have a problem with NewtonBodySetUserData and NewtonBodyGetUserData: even if I set NewtonBodySetUserData correctly, when I get the user data with NewtonBodyGetUserData (with NewtonWorldGetFirstBody), it returns wrong values which makes my program crash. However, it works when I have the NewtonBody pointer but not when I do this:
- Code: Select all
for(NewtonBody *b = NewtonWorldGetFirstBody(world); b != NULL;
b = NewtonWorldGetNextBody (world, b)){
info = (Info*)NewtonBodyGetUserData(b);//wrong values
So here is the code:
- Code: Select all
static inline void DynamicBox(glm::vec3 pos, float mass, glm::vec3 d, Info &info,
NewtonBody *b){
dgMatrix m = dgMatrix(dgQuaternion(), dgVector(pos.x,pos.y,pos.z,1.f));
NewtonCollision *c = NewtonCreateBox(world, d.x, d.y, d.z, 0, NULL);
NewtonBody *b = NewtonCreateDynamicBody(world, c, &m[0][0]);
NewtonBodySetUserData(b, (void*)(&info));
NewtonBodySetMassProperties(b, mass, c);
NewtonDestroyCollision(c);
NewtonBodySetForceAndTorqueCallback(b, ApplyForceAndTorque);
NewtonBodySetTransformCallback(b, ApplyTransform);
Info *i = (Info*)NewtonBodyGetUserData(b);//It gets the correct value
printf("%u %u %.3f %i\n", i->m, i->t, i->life, NewtonBodyGetID(b));//It returns the correct value
return b;//not necessary but sometimes I need it to set a velocity
}
Thank you for your help
My name is arkdemon and I don't approve this message

-

arkdemon
-
- Posts: 90
- Joined: Sat Jan 18, 2014 12:38 pm
by Julio Jerez » Sat Feb 08, 2014 2:41 pm
That can not be possible, getFirstBody getNextBody simple iterate over the list of bodies, you must have a bug.
check to see if the pointe to the body that you get form getFirstBody I sthe body that you expect.
-
Julio Jerez
- Moderator

-
- Posts: 12426
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
by Julio Jerez » Sat Feb 08, 2014 2:46 pm
my guess Is that the problem is here: static inline void DynamicBox(glm::vec3 pos, float mass, glm::vec3 d, Info &info, NewtonBody *b){
make sure that Info &info is not a local object created in the stack because it gets destroyed when the function returns.
That will make it work in the second code fragment but fail when you try to read after the function return.
-
Julio Jerez
- Moderator

-
- Posts: 12426
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
by arkdemon » Sat Feb 08, 2014 5:00 pm
Hello, thank you a lot! Yes, it was because the user data was destroyed after the function ended. Thank you!
My name is arkdemon and I don't approve this message

-

arkdemon
-
- Posts: 90
- Joined: Sat Jan 18, 2014 12:38 pm
Return to General Discussion
Who is online
Users browsing this forum: No registered users and 5 guests