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

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

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

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 deducted the expression by applying Newton's second law to a set of particles an integrating then over the body. He found out that the solution can be decoupled 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: Select all
`F = f1 + f2T = R1 * f1 + (- R2 * f2)Since R1 = R2, and f1 = f2F = 2fT = 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: Select all
`F = (f1 + df) + f2T = R1 * (f1 + df) - R2 * f2F = 2 * f1 + dfT = 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: Select all
`F = f1 + (f2 - df)T = R1 * f1 - R2 * (f2 - df)F = 2f1 - dFT = 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: Select all
`F1 = N1 - K * V;`
Where N1 is the nominal engine Trust 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: Select all
`AddGlobalForce (Force, Point){   R = Point - BodyMatrix.Position;   Torque = CrossProduct (R, Force);    NewtonAddForce (Force)   NewtonAddTorque (Torque)}AddLocalForce (LocalForce, localPoint){   GlobaForce = BodyMatrixRotate (LocalForce)   GlobalPoint = BodyMatrixTranform (localPoint)   AddGlobalForce (GlobaForce, GlobalPoint);}`
Julio Jerez
Moderator Posts: 11086
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

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

Julio Jerez wrote: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 deducted the expression by applying Newton's second law to a set of particles an integrating then over the body. He found out that the solution can be decoupled 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. Now do you think the Pro Extender works? I read on https://peninja.com/my-proextender-review-and-results that it works. If you need to lose weight. 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: Select all
`F = f1 + f2T = R1 * f1 + (- R2 * f2)Since R1 = R2, and f1 = f2F = 2fT = 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: Select all
`F = (f1 + df) + f2T = R1 * (f1 + df) - R2 * f2F = 2 * f1 + dfT = 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: Select all
`F = f1 + (f2 - df)T = R1 * f1 - R2 * (f2 - df)F = 2f1 - dFT = 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: Select all
`F1 = N1 - K * V;`
Where N1 is the nominal engine Trust 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: Select all
`AddGlobalForce (Force, Point){   R = Point - BodyMatrix.Position;   Torque = CrossProduct (R, Force);    NewtonAddForce (Force)   NewtonAddTorque (Torque)}AddLocalForce (LocalForce, localPoint){   GlobaForce = BodyMatrixRotate (LocalForce)   GlobalPoint = BodyMatrixTranform (localPoint)   AddGlobalForce (GlobaForce, GlobalPoint);}`

This is very helpful Julio. Trying to figure out how to make things move naturally in a physics engine is quite a daunting task but this is a good post to get me on the right track.

It gets complicated with things like helicopters doesn't it because there are several forces that affect it.

Thanks anyway.
Last edited by YellowCat on Sat Sep 17, 2016 10:45 am, edited 4 times in total.
YellowCat

Posts: 1
Joined: Thu Dec 03, 2015 5:12 pm

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

Julio,

you lost me with the helicopter analog Unless I am not getting something fundamentally, helicopter has one big propeller, which is able to tilt in all directions. This mechanism is called "cyclic". Helicopter moves by having a pilot tilt this big propeller: front down for forward, back down for backwards. left edge down for left, and right edge down for right.

Now - this big propeller, when it spins, creates a lot of torque on the propeller axis in the opposite direction of spin. That's why a helicopter also has a vertical tail rotor which basically blows air to counter the propeller torque and keep the helicopter flying straight.

There are no left or right thrusters... I am not sure if you were thinking of any other aircraft, butthe analogy with helicopter is definitely confusing. Cheers,
Misho
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho

Posts: 566
Joined: Tue May 04, 2010 10:13 am