2D airplane physics calculations

Started by
5 comments, last by Wolv3r 11 years, 11 months ago
Hello,

I'm working on a 2D airplane game in AS3. But the main problem is the physic on my plane.

Right now I have this:


Class Plane {

//----Physic--
//I know, airdensity is wrong
public var airdensity:Number = 1.4;

public var trustP:Point = new Point(0,0);

public var rocketboostP:Point = new Point(0,0);
public var rockettrust:Number = 0;

public var accelerationP:Point = new Point(0,0);

public var velocityP:Point = new Point(0,0);
static public var velocity:Number = 0;

public var dragP:Point = new Point(0,0);
public var liftP:Point = new Point(0,0);
public var gravityP:Point = new Point(0,0);
public var weight:Number = 0.2;

private function Update(e:Event) {
CalcAirDensity();
CalcRocketBoost();
CalcBugBoost();
CalcDrag();

liftP.y += CalcLift();
gravityP.y += CalcGravityY();
CalcVelocity();
}


private function CalcAirDensity() {

//I know, it's wrong calculated, maybe someone can me help here too
airdensity = 1.4;
airdensity -= Math.abs(this.y) / 100000;
if(airdensity < 0) airdensity = 0;
}

private function CalcRocketBoost() {
if(rocketBoost && !grounded && Flight.started && fuel > 0) {
rockettrust += rocketAccel;
fuel -= 0.2;
}
rocketboostP.x = Math.cos(radians) * rockettrust;
rocketboostP.y = Math.sin(radians) * rockettrust;
}

private function CalcDrag() {
if(velocity <= 0) {
var speed = Math.sqrt(accelerationP.x * accelerationP.x + accelerationP.y * accelerationP.y);
dragP.x = aerodynamic * airdensity * accelerationP.x * accelerationP.x * 0.02 / 2;
dragP.y = aerodynamic * airdensity * accelerationP.y * accelerationP.y * 0.06 / 2;
} else {
dragP.x = aerodynamic * airdensity * velocityP.x * velocityP.x * 0.02 / 2;
dragP.y = aerodynamic * airdensity * velocityP.y * velocityP.y * 0.06 / 2;
}
}

private function CalcLift():Number {
var lift:Number = 0;
var angleofattack = this.rotation;
if(angleofattack < 0) angleofattack = Math.abs(angleofattack);
else if(angleofattack > 0) angleofattack = -Math.abs(angleofattack);

var rad = angleofattack * Math.PI / 180;
var liftcoefficient:Number = 2 * Math.PI * rad;

if(velocity <= 0) {
velocityP.x = accelerationP.x - dragP.x;
velocityP.y = accelerationP.y - dragP.y;

velocity = Math.sqrt(velocityP.x * velocityP.x + velocityP.y * velocityP.y);
}

lift = 0.5 * airdensity * ((velocity / 10) * (velocity / 10)) * 0.01 * liftcoefficient;

return lift;
}

private function CalcGravityY():Number {
var gravity:Number = 0;
gravity = this.weight * 9.81 / stage.frameRate;
return gravity;
}

private function CalcVelocity() {
velocityP.x = (trustP.x + rocketboostP.x - dragP.x + gravityP.x) / 2;
velocityP.y = (trustP.y + rocketboostP.y - dragP.y + gravityP.y) / 2;
}
}


The plane is flying, but it gains speed on the flight upwards and start to lift more, because of the higher velocity.
Advertisement
My guess is that you're adding lift but not subtracting from the forward velocity. In unpowered flight I imagine that lift would always slow the vehicle. Then when it slows too much it would tend to fall more, which would increase forward speed, and therefore increase lift again for a while. Keep conservation of momentum and energy in mind.
My updated CalcVelocity method looks like this:


private function CalcVelocity() {
velocityP.x = (trustP.x + rocketboostP.x - dragP.x) / 2;
velocityP.y = (trustP.y + rocketboostP.y - dragP.y + gravityP.y - liftP.y) / 2;
}


So yes, I don't substract from velocity X. How do I calculate the substracting lift or is it the same?
Right now I only add my lift from CalcLift() to liftP.y.
I don't know what the correct physics answer is here. I suggest:
- Further research.
- Alter the x so that you maintain overall speed but only change direction.
- Conserve energy, e.g. total energy input = initial potential energy + initial throw + total rocket boost used (my paper planes don't have that!), the system can never exceed that. Drag will of course remove energy from the system (or transfer it to theoretical air that you don't simulate).
- Try some options and see what looks best.
I must admit that I haven't inspected your code very thoroughly. However, the physics of a plane is made of the "four forces":

- gravity
- lift
- drag
- engine power (this one may be absent in gliders)

Gravity is trivial to compute (it's a fixed amount always directed downward in the earth reference frame).

Lift and Drag are very similar. They both depends upon speed (squared) and other factors (air density and other constants depending upon the plane's geometry); they must be calculated using the absolute speed of the plane, and then applied in the right direction: drag must be applied in the opposite direction of speed, lift must be applied toward the plane "top", in the plane reference frame (NOT in the earth absolute reference frame!).

Engine power is applied toward the plane "front", again in the plane reference frame.

If you're doing everything correctly and the plane is still continuously accelerating, you're probably using an unrealistic lift/drag ratio, ie you're generating too much lift and too few drag so that you're violating energy conservation. In that case, simply try to reduce the lift coefficient (and/or increase the drag one) until the system becomes "stable"...

Lift and Drag are very similar. They both depends upon speed (squared) and other factors (air density and other constants depending upon the plane's geometry); they must be calculated using the absolute speed of the plane, and then applied in the right direction: drag must be applied in the opposite direction of speed, lift must be applied toward the plane "top", in the plane reference frame (NOT in the earth absolute reference frame!).


Yes. In addition, if you're just representing your plane with a single lift/drag calculation, you probably also want to include a "pitching moment" term. This will be proportional to the plane's forward speed, and tend to make the nose pitch up (basically apply a torque proportional to the forwards (not horizontal) speed).

If you want to allow your aircraft to stall (i.e. pitch down when its forward speed becomes too small), you'll have to add a term for that too.

Alternatively, you could represent the wings and tail with separate lift/drag calculations. That will give you a better stall response too.
Right now I updated my LiftCalculation to calucalate the X and Y velocity:


private function CalcLift() {
var lift:Number = 0;
var angleofattack = this.rotation;
if(angleofattack < 0) angleofattack = Math.abs(angleofattack);
else if(angleofattack > 0) angleofattack = -Math.abs(angleofattack);

var rad = angleofattack * Math.PI / 180;
var liftcoefficient:Number = 2 * Math.PI * rad;

if(velocity <= 0) {
velocityP.x = accelerationP.x - dragP.x;
velocityP.y = accelerationP.y - dragP.y;
}


liftP.y += 0.5 * airdensity * ((velocityP.y / 10) * (velocityP.y / 10)) * 0.01 * liftcoefficient;
liftP.x += 0.5 * airdensity * ((velocityP.x / 10) * (velocityP.x / 10)) * 0.01 * liftcoefficient;;
}


This works realy good, but something feels not right when the plane is flying. Maybe I need to play around with the airdensity and drag.
But what is the pitching moment? I read it on wikipedia but I don't understand it.

This topic is closed to new replies.

Advertisement