# Flying in space

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

## Recommended Posts

Hello. I'm creating a 2d space flying game and need little help with physics. Nothing fancy, just little tiny bits of reality here and there. My first question is to fulfill my knowlege of space physics: what should I consider? What are the must-haves of space flying? At least gravitation with large planets and big objects generally. And my second question is about directions. The space ship can rotate 360 degrees, 2pi in radians. So how should I calculate the resultant angle, if I have two components already? I have some idea of it, but for example angles that are over 90 degrees apart don't go with my plannings. If my angle_1 is 40 degrees and angle_2 is 60 degrees, it's fairly easy to calculate the resultant: (40 + 60) / 2. But this makes very unrealistic resultants if the delta of the angles if over 90. For example if you have opposite directions, 90 and 270, the resultant would be 180, which isn't correct. So I need some advice with my formulas and calculations but mostly with the moving part. I took some space / gravitational physics courses in my school so the gravitation is quite easy for me to understand in low-level physics programming, and that's what I want. Thanks already! I appreciate all the help and support I get. edit. some adds to the direction problem. With two angles I meant that if you fly to direction 44 (degrees), turn your ship to face direction 79 (degrees), without throttling, while still going to direction 44 (there's no force to move you even if you turn your ship) and throttle again. Then the force moving you to direction 44 will "battle" against the acceleration force moving you to the new direction. Thus there can't be three components at the same time. [Edited by - nyrhinen on October 25, 2007 4:29:44 PM]

##### Share on other sites
Quote:
 Original post by nyrhinenedit. some adds to the direction problem.With two angles I meant that if you fly to direction 44 (degrees), turn your ship to face direction 79 (degrees), without throttling, while still going to direction 44 (there's no force to move you even if you turn your ship) and throttle again. Then the force moving you to direction 44 will "battle" against the acceleration force moving you to the new direction. Thus there can't be three components at the same time.

The first "force" is your inertia. The second force is the acceleration. First off, you should work with vectors, not angles. Then, just apply Newton's laws.

For example, your ship moves at 40m/s in direction v. Then you apply a force of F newtons in direction n for t seconds. Your ships has a mass M.

We know by Newton's law that F=MA, so A = F / M

The resulting velocity will be 40*v + F/M*n*t.

##### Share on other sites
Quote:
 Original post by SteadtlerFirst off, you should work with vectors, not angles. Then, just apply Newton's laws.

I know what vectors are but I simply can't get the idea behind them in this problem. This far I've used only simple angular calculations in movement. Like this:
/* The game is written in C and uses SDL with it. * A piece of my ways of movement this far.**/if (button[SDLK_UP]) {    x += cos_table[angle] * speed;    y += sin_table[angle] * speed;}

Angle can be from 0 to 359 (because 0 and 360 are the same thing). The player can change angles by pressing left or right. By using the code above, the ship moves into the right direction but when the idea behind space travelling comes along, it can't be used like that.

I drew a small picture to clarify the vision I have in my mind:
http://koti.mbnet.fi/nyrhinen/space_flying.png

##### Share on other sites
I'm curious - how is one supposed to "use vectors", without splitting up the vectors in to components?

##### Share on other sites
Consider your space ship class to be something like (requires a custom 2d Vector class, but that's something you should have)

class SpaceShip{public:   Vector2d Position, Velocity, Acceleration;   float Direction;  //This could be also a 2d vector and you could use 2x2 matrix to manipulate it.   void Update()   {      Position += Velocity;      Velocity += Acceleration;      //set Acceleration to 0,0 or dampen it somehow.          }      void ProcessInput(int Key)   {      if(Key == UP)Acceleration += Vector2d(cos(Direction),sin(Direction));      if(Key == LEFT)Direction += 0.01; //or something      ...etc   }}

I know that the physics presented here isn't really correct, but it just gives you the idea of how your ship is moving in the space. Instead of modifying your position or velocity, you'll just modify the acceleration (that is, apply some sort of thrust to your ship).

This also handles correctly the situation you present in your drawing.

So, in order to make your ship to stop, you'll need to apply a force opposite of your current velocity. Or, if you want your ship to move only to the direction it is heading, you'll have to apply a certain force which will eliminate moment in any other direction.

Best regards!

##### Share on other sites
Man, now I'm confused! :-)
I realised how off I am in this whole matter. Firstly I don't have a custom 2d vector class and secondly this is way harder than I expected!

I'll try to understand vectors and flying with your help this far, but feel free to add something to help me and if you know something else about space physics, please inform me.

##### Share on other sites
Quote:
 Original post by nyrhinenMan, now I'm confused! :-)I realised how off I am in this whole matter. Firstly I don't have a custom 2d vector class and secondly this is way harder than I expected!I'll try to understand vectors and flying with your help this far, but feel free to add something to help me and if you know something else about space physics, please inform me.

Oh, don't let the words such as "vector" to scare you. The vector is just a class that just wraps x,y coordinates so that you don't always have to write something like

positionx += something;
positiony += something;

just

position += something;

This will make the code much cleaner and will produce less errors. Honestly.

The class I wrote earlier had few variables inside it:

- Position, location of your ship in x,y space (nothing new here)
- Velocity, a vector which tells how much your ship moves on every update
- Acceleration, a vector telling how much your velocity changes on every update. (that is, the thrust of your space ship engine).
- Direction (angle), as I wrote, this could also be a vector or a matrix (which I would use if I was writing a game like this).

you should find plenty of tutorials for how to make your own vector class. For any language you are using.

//Here is a quickly copied Vector2d class with a very few member function, should be enough for the code I wrote earlier//I assume that you are using C++, maybe this is a bad assumptionClass Vector2d{public:   float x,y;	Vector2d operator-(const Vector2d &a) const 	{		return Vector2d(x-a.x,y-a.y);	}	Vector2d operator+(const Vector2d &a) const 	{		return Vector2d(x+a.x,y+a.y);	}	Vector2d& operator+=(const Vector2d &a)	{		x+=a.x;		y+=a.y;		return *this;	}	Vector2d& operator-=(const  Vector2d &a)	{		x-=a.x;		y-=a.y;		return *this;	}}

##### Share on other sites
Hehe, I'm back again. First I want to say that I'm too lazy to make a 2d vector class, even if it's very easy, so I wrote my own solution today at school. I haven't tested it yet, but it looks pretty nice to me. Feel free to express your opinions, optimization tricks or what ever.

Here it goes:
I'll create an array with as many elements as there are possible directions divided by two. For example, in my game the ship rotates only two degrees at once, so I'll need 180 / 2 = 90 different elements.
Every time the ship throttles, you'll have to check the value of the angle. If it's over 179, you'll subtract 180 from it and divide it by two. Angle: 182, element: (182 - 180) / 2 = 1. And because the angle is over 179, element one is subtracted with the acceleration. If the angle is under 180, you'll just divide it by two and increase the element with acceleration. Angle: 80, element: 80 / 2 = 40.

Then the game has a calculation loop somewhere. The loop goes through all the elements in the directional array while calculating values to d_x (delta x) and d_y (delta y). d_x and d_y will be added to the ship's coordinates.

There might be some flaws, 'cos it's very fresh idea and I haven't done the "debugging" yet. :)

edit.
x = cos(i) * directionsy = sin(i) * directions    ^ in degrees, angle * 3.14159 / 180.