Julio or anyone. Forces and Torque help?

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: Julio or anyone. Forces and Torque help?

Postby Gibbon » Thu Mar 10, 2011 4:47 am

Hi Julio, You are correct, but i now have an ok understanding of the cross & dot product and well in the code you gave me there isnt much more than that?

Here is my lua code to show ONLY the calculations.

ever loop it calls: function addforce()

which then loops through 2 wing surfaces (like a paraglider) and does the calculations as below, but all the airplane does is fall to the ground?


Code: Select all
//wing airfoil data
Cl = {-0.54,-0.2,0.2,0.57,0.92,01.21,1.43,1.4,1.0} //Airfoil lift data
Cd = {-0.01,-0.0074,0.004,0.009,0.013,0.023,0.05,0.12,0.21} //Airfoil drag data
angle = {-8.0,-4.0,0.0,4.0,8.0,12.0,16.0,20.0,24.0} //Wing angle for array ref

   //calculate wing lift (simply return the Cl value from array based on AOA)
   function coeficient_lift(aoa)
      for i=1,8 do
         if angle[i] <= aoa and angle[i+1] > aoa  then
            CL = Cl[i]
         end
      end
   return CL
   end

   
   //calculate wing drag (simply return the Cd value from array based on AOA)
   function coeficient_drag(aoa)
      for i=1,8 do
         if angle[i] <= aoa and angle[i+1] > aoa then
            CD = Cd[i]
         end
      end
   return CD
   end


   //calculate force and torque of given airfoil data
   function calcforcetorque(body)

      //Calculate lift and drag of given airfoil
      calcliftforce(body)
      calcdragforce(body)
   
      //Total Force
      total_airfoil_force = liftforcedir

      //Total Torque
      airfoilorigin = airplane:GetPosition(0) //global position of airplane
      airfoil_torque = cross_product(airfoilorigin,total_airfoil_force)

   end


   //Calculate airfoil lift force
   function calcliftforce(body)
   
      //Find Velocity
      velocity = GetBodyVelocity(parts[body])
      unit_velocity = normalize(velocity) --velocity normalized
   
      //Find AOA
      aoa = calcaoa(body,unit_velocity.x,unit_velocity.y,unit_velocity.z)

      //find coefienct lift (turn is input value to make airplane turn)
      CL = coeficient_lift(aoa) + turn
   
      //Lift direction vector
      airfoil_lift_cross = cross_product(aoa_vector,unit_velocity)
      unit_lift_vector = normalize(airfoil_lift_cross)

      //lift magnitude
      velocity_dot = dot_product(velocity,velocity)
      lift_magnitude  = math.sqrt(velocity_dot*velocity_dot) * CL

      //lift force and direction
      liftforcedir = Vec3(unit_lift_vector.x*lift_magnitude,unit_lift_vector.y*lift_magnitude,unit_lift_vector.z*lift_magnitude)
   end

   //Calculate airfoil drag force
   function calcdragforce(body)
   
      //Find Velocity
      velocity = GetBodyVelocity(parts[body])
      unit_velocity = normalize(velocity) --velocity normalized
   
      //Find AOA
      aoa = calcaoa(body)

      //find coefienct drag
      CD = coeficient_drag(aoa,unit_velocity.x,unit_velocity.y,unit_velocity.z)
   
      //drag direction vector
      airfoil_drag_cross =  cross_product(aoa_vector,unit_velocity)
      unit_drag_vector = normalize(airfoil_drag_cross)

      //drag magnitude
      velocity_dot = dot_product(velocity,velocity)
      drag_magnitude  = math.sqrt(velocity_dot*velocity_dot) * CD

      //drag force and direction
      dragforcedir = Vec3(unit_drag_vector.x*drag_magnitude,unit_drag_vector.y*drag_magnitude,unit_drag_vector.z*drag_magnitude)
   end
   
   //Aoa
   function calcaoa(body,unit_velocityx,unit_velocityy,unit_velocityz)

      cogmatrix = GetEntityMatrix(airplane)
      cogfront = Vec3(cogmatrix.ix,cogmatrix.iy,cogmatrix.iz) //Front from matrix
      airspeed = Vec3(unit_velocityx,unit_velocityy,unit_velocityz) //unit airspeed
      aoa_vector = cross_product(airspeed,cogfront) //aoa vector   
      airfoilaoa = math.asin(dot_product(cogfront,aoa_vector)) //aoa

      return airfoilaoa
   end

   
   //add the forces
   function addforce()

   force = Vec3(0,mass,0)
   torque = Vec3(0,0,0)

        //Loop through the 2 wing surfaces
      for i=1,2 do
           calcforcetorque(i)
           force = add2vec(force,total_airfoil_force) //Adds the forces
           torque = add2vec(torque,airfoil_torque) //Adds the torques
      end
   
   airplane:SetForce(Vec3(force),1) //Set the force (globally?)
   airplane:SetTorque(Vec3(torque),1) //Set the torque (globally?)
   end
               
     main loop
     
     //start calculations
     addforce()
   
     endloop

