Move the ball in Bullet's Hello World example

Post Reply
123iamking
Posts: 10
Joined: Wed Oct 11, 2017 4:45 am

Move the ball in Bullet's Hello World example

Post by 123iamking »

I'm studying Hello World example (commit e36b7e0 on Mar 14) of Bullet.
I see that I can only move the ball when the ball is falling with this code:

Code: Select all

btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[1];/*0 is the ground, 1 is the ball*/
btRigidBody* body = btRigidBody::upcast(obj);
body->translate(btVector3(0, 50, 0)); //This will try to move the ball up, but only work when the ball is falling
When the ball is on the ground, I can't move the ball anymore :( I don't understand how this thing work, please help me :cry:

Thanks for reading :)
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Move the ball in Bullet's Hello World example

Post by drleviathan »

You should be doing somthing this:

Code: Select all

btTransform transform = object->getWorldTransform();
btVector3 position = transform.getOrigin();
transform.setOrigin(position + btVector3(0, 50, 0));
object->setWorldTransform(transform)
Also, you probably want to activate the ball whenever you move it, just in case it has deactivated. Otherwise it might move to the new location but not fall.

Code: Select all

object->activate();
123iamking
Posts: 10
Joined: Wed Oct 11, 2017 4:45 am

Re: Move the ball in Bullet's Hello World example

Post by 123iamking »

drleviathan wrote:You should be doing somthing this:

Code: Select all

btTransform transform = object->getWorldTransform();
btVector3 position = transform.getOrigin();
transform.setOrigin(position + btVector3(0, 50, 0));
object->setWorldTransform(transform)
I think translate will make the object move more 'physically like'.
And I just found out that we can also use applyForce() to move the ball (the force has to be big like 1000)

Code: Select all

body->applyForce(btVector3(0, 1000, 0), btVector3(0, 50, 0));
drleviathan wrote: Also, you probably want to activate the ball whenever you move it, just in case it has deactivated. Otherwise it might move to the new location but not fall.

Code: Select all

object->activate();
Thank drleviathan, it works, the method activate() for the rigid body is the answer :)

Code: Select all

btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[1];/*0 is the ground, 1 is the ball*/
btRigidBody* body = btRigidBody::upcast(obj);
/*This will try to move the ball up, 
but only work when the ball is falling, not when it's on the ground*/
body->translate(btVector3(0, 50, 0)); 
/*After add this code, the ball can be moved up when it's on the ground*/
body->activate();
Post Reply