Sign in to follow this  

Bullet Physics Changing RigidBody Position

This topic is 1909 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to change the position of a RigidBody in Bullet Physics, here is the code that I am using for rendering:

[CODE]dynamicsWorld->stepSimulation(1.f/60.f,10);

//print positions of all objects
for (int j=dynamicsWorld->getNumCollisionObjects()-1; j>=0 ;j--)
{
// -- For each RigidBody
btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[j];
btRigidBody* body = btRigidBody::upcast(obj);
if (body && body->getMotionState())
{
btTransform trans;
body->getMotionState()->getWorldTransform(trans);
model[j]->X = float(trans.getOrigin().getX());
model[j]->Y = float(trans.getOrigin().getY());
model[j]->Z = float(trans.getOrigin().getZ());


//printf("world pos = %f,%f,%f\n",float(trans.getOrigin().getX()),float(trans.getOrigin().getY()),float(trans.getOrigin().getZ()));
}
[/CODE]

I want to be able to change the position (x, y, z), I tried the following but it doesn't work:[CODE]
float newX = // Any new X value here
float newY = // Any new Y value here
float newZ = // Any new Z value here
int modelId = 1;
btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[modelId];
btRigidBody* body = btRigidBody::upcast(obj);
if (body && body->getMotionState())
{
btTransform trans;
// -- Set new transform
trans.setOrigin(btVector3(newX, newY, newZ));
body->getMotionState()->setWorldTransform(trans);
}
[/CODE]

Share this post


Link to post
Share on other sites
[CODE]
void translateBody(btRigidBody *body)
{
body->activate(true);
body->translate(d);
}
[/CODE]
I use this in my engine to translate (note: not the same as just set) the positions of rigid bodies. To go to a specific position, I assume you'd just have to retrieve the original position first

Share this post


Link to post
Share on other sites
I tried that, but it doesn't transform correctly as expected.

Here is what I have tried:
[CODE]float newX = // Any new X value here
float newY = // Any new Y value here
float newZ = // Any new Z value here
int modelId = 1;
btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[modelId];
btRigidBody* body = btRigidBody::upcast(obj);
if (body && body->getMotionState())
{
body->translate(btVector3(newX, newY, newZ));
body->activate(true);
}[/CODE] Edited by Medo3337

Share this post


Link to post
Share on other sites
Instead of directly changing the position, it is better to change the velocity.

Here is some sample code to show how this can work
http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?p=29081#p29081

Share this post


Link to post
Share on other sites
Sign in to follow this