Gibbon
 
Posts: 44
Joined: Tue Dec 15, 2009 5:18 am

Re: Julio or anyone. Forces and Torque help?

Postby JernejL » Thu Mar 10, 2011 5:41 am

Gibbon, i believe this thread might help you out:

http://www.newtondynamics.com/forum/vie ... f=9&t=5106

Especially see my reply there with some utility functions (GetPointLocalVelocity & AddPointLocalForce), these two functions might help out your calculations since you can easily calculate lift forces just as vectors and not worry about converting it to torque to apply it, you just apply a vector force at point in LOCAL space on body (AddPointGlobalForce is also provided there).

Julio: could these utility functions there be added to the engine? I think they would help new users a lot, and everyone has at some point written a custom version of them, but having them as part of newton would simplify precise control such as mouse picking, AddPointLocalForce is a very important part of raycast vehicles and any more complex movement control, because most of time you have the object perform some movement, and you always work on that from object's "local space".

Just forces in local points on a body are much easier to understand when you deal just with them. Working with torque directly in global space is sometimes too complex for people to understand.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Julio or anyone. Forces and Torque help?

Postby Gibbon » Thu Mar 10, 2011 7:29 am

Hi Delfi, thanks for your input, gives me some more motivation to carry on.

This does seem alot more easier to me, and also as Leadwerks has a command to AddForceAtPoint locally and globally.

Few things i would like to ask though.

Do i still calculate the lift and drag etc as i have been, but instead of calculating them up into one force and torque do i just add the forces at point, to each given surface every loop?

If you could just elaborate a little it would be much appriciated. I read your functions and they will come very useful, i just need a little clearing on the principle of doing it this way as apposed to how julios been trying to tell me.

Thanks
Andy
Gibbon
 
Posts: 44
Joined: Tue Dec 15, 2009 5:18 am

Re: Julio or anyone. Forces and Torque help?

Postby JernejL » Thu Mar 10, 2011 7:35 am

You calculate forces each time the forcetorque callback is called - this is whenever you call newtonupdate - the clalback triggers 1 time per body, and at that point you calculate the aerodynamic formulas for each defined point on your aircraft (flaps, rudder, etc..).
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Julio or anyone. Forces and Torque help?

Postby JoeJ » Thu Mar 10, 2011 7:57 am

I can't follow the code exactly - but some things looking wrong:

Looking at angle array, is it based on degrees? asin gives radians.
Also, what happens if aoa is not in between of any angle[x] and angle[x+1]?
math.sqrt(velocity_dot*velocity_dot) gives no sense - got something wrong there?
what happens if vectors are too small to normalize? feed simulation with NANs?
Have you tried to scale the forces, maybe with mass or some big values? Still falling straight downwards?

Maybe good idea try some easier first, like Spaceship? No gravity, no air... just some thrusters in local spaceship space.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Julio or anyone. Forces and Torque help?

Postby Gibbon » Thu Mar 10, 2011 8:07 am

Hi Delfi, i see that but what i mean is.

How does this method do away without having to calculate torque?

What im trying to ask is, i know that if i apply a ForceAtPoint on a body then torque will be generated, yes? But do i actually AddForce to each wing body? Or calculate the force and add it to the main body?

Hi Joe:

I suppose when AOA is not between the two it errors, so i would have to impliment a check for this.

math.sqrt(velocity_dot*velocity_dot).. Yes your correct think i should have that as just "velocity_dot"

what happens if vectors are too small to normalize? feed simulation with NANs? <-- i presume these will aslo result in errors

I have tried to scale the forces but have no look.

The other problem is see is, when i run it, it obviously has no forward velocity. how can i add a velocity to it?

Thanks
Andy
Gibbon
 
Posts: 44
Joined: Tue Dec 15, 2009 5:18 am

Re: Julio or anyone. Forces and Torque help?

Postby JoeJ » Thu Mar 10, 2011 10:06 am

NewtonBodySetVelocity is function to set velocity.
At the very first step there is no velocity, so the normalisation generates NANs.
This could explain why from this point on nothing has any effect.

Do you use different bodys for wings and "cockpit"?
Why and how are they connected?
I'd suggest to use one body for the whole aeroplane, and apply forces and resulting torques at a point near the wings tips,
using the ApplyForce functions you already know.

Do you visualize all vectors like unit_lift_vector with a colored line in graphics engine in real time and always?
This helps to proof and understand - no way around it.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Julio or anyone. Forces and Torque help?

Postby Gibbon » Thu Mar 10, 2011 10:25 am

Hi Joe, thanks for getting back. You always seem to make things clear! :)

Yes, thats what i thought as lift obviously is generated from the velocity, so without it.. no lift. And it appears i forgot about that command, i always "addforce" in direction of thrust vector.

Yes i use different bodies to build up my plane, only reason for this is because i "thought" that was the correct way, and i use a fixed joint between them?

