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