## Applying a torque to a sphere around a given axis

nero1402
Posts: 2
Joined: Mon Oct 26, 2020 3:55 pm

### Applying a torque to a sphere around a given axis

Hi there

I'm new to bullet and making a 3d game where you control a rolling ball on a heightmap terrain. I have the collisions all working with a btHeightfieldTerrainShape. The ball (a btSphereShape) rolls and bounces nicely around the terrain.

I have a 'camera' that is placed just above the ball and can be moved to face any direction with the mouse.

Now I want to apply a torque that causes the ball to move in the direction the camera is facing eg. (1, 1, 0).

By this I mean I take the x and z part of the camera's direction vector (from the player's perspective this is the direction they are 'facing' without the up/down part) which would be (1, 0, 0)

I then want to create a vector that lies horizontally, perpendicular to the y axis and the 'xz' vector I mentioned above:

A clockwise rotation makes it (0, 0, -1). Relative to the view on the screen this vector would lie horizontally left to right across the screen.

Now the bit I can't do.

Can I apply a torque to the sphere clockwise around the vector I've calculated (0, 0, -1) through centre of the sphere? Is this possible and what is the best way to do it with bullet?

I hope that made sense, it's sort of like the torque applied to a car wheel if you are facing in the direction the wheel is rolling.

drleviathan
Posts: 657
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

### Re: Applying a torque to a sphere around a given axis

As per the documentation these methods are available for btRigidBody and can apply torque directly:

Code: Select all

``````void 	applyTorque (const btVector3 &torque)
void 	applyTorqueImpulse (const btVector3 &torque)
void 	applyImpulse (const btVector3 &impulse, const btVector3 &rel_pos)``````
However, those only apply once (for the simulation step, I think, rather than just for the first substep when there are multiple substeps on the step, but I'm not 100% sure about that). In any case, to gradually control the ball with torque you'd have to re-apply before each step.

The units of torque to achieve the results you want are tricky, whether you're using torque proper or torque impulse. This is ok if your ball is always a finite set of masses: you can tune by trial and error and not care about the units. An alternative way to do it with simpler units (and possibly mass-agnostic) is to just compute an adjustment to the sphere's angular velocity and slam it:

Code: Select all

``````old_angular_velocity = body->getAngularVelocity();
body->setAngularVelocity(new_angular_velocity);``````
If you're applying little torques/impulses/velocities all the time then an easy way to fit your torque calculation/application into the flow of the physics simulation is to implement a CustomAction from btActionInterface and override its pure virtual updateAction() method to do what you want. If you add that Action to the simulation then Bullet will call its updateAction() every substep.
nero1402
Posts: 2
Joined: Mon Oct 26, 2020 3:55 pm

### Re: Applying a torque to a sphere around a given axis

Thanks for your help drleviathan. I need to understand how applyTorque works. Perhaps I'm going about this the wrong way by thinking of torque as rotation around an axis.

I'll experiment a bit and see what works best. Thanks again.