And yes i can see now how having just one body would make things easier.

BUT i get confused about is:

You say "apply forces and resulting torques at a point near the wings tips,using the ApplyForce functions you already know."

Where as julios way always seem to end up with one "force value & vector"? (which confuses me)

doing it as you say, are you suggesting i have say a force for the left wing, a force for the right wing ETC?

I do not yet visulise but it is somthing that only lastnite i started to impliment as this would be a big help. I do obvisouly ouput the values to text on the screen to try and see what is happeneing.

Thanks
Andy
Gibbon
 
Posts: 44
Joined: Tue Dec 15, 2009 5:18 am

Re: Julio or anyone. Forces and Torque help?

Postby JoeJ » Thu Mar 10, 2011 10:53 am

Yes, you can put force on two points of one body. Just compute one independent of the other and sum both forces and both torques and then apply to the single body (ignoring my doubts from the pencil / energy stuff).

My initial guess was simpler: Compute force from lift and drag in relation aeroplane orientation <-> velovity + wind.
I know nothing about aerodynamics but assume aoa is equal for both wings if they are aligned on one direction?

And both torques cancel each other out so only a single force is result with both methods in most cases.

However, it looks like you are close. Visualisation will help and also the normalization check.
Giving up is not an option, i want to see the video :)
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Julio or anyone. Forces and Torque help?

Postby Gibbon » Thu Mar 10, 2011 11:19 am

ok, i will try and impliment / fix these problems to try again! I shall not give up (yet!) :)

I dont want to feeling like im bugging you but i am still not understanbding what you mean with this:

Yes, you can put force on two points of one body. Just compute one independent of the other and sum both forces and both torques and then apply to the single body (ignoring my doubts from the pencil / energy stuff).


Why if im adding forces at two points of the body do i have to compute the sum of the forces and torques and apply them to the body too?

yes for to keep it simple, AOA is the same for each wing.

So.. i shall, change to use one body only.

Its this bit im confused with...

Do i... Apply two forces to the body, at each tip of the wing?

Or... Sum the forces and torques and apply the resulting force and torque to the body?


Thanks
Andy
Gibbon
 
Posts: 44
Joined: Tue Dec 15, 2009 5:18 am

Re: Julio or anyone. Forces and Torque help?

Postby JoeJ » Thu Mar 10, 2011 11:57 am

No reason for confusion...
Using a single body is for sure better than more bodies with fixed joints.
So apply to wing OR plane is no question anymore.

If you compute two forces/torques at the wing tips, it's the same if you call BodyAddForce()/BodyAddTorque() twice,
or if you sum up both forces/torques yourself and call once with sum.

What i mean is: In your case the torques will be equal but opposite and sum to zero.
So you can just calculate one single force and don't need to care about torque.
This holds true if wings are aligned through center of mass, and steering is handled differently.
But looking at your code i'd first keep the 2 forces way, as it's more complete and could include the steering.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Julio or anyone. Forces and Torque help?

Postby Gibbon » Fri Mar 11, 2011 5:04 am

Hi, as im now going to use only a single body, does it matter what shape the body is?

Does it just be a box? Or should it be a convex hull?

Thanks
Andy
Gibbon
 
Posts: 44
Joined: Tue Dec 15, 2009 5:18 am

Re: Julio or anyone. Forces and Torque help?

Postby JoeJ » Fri Mar 11, 2011 6:15 am

A compound collision of two boxes should give a nice approx. inertia, which affects simulation in air.
Later you can create a compound of 2-4 convex hulls for accurate collisions.

If compound setup is complicated, you can also use single box for now, choose dimensions by thinking on how difficult it should be to rotate about axis.
For classic aeroplane i would cut both wings at half, then use bounding box.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Julio or anyone. Forces and Torque help?

Postby Gibbon » Fri Mar 11, 2011 8:01 am

Hi Joe, ok a bit of understanding is need here then.

2 compound collision boxes???

This might be just a leadwerks thing as currently what i do in leadwerks is just createbody and that body can be a box, sphere, cone, tree or hull?

So up until now, i have used 3 box bodies, 1 for left wing and 1 for right wing and one for main plane, then i join left wing to main plane and right wing to main plane using fixed joint. But now we using 1 body?

Thanks
andy
Gibbon
 
Posts: 44
Joined: Tue Dec 15, 2009 5:18 am

Re: Julio or anyone. Forces and Torque help?

Postby JoeJ » Fri Mar 11, 2011 9:10 am

hmm...

Newton allows to fuse more shapes to one rigid body, which is called compound collision.
A fixed joint is not included - it's possible to create one, but in most cases it's useless (except someone wants it breakable).
So maybe Leadwerks internally already used compound, but if you can interface all 3 bodies i think not.

If you have Leadwerks source you could take a look, function is NewtonCreateCompoundCollision.

You should discuss that with Leadwerks...
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

PreviousNext

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron