Trouble with body creation class.

Epimetheus

23-02-2006 21:17:00

I have a class that creates bodies thats called baseBody it has a constructor which sets the variables for the class and then calls createBody with some extra variables that i havent set as class members. In createBody i have this:

mBody->setCustomForceAndTorqueCallback(baseBody::defaultForceCallback);
mBody->setUserData(this);


but when i, in the defaultForceCallback do this:

baseBody* me = (baseBody*)body->getUserData();


and try to call anything from the me variable it just crashes, as if its a null pointer or something.

Can anyone give me an idea of a solution or should i post this in the "back to basics" forum instead?

walaber

23-02-2006 22:44:41

that's definately right... there must be a stupid mistake somewhere, like the setUserdata() isn't being called or something...

Epimetheus

24-02-2006 07:31:34

Could this have anything to do with the fact that im running dagon?

walaber

24-02-2006 07:35:36

no. I've been using Dagon for several months.

Epimetheus

24-02-2006 07:39:50

[Deleted]So you've never encountered such a problem before? :S

Damn i hate being unique :P[/Deleted]

:oops: hmm... dont bother with this problem anymore, it was really stupid and cause by my lack of knowledge about pointers and such.

The trouble was that when i call the constructor i didnt assign it to any variable either and therefor i wrote:
baseBody(mSceneMgr, mWorld, "GZMetalCrate"+StringConverter::toString(i),
baseBody::SP_CUBE, "UVBox.mesh", "MetalCrate/GroundZero",
Vector3(1,1,1), Quaternion::IDENTITY, Vector3(0,i+1,0), 10);


instead of:
new baseBody(mSceneMgr, mWorld, "GZMetalCrate"+StringConverter::toString(i),
baseBody::SP_CUBE, "UVBox.mesh", "MetalCrate/GroundZero",
Vector3(1,1,1), Quaternion::IDENTITY, Vector3(0,i+1,0), 10);

Epimetheus

24-02-2006 08:01:38

Bah! it just solved a small part of the problem, everything still crashes when i call a pointer from that class.

Kerion

24-02-2006 15:47:31

Bah! it just solved a small part of the problem, everything still crashes when i call a pointer from that class.

Well, even if you created the object on the stack (using baseBody(...), rather than new baseBody(...)), the this pointer would still be valid to the object itself. It would be stack allocated, but it would be valid.

When you 'new' an object, you are simply creating it on the heap rather than the stack. There are obvious symantic differences there, as well as concrete ones.

If you can post a more complete code sample, I will see if I can give you a hand to track down the crash.

Epimetheus

24-02-2006 16:36:51

I appreciate the offer but i scrapped the code and started out fresh, so far it works excellent and now i have slightly better code structure so that if the problem where to surface again im more confident in being able to solve it or atleast pin the problem