Rolling ball

Post Reply
wil
Posts: 2
Joined: Tue Jan 29, 2019 11:35 am

Rolling ball

Post 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.
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Rolling ball

Post 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.
wil
Posts: 2
Joined: Tue Jan 29, 2019 11:35 am

Re: Rolling ball

Post 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 ?
Attachments
expectation.png
expectation.png (36.72 KiB) Viewed 3909 times
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Rolling ball

Post 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);
    }
}
Post Reply