Bodies don't move

tuxbook
Posts: 10
Joined: Sun Aug 03, 2008 8:40 am

Bodies don't move

Post by tuxbook »

Hi all,

I just had a go at implementing Bullet for the first time into my engine today.

I set everything up just like in CcdPhysicsDemo. Initiation and creation of rigid bodies seems to all be fine.

To move my graphic objects, I give them a pointer to their btRigidBody and then try to get their updated position every frame from it. This code is below.

Code: Select all

btTransform trans;
m_RigidBody->getMotionState()->getWorldTransform(trans);
btVector3& phys_pos = trans.getOrigin();
position.x = phys_pos.getX();
position.y = phys_pos.getY();
position.z = phys_pos.getZ();
Problem is that getOrigin() never returns anything but the original position values.

I figure I'm probably doing something stupid or missing something here. What is the correct way to keep graphics object in sync with rigid bodies? Or is something else the problem?
Rob2687
Posts: 5
Joined: Sat Jun 28, 2008 12:25 am

Re: Bodies don't move

Post by Rob2687 »

Which graphics API are you using?
In OpenGL you can use the 4x4 matrix to apply the tranformations.

Code: Select all

btTransform trans;
float matrix[16];
m_RigidBody->getMotionState()->getWorldTransform(trans);
trans.getOpenGLMatrix(matrix);
glMultMatrixf(matrix);
tuxbook
Posts: 10
Joined: Sun Aug 03, 2008 8:40 am

Re: Bodies don't move

Post by tuxbook »

I'm using a custom DirectX engine. I suppose I could try converting the OpenGL matrix to a DirectX one. I'll try that when I get home, but it still seems like getOrigin should work (unless that only returns where the object was created).
tuxbook
Posts: 10
Joined: Sun Aug 03, 2008 8:40 am

Re: Bodies don't move

Post by tuxbook »

I tried getOpenGLMatrix(), but it too is returning constant values...

And yes, stepSimulation() is being called.

Any ideas?
fullmetalcoder
Posts: 29
Joined: Mon May 19, 2008 5:01 pm

Re: Bodies don't move

Post by fullmetalcoder »

Have you tried setting back the world transform? :roll:

Your btTransform object is a copy of the original transform, not a reference to it. Modifications to it do not get applied to the original transform until you call setWorldTransform()...
tuxbook
Posts: 10
Joined: Sun Aug 03, 2008 8:40 am

Re: Bodies don't move

Post by tuxbook »

All I'm trying to do is get the position of rigid bodies so I can set my graphics objects to their positions. I'm not try to set their transforms, only get them (I assume Bullet is setting them based on gravity and other forces when I call stepSimulation()).
fullmetalcoder
Posts: 29
Joined: Mon May 19, 2008 5:01 pm

Re: Bodies don't move

Post by fullmetalcoder »

Does your body has a valid mass? The motion state of static and kinematic objects are not updated.
Also it is possible that no movement happen because the collision shape is somehow broken (can happen with trimesh) and Bullet discards it.
Have you checked that you passed a valid dt to stepSimulation() ? Have you checked that you actually added the body to your dynamics world?
tuxbook
Posts: 10
Joined: Sun Aug 03, 2008 8:40 am

Re: Bodies don't move

Post by tuxbook »

My bodies have a mass of 2.

I'm using boxes as collision shapes with pretty small dimensions (like .2 by .3 by .2).

I've tried passing both a valid delta time and 1/60 to stepSimulation().

I'm sure that I've added the bodies to the world. I checked the number of bodies in the world and the number was correct.
sparkprime
Posts: 508
Joined: Fri May 30, 2008 2:51 am
Location: Ossining, New York

Re: Bodies don't move

Post by sparkprime »

tuxbook wrote:

Code: Select all

btTransform trans;
m_RigidBody->getMotionState()->getWorldTransform(trans);
This is not the way to get the position/orientation of the rigid body. Use the following:

btRigidBody::getCenterOfMassTransform()

or derive your own btMotionState, putting graphics update code into setWorldTransform
tuxbook
Posts: 10
Joined: Sun Aug 03, 2008 8:40 am

Re: Bodies don't move

Post by tuxbook »

Oh ok, thanks for that sparkprime.

I tried getCenterOfMassTransform(), but the numbers still aren't changing. :(

I tried moving around the initial positions of objects and changing the direction of gravity, tried different box sizes, double checked that all the rigid bodies were created correctly, tried adding an impulse, and tried activating the new rigid bodies. I also looked through CcdPhysicsDemo again line by line and didn't find anything I was missing. I tried constructing the rigid bodies without motion states as well.

I'm looking at the member values of my rigid body now. Variables that seem relevant:

m_linearVelocity: m_y = -1.66
m_angularVelocity: all = 0
m_gravity: y = -20
m_totalForce: all = 0

I thought the last one was interesting, so I tried applying a force to the body. But when I checked the body again, m_totalForce was still 0 and it still wasn't moving.

It's as if the rigid bodies themselves aren't being changed at all, even though they've been added to the world and the simulation is being stepped. I'm using a pointer to the body set right after creation, so that should be fine.

I double checked that dynamicsWorld has my rigid bodies, and getNumCollisionObjects() does return the correct number of 16 (15 boxes, 1 plane; I have tried removing the plane and tried having only one box).

I'm totally out of ideas...

Anyone?
fullmetalcoder
Posts: 29
Joined: Mon May 19, 2008 5:01 pm

Re: Bodies don't move

Post by fullmetalcoder »

turning on debug rendering may help here (the problem could lie in the graphics part or in the communication between physics and graphics)...
tuxbook
Posts: 10
Joined: Sun Aug 03, 2008 8:40 am

Re: Bodies don't move

Post by tuxbook »

Sure, I'm going to give that a shot later, but right now I'm looking at the numbers from the rigid bodies (positions and matrices) in Visual Studio and they're not changing.
fullmetalcoder
Posts: 29
Joined: Mon May 19, 2008 5:01 pm

Re: Bodies don't move

Post by fullmetalcoder »

May sound stupid but I have no ideas left : are you sure stepSimulation() actually gets called? (a simple debug statement near the call would be enough to check).
tuxbook
Posts: 10
Joined: Sun Aug 03, 2008 8:40 am

Re: Bodies don't move

Post by tuxbook »

Yes, I'm absolutely sure. I stepped into stepSimulation() as well, but nothing stood out at me.
sparkprime
Posts: 508
Joined: Fri May 30, 2008 2:51 am
Location: Ossining, New York

Re: Bodies don't move

Post by sparkprime »

Maybe it doesn't work unless you add the btMotionState callback. Try that (instructions are in the manual).