check if a NewtonBody ptr still exists without crashing

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

check if a NewtonBody ptr still exists without crashing

Postby arkdemon » Sat Nov 15, 2014 11:06 am

Hello everyone :).
I wanted to know how we can check if a NewtonBody* is valid / not destroyed. Whenever I call
Code: Select all
if(!body)//if body not destroyed
when "body" is destroyed or inexistant, my program crashes. (body is a NewtonBody* )

My newton version is 3.12 (the last one that works for me) so this issue might have been addressed already.

Thank you for your answers
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm

Re: check if a NewtonBody ptr still exists without crashing

Postby AntonSynytsia » Sat Nov 15, 2014 9:39 pm

I wish there were such methods to check valididy, like:
NewtonIsWorldValid(world_ptr) and it would return 1 or 0.
NewtonIsBodyValid(body_ptr)
NewtonIsCollisonValid(collision_ptr)
etc...

But posting this once, Juleo suggested to use BodyDestructorCallback.
Here is the post: viewtopic.php?f=12&t=8509
AntonSynytsia
 
Posts: 193
Joined: Sat Dec 28, 2013 6:36 pm

Re: check if a NewtonBody ptr still exists without crashing

Postby arkdemon » Sun Nov 16, 2014 12:22 pm

I'm sure I checked the forum before but I didn't find anything. strange...
Anyway thank you for your answer :)
I'll do as you recommend
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm

Re: check if a NewtonBody ptr still exists without crashing

Postby FSA » Sun Nov 16, 2014 3:23 pm

If newton provides such functions it would be a bit pointless. Newton is not responsible to check for valid bodies. The user should check it. Or what is your argument to have such functions? :)
User avatar
FSA
 
Posts: 322
Joined: Wed Dec 21, 2011 9:47 am

Re: check if a NewtonBody ptr still exists without crashing

Postby arkdemon » Sat Nov 22, 2014 9:15 am

Well I found a way using Newton: (didn't check if it works so it might have bugs - because I found it right now :3 )
Code: Select all
    bool ValidBody(NewtonWorld *w, NewtonBody *b){
        for(NewtonBody *_b = NewtonWorldGetFirstBody(w); _b; _b = NewtonWorldGetNextBody(w, _b)){
            if(b == _b) return true;
        }
        return false;
    }


If newton provides such functions it would be a bit pointless. Newton is not responsible to check for valid bodies

If you look at my function, you can see that Newton provides a way to do this :p (not sure if it is the best way to do it though)
My name is arkdemon and I don't approve this message :D
User avatar
arkdemon
 
Posts: 90
Joined: Sat Jan 18, 2014 12:38 pm

Re: check if a NewtonBody ptr still exists without crashing

Postby FSA » Sat Nov 22, 2014 4:42 pm

You are just iterating over the list of bodies. These functions are 100% not for this usage :D
User avatar
FSA
 
Posts: 322
Joined: Wed Dec 21, 2011 9:47 am

Re: check if a NewtonBody ptr still exists without crashing

Postby AntonSynytsia » Sat Nov 22, 2014 5:25 pm

letter123 wrote:You are just iterating over the list of bodies. These functions are 100% not for this usage :D

What do you mean its not 100% for this use?
It determines whether the specified body exists in the world... If it doesn't then false is returned, which means the body is not part of the current world and/or it's destroyed...
AntonSynytsia
 
Posts: 193
Joined: Sat Dec 28, 2013 6:36 pm

Re: check if a NewtonBody ptr still exists without crashing

Postby JoeJ » Sat Nov 22, 2014 6:04 pm

What he means ist most probably that it would be better to set a flag in the user data of a body before it is removed from the world, and clear it when the body is added to the world again.
Now you only need to look at the flag of the body instead of a loop.
Also you could add / remove the body (its user data) to a linked list to quickly find all disabled bodies.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: check if a NewtonBody ptr still exists without crashing

Postby AntonSynytsia » Sat Nov 22, 2014 6:56 pm

JoeJ wrote:What he means ist most probably that it would be better to set a flag in the user data of a body before it is removed from the world, and clear it when the body is added to the world again.
Now you only need to look at the flag of the body instead of a loop.
Also you could add / remove the body (its user data) to a linked list to quickly find all disabled bodies.

The flag technique works too, and I think is the best solution. Although unlike the previous technique by arkdemon, this technique will require to use the destructor callback to determine when the body gets destroyed. There is nothing bad about the destructor callback, I'm just listing the requirements of both techniques.
AntonSynytsia
 
Posts: 193
Joined: Sat Dec 28, 2013 6:36 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron