360 Degree Spaceship slide question
Hey, I'm new here.
But this looks like a good board.
My questions is: I have a space ship movement using:
vx = v * cos(angle)
vy = v * sin(angle)
How can I make it that when I change the angle the ship will continue to slide in the direction it was.
I hope I made that clear.
Thanks for any help.
YAS
But this looks like a good board.
My questions is: I have a space ship movement using:
vx = v * cos(angle)
vy = v * sin(angle)
How can I make it that when I change the angle the ship will continue to slide in the direction it was.
I hope I made that clear.
Thanks for any help.
YAS
You'll have to make some modifications to how you control movement. Rather than setting the velocity when the user presses a key, you will want to accelerate it. The psuedo-code will go something like this:
I'm assuming you have a time based physics system (that way the game physics is the same across all machines) so the delta_t term is the time elapsed since the last frame. If you don't use this yet, just leave 'em out (Though you will have to be careful what values you choose for DRAG and MASS). You could also make the drag effect second order (more dramatic and more accurate at high velocities) by multiplying the length of the velocity vector into the into the DRAG/MASS*delta_T part.
Code:
// When player presses key
boostPlayer(float angle, float delta_t)
{
if(abs(player.vx) < MAX_SPEED)
player.vx += BOOST*delta_t*cos(angle);
if(abs(player.xy) < MAX_SPEED)
player.vy += BOOST*delta_t*sin(angle);
}
// Called every frame
applyPhysics(delta_t)
{
player.x += player.vx*delta_t;
player.y += player.vy*delta_t;
// This will add air drag if you want the ship to slow down when drifting
player.vx *= (1 - DRAG/MASS*delta_t); // DRAG >= 0
player.vx *= (1 - DRAG/MASS*delta_t); // MASS > 0
}
I'm assuming you have a time based physics system (that way the game physics is the same across all machines) so the delta_t term is the time elapsed since the last frame. If you don't use this yet, just leave 'em out (Though you will have to be careful what values you choose for DRAG and MASS). You could also make the drag effect second order (more dramatic and more accurate at high velocities) by multiplying the length of the velocity vector into the into the DRAG/MASS*delta_T part.
Implement velocity. Example:
Depending on how you want your game mechanics to work, you may want to damp velocity a bit each frame (though if this takes place in space, probably not).
Edit: Nevada beat me to it.
Code:
float velocityX, velocityY;
float positionX, positionY;
...
if (movingForward) {
velocityX += ACCELERATION_SPEED * cos(angle);
velocityY += ACCELERATION_SPEED * sin(angle);
}
positionX += velocityX;
positionY += velocityY;
Depending on how you want your game mechanics to work, you may want to damp velocity a bit each frame (though if this takes place in space, probably not).
Edit: Nevada beat me to it.
I think I get it, BUT, and excuse my ignorance, but if player.vx is maxed out, how do I keep player.vy from increasing and changing the angle that the player moves?
Thanks again
Thanks again
I'm sorry, that was my mistake. The code should be
Code:
boostPlayer(float angle, float delta_t)
{
if((player.vx*player.vx + player.vy*player.vy) < MAX_SPEED*MAX_SPEED)
{
player.vx += BOOST*delta_t*cos(angle);
player.vy += BOOST*delta_t*sin(angle);
}
}
Possibly Related Threads...
Thread: | Author | Replies: | Views: | Last Post | |
Anyone who has a degree in computer programming please help me!!! | sg225746 | 3 | 5,840 |
Sep 19, 2012 11:30 AM Last Post: blot-blaqksheep |