Original Post
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:
The plane is flying, but it gains speed on the flight upwards and start to lift more, because of the higher velocity.
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.