getMotionState() returns NULL

Post Reply
jojojijijojo
Posts: 6
Joined: Sun Oct 13, 2013 5:46 pm

getMotionState() returns NULL

Post by jojojijijojo »

Hi guys,
I'm having this weird problem using bullet 2.82 r2704 where getMotionState() always returns NULL.

Code: Select all

 transform.setIdentity();
							   transform.setOrigin(btVector3(position->x, position->y, position->z));

							   btDefaultMotionState* myMotionState = new btDefaultMotionState(transform);
							   body->setMotionState(myMotionState);
							   if (body->getMotionState()) // this return NULL
							   body->getMotionState()->setWorldTransform(transform); //Code crashes here because getMotionState() is NULL
As you can see, even if I explicitly setMotionState(), I still get a NULL on getMotionState.
Any help is appreciated :?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: getMotionState() returns NULL

Post by drleviathan »

According to the code in my copy of Bullet-2.82... what you claim is happening is not possible.

Therefore it is time for you to do a sanity check:

(1) Make sure you're actually running the executable that you think you're building. One way to do this is to delete the executable that you're running and force a rebuild -- is the executable created anew? (Hint: perhaps you're building in one sandbox and testing in another.) If the new executable is still failing then you should delete it again and insert a compile error in your code so that the build fails. After the build fails check to see if the executable has shown up again -- if it does then you're testing something different than what you're building. I offer up these crazy ideas because I've made such mistakes myself.

(2) If you're sure you're testing what you're building then you need to start sanity checking every line. Either step through it in a debugger or add an assert(myMotionState) right after you create the motion state, and add a similar assert in btRigidBody::setMotionState() and another in btRigidBody::getMotionState() -- basically you have to force it to fail closer to where the error is actually located.

If it continues to fail in mysterious ways after (2) then your "body" pointer is be suspect and you have to add earlier sanity checking.
jojojijijojo
Posts: 6
Joined: Sun Oct 13, 2013 5:46 pm

Re: getMotionState() returns NULL

Post by jojojijijojo »

Thank you for your suggestions,

Yes, I'm running the same exe that I'm debugging. That's for sure.
I have set a couple breakpoints to track the execution and it seems that it fails at the getMotionState() function. If I comment out that line, the code works fine, but of course I got no position transform.

I have also checked the created body memory address, and it matches the body pointer that I'm using to get the motion state, so It is indeed the same created body.

I know this problem is weird, because after checking bullet's getMotionState and setMotionState functions, they seems ok. Hmmm.

m_optionalMotionState, the pointer returned by getMotionstate seems to be valid inside the body, but it is protected so I can't access it directly.

Maybe getMotionState is overridden somewhere?
jojojijijojo
Posts: 6
Joined: Sun Oct 13, 2013 5:46 pm

Re: getMotionState() returns NULL

Post by jojojijijojo »

Btw, is it normal that

Code: Select all

	//btMotionState allows to automatic synchronize the world transform for active objects
	btMotionState*	getMotionState()
	{
		return m_optionalMotionState;
	}
	const btMotionState*	getMotionState() const
	{
		return m_optionalMotionState;
	}
	void	setMotionState(btMotionState* motionState)
	{
		m_optionalMotionState = motionState;
		if (m_optionalMotionState)
			motionState->getWorldTransform(m_worldTransform);
	}
never get actually called by my program? That portion of code is included in the btRigidbody.h header, but when setting breakpoints on it, it is never called.

Could it be because I compiled bullet using the multithreaded-dll runtime library?
jojojijijojo
Posts: 6
Joined: Sun Oct 13, 2013 5:46 pm

Re: getMotionState() returns NULL

Post by jojojijijojo »

As strange as it seems, this problem was fixed but doing a clean compilation of my project. I'm not sure what was the problem, but I believe it was some sort of optimization done by vs++ that messed up things. Hmm.
Post Reply