# How can I control an object using a physics engine?

from the official newton forum (user: Julio Jerez)

How can I control an object using a physics engine?

Most of the time beginners and medium level programmers are intimidated when they are faced with the challenge of controlling a vehicle or character using a real time physics engine.

There are not fundamental differences between a real time physics engine and most in house implementations of real time physics, when the later abide by the laws of physics.

The first thing we need to understand is that torque is not a real quantity, that's why people have such hard time imaging a torque. I certainly do too, if you read in the Newton forum you will notice I almost never talk about torque nor I use torque in any of the demos to control anything.

So if torque does not exist then what is it? Torque is the mathematical consequence of applying Newton's second and third law to a group of particles tight together called rigid body. Leonard Euler in the 1700's deducted the expression by applying Newton's second law to a set of particle an integrating then over the body. He found out that the solution can be decouple from the equations of linear motion for each particle, and that the motion of each particle can be interpolated from the motion of the center of mass, the mass, and the position of the particle relative to the center of distribution. In conclusion the reason it is so hard to imaging torque, is because there is not such thing as principle of conservation of torque, or Euler's second law of torque. Torque plain and simple is a mathematical consequence not a principle.

For example, everybody knows that is you push an object at the center the object moves straight, but if you push the object at an offset from the center, the object tends to rotate to the side opposite to the point where the forces was applied. In reality what happens is that the particles directly on the point where the force was applied starts to move in straight line, and every other particle away from that one will get a reaction force generated by the link connecting them to the neighbor particle. The ones in near proximity get more reaction and they move in the same direction. The one at the origin does not move at all, and the ones past the origin move backwards. The net effect is a rotation of the body.

Looking at the problem this way is a futile and useless effort, however it helps to understand what is going on. Euler was bother by this and he tried to solve the problem mathematically. He found out that the solution to the problem is equivalent to solving a problem where the force is applied to the center of gravity of the body, and a quantity equivalent to R * F (point of focus cross product) to spin the object.

That simplifies the problem a lot. Because now we know (thanks to Euler) we most never use torque to control an object; we should only apply forces at a point.

Two examples to clarity further. Say you have an helicopter in real life, every one knows that to push the helicopter forward we need thrusters on each side, when both thruster push with equal force what you get is:

Code:

F = f1 + f2 T = R1 * f1 + (- R2 * f2) Since R1 = R2, and f1 = f2 F = 2f T = 0

No spin and straight motion

Now say you want to turn right, say also R1 is the right thruster. Every one knows that to do that one must increase the trust in the right thruster, so we get

Code:

F = (f1 + df) + f2 T = R1 * (f1 + df) / R2 * f2

F = 2 * f1 + df T = R * dF > 0

a net torque, plus straight motion

However you will find that this will accelerate during the turn, and in real life the pilots probably slow down during turn, so instead of increasing the right trust, they decrease the left thrust, or they increase the right thruster and decrease the left at the same time. Say they decrease the left.

Code:

F = f1 + (f2 / df) T = R1 * f1 / R2 * (f2 - df)

F = 2f1 /dF T = R1 * df > 0

Still turn to the right and also slow down at the same time.

Also you need to decrease the forces as you move forward. So instead of the thrusters applying a constant force they apply a force that is smaller the faster the helicopter goes, something like this

Code:

F1 = N1 / K * V;

Where N1 is the nominal engine Thrust controlled by the pilot, K is the combine attenuation generated by air drag, internal engine drag etc. and V is the linear velocity of the point where the force is applied on the rigid body. (It is important to notice that V is not the velocity of the object, but the velocity at the point in the body), and then you plug in this into to the previous formulas.

If you get this concept you will find that you will be able to control no just helicopters but anything you want. The good thing about this is that the result will always be realistic. Meaning the model can be set in reality, the only thing separating your model from a real one is how good you are a thinking of how to apply those forces, and also the inevitable errors we get from numerical integration.

To put this into practice, the application must implement a set of utility functions that are not provided by the physics engine as they can be easily written using the generic functionality.

Code:

AddGlobalForce (Force, Point) { R = Point / BodyMatrix.Position; Torque = CrossProduct (R, Force); NewtonAddForce (Force) NewtonAddTorque (Torque) }

AddLocalForce (LocalForce, localPoint) { GlobaForce = BodyMatrixRotate (Force) GlobalPoint = BodyMatrixTranform (localPoint) AddGlobalForce (GlobaForce, GlobalPoint); }

back to the FAQ