User Data Problem.

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

User Data Problem.

Postby Auradrummer » Tue Apr 07, 2009 9:37 am

Hello guys,

I'm trying to assign a integer as UserData and then attach it to a body, to be able to get it on the Callbacks.

In the same .cpp where the body is created I put this piece of code and worked well.

Code: Select all
   userData = &ID;
   NewtonBodySetUserData(suspension[ID].body, userData);
   int * test;
   test = (int *) NewtonBodyGetUserData (suspension[ID].body);
   printf ("User data %u \n", *test);


But, the same code inside the CallBack (that is in another .cpp), the printed value was crazy. Any tip?

Thanks
Developing a racing game.
Auradrummer
 
Posts: 132
Joined: Sat May 17, 2008 9:17 am

Re: User Data Problem.

Postby Julio Jerez » Tue Apr 07, 2009 9:41 am

make sure it is teh same body.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: User Data Problem.

Postby Auradrummer » Tue Apr 07, 2009 10:24 am

Hi Master,

Yes, is the same body, because I get the mass using the GetMassMatrix and I received the exact value I assign.
Developing a racing game.
Auradrummer
 
Posts: 132
Joined: Sat May 17, 2008 9:17 am

Re: User Data Problem.

Postby Auradrummer » Tue Apr 07, 2009 10:33 am

The crazy value that is shown is changing every time I run the program...
Developing a racing game.
Auradrummer
 
Posts: 132
Joined: Sat May 17, 2008 9:17 am

Re: User Data Problem.

Postby martinsm » Tue Apr 07, 2009 10:58 am

It's better to store integer by value, not by pointer:
Code: Select all
userData = (void*)ID;
NewtonBodySetUserData(suspension[ID].body, userData);

int test;
test = (int)NewtonBodyGetUserData(suspension[ID].body);
printf ("User data %u \n", test);


Integer value can change location in memory so you must be careful when storing pointer to it. But in value case you don't care about how and where in memory value resides.
martinsm
 
Posts: 86
Joined: Mon Dec 19, 2005 3:15 pm
Location: Latvia

Re: User Data Problem.

Postby Auradrummer » Tue Apr 07, 2009 3:20 pm

Very creative solution.
If I understood well, you passed the value of the variable 'ID' as it was a memory address. So, further, I get this 'memory address' back, and recover the integer value. Very good.

The only thing I had to mention is that (void*) pointer didn't work. I got the message 'cannot convert *void to *int'. So, I declared the pointer as (int *) and I recovered it at the Callback without problems.

Thanks very much, I think this tip is very useful to a large number of people.
Developing a racing game.
Auradrummer
 
Posts: 132
Joined: Sat May 17, 2008 9:17 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 10 guests

cron