Page 1 of 1

Rolling ball

Posted: Tue Jan 29, 2019 5:28 pm
by wil
Hi,
I'm trying to make a ball roll with keyboard arrows just like this game : https://www.youtube.com/watch?v=_D_49vs7c5U
With this code:

Code: Select all

transform.setOrigin(new Ammo.btVector3(p.x(),p.y(),p.z());
ball.userData.physicsBody.setCenterOfMassTransform(transform);
The ball moves but does not roll

how could i do this ?
Thanks for your ideas.

Re: Rolling ball

Posted: Tue Jan 29, 2019 5:55 pm
by drleviathan
Assuming the body is dynamic you need to:
(1) Make sure the body is active.
(2) Apply a force or torque on the ball.

Re: Rolling ball

Posted: Tue Jan 29, 2019 6:47 pm
by wil
I would like the ball to stay on a circle like in the image attached.
How could i do that with forces and torques ?

Re: Rolling ball

Posted: Tue Jan 29, 2019 10:01 pm
by drleviathan
What do you really want to do?

Do you want the ball to literally track a circular path around a known center? or do you want to be able to turn the ball around a corner and just assume it would follow something like a circular arc when doing so?

Assuming you just want to steer the ball around I would suggest doing something like this every substep:

Code: Select all

// compute delta velocity in world-frame
btVector3 deltaLinearVelocity = desiredLinearVelocity - currentLinearVelocity;

// try to effect velocity change if it is big enough
if (deltaLinearVelocity.length2() > someThresholdSquared) {
    
    // compute force that will result in the velocity change
    btVector3 force = (body->getMass() / subStepDuration) * deltaLinearVelocity;
    
    // apply force
    body->applyCentralForce(force);
    
    // always activate body when applying force, else the force accumulates and is all applied at once when it does activate
    if (!body->isActive()) {
        body->activate(true);
    }
}