# Add impulse and torque to a cube

I would like to spawn a cube if I press space and apply a short impulse of force and torque to make the cube rotate. I have some calculations for the impulse but I think they are wrong since the cube only gets faster over time and not fast on the impulse. The formular for the impulse is p = m · v. In my case I decided to give the cube a mass of 1.

At the moment I calculate the whole impulse thing this way in c++:

                f = 8.f / dt; //force = impulse / dt
a = f / 1; //acceleration = force / mass
v += dt * a; //velocity = velocity + a * dt
p = m_pObject1->getPosition();
p += v * (float)event.dt; //position= position + v * dt
m_pObject1->setPosition(glm::vec3(p,m_pObject1->getPosition(), m_pObject1->getPosition()));


I am sure I am doing something wrong here also I am a little bit lost in implementing the torque here. I hope my code and question is clear and I would be happy to see some suggestions. :)

Kind regards

Torque is just a rotational version of the same principle as linear impulses:

Let us look at

F = ma


To compute the change in velocity, we change it to

a = F/m


Likewise we can say:

T = Iw


Where T is torque, I is inertia, and w is angular acceleration. Let's rearrange that:

w = T/I


Now we can use torque to compute the change in rotation. However, angular acceleration is a vector, and inertia is a tensor, that is a matrix, representing the resistance to change in velocity along 3 axes. This creates a small issue, in that we cannot divide a vector by a matrix directly. but let's construct our tensor first:

float ix = cube.height * cube.height;
//cube so all dimensions equal,
//but other objects may not be
ix = (cube.mass * (ix + ix + ix) )/12.0f;
matrix3x3 inertia = {{ix, 0.0f, 0.0f},
{0.0f, ix, 0.0f},
{0.0f, 0.0f, ix}};


But first, we need to compute T. To compute T we need to know where on the cube, the linear impulse is being applied (point p). Once we know this, we can calculate the level vector, t:

vec3 t = p-cube.centre();
// the lever is defined as a vector from the centre of mass, to the point of impulse


Now we can compute the torque:

T = F X t


where T is the cross product of the linear force (F) and the lever(t)

Now, in order to perform our division, we use a little trick. We multiply the torque impulse by the inverse of the inertia tensor, in order to get the change in angular velocity:

w = inertia.inverse() * T;


This will give us our final value. This is a vector, who's magnitude is the amount of acceleration, and the normalized vector is the axis of rotation.

Update:

A final point on updating position/orientation, and calculus. They are not directly changed by forces. An impulse, or force, translates to a change in velocity, or acceleration. It then follows that velocity is a change in position. Calculus is the mathematics of change.

Once you have computed your change in linear velocity, you simply add it to your cubes current velocity. Same with angular velocity.

cube.velocity += a; //a is acceleration: a=F/m
cube.angularVelocity += w; //w is angular acceleration: w = T/I


Then, during each frame, or update, you perform integration on the velocities to make changes to position/orientation, thusly:

position+=velocity * deltaTime; //deltatime is a very small interval (~60th of a second)
orientation+=angularvelocity*deltaTime;


This is known as Euler integration. It is very useful, but not the only way of doing this. You can also look into Verlet integration, which I will not go into here.

• Thanks for the detailed reply, it is much clearer now. The only thing I wonder is, which of this can I use as my new position so that the cube rotates and moves? – potu1304 Nov 4 at 14:32
• You don't directly update position or orientation, I'll update my answer with more detail on this. – Ian Young Nov 4 at 15:46