• Advertisement
Sign in to follow this  

2D Flight Simulator

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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);
//tmpangle = 180.0-RadToDeg(AngleOfAttack);





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;

double AOA = ELEMENTS[i]->GetAngle()*17.5;//RadToDeg(AngleBetweenVectors(ELEMENTS[i]->ReturnDeflectionFront(YPRangle.AIR_MATRIX), Normalize(vel)));

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;
BASE_ROT_MAT.LoadGLMatrix(YPRangle.AIR_MATRIX);
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement