• Advertisement
Sign in to follow this  

Help finding the opposite direction

This topic is 4796 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 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 this post


Link to post
Share on other sites
Advertisement
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 this post


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


Link to post
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 velocity
vector OldVelocity = GetVelocity();
//get the thrust vector from the orientation and thrust speed.
vector ThrustVector(cos(GetOrientation()) * GetThrustSpeed(), sin(GetOrientation()) * GetThrustSpeed());
//add the two vectors together
vector NewVelocity = OldVelocity + ThrustVector;

//decrease the speed of the ship
NewVelocity *= 0.9;

//sets the ships new velocity
SetVelocity(NewVelocity);
}




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

Share this post


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

Share this post


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


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


Link to post
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 ship
NewVelocity *= 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 this post


Link to post
Share on other sites
Ok. I think I'm almost there. Here's what I've got...


// my polygon struct
typedef 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 momentum
if (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]

Share this post


Link to post
Share on other sites
Quote:

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.


This is quite normal for this type of system, and nicely models what would happen on an actual spacecraft using thrusters to attempt to stop. If you want to stop the ship completely, you'll probably need to:
a) create some kind of "brake" control, that stops the ship on the spot (or to be more realistic reduced momentum until the ship is stationary), or
b) check if the momentum of the ship at any time gets below a particular value (which should be quite low - you'd have to experiment for a good one), and stop the ship from moving if it is.

Without one of those systems in place (they both work in nearly the same way btw, but one is easier for the player to use), players will find it near impossible to reverse thier thrust in such a way as to come to an absolute stop without drifting slightly.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kazgoroth
Without one of those systems in place (they both work in nearly the same way btw, but one is easier for the player to use), players will find it near impossible to reverse thier thrust in such a way as to come to an absolute stop without drifting slightly.


That's cool. I think that's how the original game played, anyway. Thanks for the suggestions. I think I'll try for checking the momentum of the ship. We don't want to make this too easy on the player ;)
Thanks for the advise.

Share this post


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

  • Advertisement