Public Group

# Help finding the opposite direction

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

## Recommended Posts

I'm making an Asteroids clone. I have the ship moving nicely, but I want to account for the ships inertia when changing directions. I currently have something like this...
void move_ship() {

starship.x0 = cos(starship.top) * starship.inertia
starship.y0 = sin(starship.top) * starship.inertia

if (starship.inertia > 0){
starship.inertia -= .05  // degredate inertia
}

//.. cliping code

}


If the player hits the Up arrow (thrusters), I increment starship.inertia by 1 each iteration through the game loop to show acceleration. When the ship changes direction, I flip the sign on starship.inertia to represent the force in the other direction, but it's not quite working. Am I approaching this the right way? How can I calculate the oposite direction of the ship? This is my first shot at any kind of physics and I would really appreciate the help. Thanks.

##### Share on other sites
I'm not sure what you mean by the opposite direction but I guess you want your ship to move with inertia acting on it.

It seems as if your code makes the ship always fly in the direction its facing. You need to seperate the ships orientation and its velocity.

Its orientation can just be stored as a plain angle. The velocity should be stored in a 2d vector (you can use an actual vector class or just do the calculations yourself). To slow the ship down you can multiply the vector by a scalar of 0.9 or something. To make the ship head off in a new direction when you use the thrusters, create a new vector using the cos and sin of the ships orientation (like you do at the moment) and just add it onto the original vector. The velocity of the ship should then simulate inertia.

##### Share on other sites
So something like this...
void move_ship() {int dx = cos(starship.direction);int dy = sin(starship.direction) ;starship.x0 = ( cos(starship.top) * starship.inertia ) +  dx ;starship.y0 = ( sin(starship.top) * starship.inertia ) + dy ;if (starship.inertia > 0){   starship.inertia -= .05  ;// degredate inertia   }   //.. cliping code}

##### Share on other sites
I don't think that would work as, you are still using an angle to hold your velocity without combining it with a speed.

In pseudo code heres what I was suggesting:
void move_ship() {//ships old velocityvector OldVelocity = GetVelocity();//get the thrust vector from the orientation and thrust speed.vector ThrustVector(cos(GetOrientation()) * GetThrustSpeed(), sin(GetOrientation()) * GetThrustSpeed());//add the two vectors togethervector NewVelocity = OldVelocity + ThrustVector;//decrease the speed of the shipNewVelocity *= 0.9;//sets the ships new velocitySetVelocity(NewVelocity);}

EDIT: On a second reading of your code it might work. Check it out.

##### Share on other sites
Oh, ok. I think I get the idea. I'll play around with it more. Thanks for the help.

##### Share on other sites
I think there's a conceptual problem here too, based on the way you named the variables; inertia is essentially the same thing as mass. Unless the spaceship's mass is being changed when its speed changes, you shouldn't be modifying its inertia when the speed changes.

##### Share on other sites
What I meant by inertia was the force that makes the ship continue forward. When the ship changes direction, I want to have the ships speed and direction be affected by the change. How would you put that in physics or geometry terms? I have very little background in those subjects.

##### Share on other sites
I believe the term you're looking for may be momentum, or impulse.

##### Share on other sites
So you simply want to have to accelerate in the opposite direction to that which you are currently travelling, in order to slow down, and eventually go in the other direction right?

stro has the right idea. Althought this line:
//decrease the speed of the shipNewVelocity *= 0.9;
will make the ship slow down on it's own very quickly, which is not very realistic for in-space simulation. However such slowing can be useful. I would leave it in but change the constant to something more like 0.995 (depending on your framerate of course)

My guess is that currently you accelerate upwards then when you head downwards you suddenly go fast downwards. This is because you are flipping the sign on the inertia. You need to instead seperate the force applied (acceleration) from the velocity, and keep applying (adding) the former to the later.

##### Share on other sites
Ok. I think I'm almost there. Here's what I've got...
// my polygon structtypedef struct POLYGON2D_TYP        {        int state;        // state of polygon        int num_verts;    // number of vertices        float x0,y0;        // position of center of polygon          float xv,yv;        // initial velocity		float dx, dy ;	  // new velocity		int top ;		  // 'top' of the polygon		int direction ;    // 		int old_direction ; //		float momentum ;	  //        DWORD color;      // could be index or PALETTENTRY        VERTEX2DF *vlist; // pointer to vertex list         } POLYGON2D, *POLYGON2D_PTR;int Game_Main(void *parms = NULL, int num_parms = 0){// .. other code// Apply thrust and allow for momentumif (KEYDOWN(VK_UP) ){	if (starship.direction != starship.top){		if (starship.momentum > 1) {			starship.dx = starship.xv ;			starship.dy = starship.yv ;			//starship.momentum *= -1 ;			starship.momentum = .1f ;		}	}		if (starship.momentum < MAX_MOMENTUM){		starship.momentum += 1;	}	starship.direction = starship.top ;	move_ship() ;}else if (starship.momentum > 0){	//continue moving on momentum only	move_ship() ;}// the rest of the loop}void move_ship(){		// apply the old velocity	starship.xv = (cos_look[starship.direction] * starship.momentum) + (starship.dx * .9) ;	starship.yv = (sin_look[starship.direction] * starship.momentum) + (starship.dy * .9) ;		// account for friction 	if (starship.momentum > 0 ){		starship.momentum -= .03f ;	}		// the old velocity	// decrement that over time until it is zero	// it gets reset when the ship changes direction	if (starship.dx > 0){		starship.dx -= .5 ;		starship.dy -= .5 ;	}	starship.x0 += starship.xv ;	starship.y0 += starship.yv ;	// handle clipping	if (starship.x0 > SCREEN_WIDTH + 10)		starship.x0 = -10 ;	else if (starship.x0 < -10)		starship.x0 = SCREEN_WIDTH + 10 ;	if (starship.y0 > SCREEN_HEIGHT + 10)		starship.y0 = -10 ;	else if (starship.y0 < -10)		starship.y0 = SCREEN_HEIGHT - 10 ;} // end move_ship

This works pretty good with only a few little quirks. Should I be using floats to store the data. I'm finding that the ship will drift when I would expect it to be still. That is, after accelerating, then accellerating in the opposite direction, I almost come to a stop, but it will drift in odd dirctions. Sto, I tried to apply your pseudo code. If this isn't what you meant, could I trouble you to show a more specific example?

Regarding vectors, as you can see I'm not using them, mainly because I never have before (one disaster at a time). Am I correct in understanding that a vector would just contain (starship.x0, starship.y0)? Could somebody give an example of what this might look like using vectors? Thanks for the help.

[Edited by - vinb on January 5, 2005 8:22:37 PM]

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632983
• Total Posts
3009714
• ### Who's Online (See full list)

There are no registered users currently online

×