# 2D Flight Simulator

This topic is 1359 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to make a fun simple 2d plane simulation game in Unity(2D).

I tried several approaches but each time i end up having unrealistic flights.

I would like something like this : http://runway.countlessprojects.com/prototype/index.html

Can someone point me some starting points ? I want to use physics so later I can add wind and other fun stuff

A while ago someone posted this code in the forums; don't know if that can be a starting point to convert to forces

Posted 03 February 2006 - 01:24 PMHmm, something like this?
========================================
//Gravitation
speed.Y -= gravitation*elapsedTime;

//Air-resistance
totalSpeed = (float)Math.Sqrt(speed.X * speed.X + speed.Y * speed.Y);
speedAngle = (float)Math.Atan2(speed.Y, speed.X);
speed.X = (float)Math.Cos(speedAngle) * totalHastighet * 0.999f;
speed.Y = (float)Math.Sin(speedAngle) * totalHastighet * 0.999f;

//Engine
speed.X += (float)Math.Cos(planeAngle) * acceleration*elapsedTime;
speed.Y += (float)Math.Sin(planeAngle) * acceleration*elapsedTime;

//Liftforce
totalSpeed = (float)Math.Sqrt(speed.X * speed.X + speed.Y * speed.Y);
speed.X += (float)Math.Cos(planeAngle + Math.PI/2) * totalSpeed * totalSpeed *elapsedTime /weight;
speed.Y += (float)Math.Sin(planeAngle + Math.PI/2) * totalSpeed * totalSpeed *elapsedTime /weight;

//Move
position.X += speed.X * elapsedTime;
position.Y += speed.Y * elapsedTime;



##### Share on other sites

i believe thats not how this game works, i see there basic lift, drag equations (along with thrust and weight forces) they produce linear acceleration and angular acceleration which you will need to calculate.

maybe this code will give you some light:

	double l;
timin->stopTimer();

l = timin->getElapsedTime();

double dt = double(l);     //get elapsed time in seconds
timin->startTimer();
if (dt <=0 ) return;
lastDeltaTime = dt;
dt = 0.0330;

double V = VectorLength(vel);
double squareSpeed = V*V;

t3dpoint<double> pitch_vec = -t3dpoint<double>(YPRangle.AIR_MATRIX[8], YPRangle.AIR_MATRIX[9], YPRangle.AIR_MATRIX[10]);

ThrustForce_vec = pitch_vec * Throttle_to_force( Throttle );

//double AngleOfAttack =	0.0;//			AngleBetweenVectors(-vel,ThrustForce_vec);

result_force = t3dpoint<double>(0.0, 0.0, 0.0);
angularity = t3dpoint<double>(0.0, 0.0, 0.0);
int i;
for (i=0; i<elcnt-1; i++)
{
t3dpoint<double> ELEMENT_FORCE = t3dpoint<double>(0.0, 0.0, 0.0);
double DynPressure = (0.5*squareSpeed*dens*ELEMENTS[i]->area);

double r 	= VectorLength(ELEMENTS[i]->center_of_pressure);
double EMOI = (ELEMENTS[i]->mass*r*r);

double defk = 1.0 * absnf(ELEMENTS[i]->GetAngle());
t3dpoint<double> EDragForce =                                  //flat square at 90 = 1.1  => 17.5 deg * 5.14
ELEMENTS[i]->ReturnDeflectionNormal(YPRangle.AIR_MATRIX) * defk * DynPressure;

t3dpoint<double> ELiftForce = ELEMENTS[i]->ReturnDeflectionNormal(YPRangle.AIR_MATRIX) * LiftCoefficient(AOA,0) * DynPressure;

t3dpoint<double> EGravityForce = Normalize(-pos)*eGForce*ELEMENTS[i]->mass;

ELEMENT_FORCE = EDragForce + ELiftForce + EGravityForce;

double kamikadze = VectorLength(ELiftForce) - VectorLength(EGravityForce);

Matrix44<double> BASE_ROT_MAT;
t4dpoint<double> rotcoord = BASE_ROT_MAT * ELEMENTS[i]->center_of_pressure;
t3dpoint<double> ELEMENT_R = t3dpoint<double>(rotcoord.x, rotcoord.y, rotcoord.z);

t3dpoint<double> ETorque = ELEMENT_R * ELEMENT_FORCE;      // R x F

t3dpoint<double> EAngAcc = ETorque / EMOI;

t3dpoint<double> EAngVel = EAngAcc*dt;
t3dpoint<double> EAngMov = EAngVel*dt;

angularity = angularity + EAngMov;
result_force = result_force + ELEMENT_FORCE;
}

//fuselage has different approach because we subtract fuselage drag length from overall lift value

result_force = result_force + Normalize(-vel) * (0.5*0.270*squareSpeed*dens*FUSELAGE->area) +
ThrustForce_vec + Normalize(-pos)*eGForce*FUSELAGE->mass;

YPRangle.pitch(cos(angularity.x*imopi),sin(angularity.x*imopi));
YPRangle.DoRotation();

YPRangle.yaw(cos(angularity.y*imopi),sin(angularity.y*imopi));
YPRangle.DoRotation();

YPRangle.roll(cos(angularity.z*imopi),sin(angularity.z*imopi));
YPRangle.DoRotation();

//add skin friction drag  skin friction coeff for f-18 maybe 0.0031

Acceleration = result_force / mass;
vel = vel + (Acceleration * dt);
pos = pos + (vel * dt);

Edited by WiredCat

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633269
• Total Posts
3011152
• ### Who's Online (See full list)

There are no registered users currently online

×