iDevGames Forums
360 Degree Spaceship slide question - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: 360 Degree Spaceship slide question (/thread-3026.html)



360 Degree Spaceship slide question - YetAnotherSuperhero - Sep 19, 2007 01:21 PM

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


360 Degree Spaceship slide question - Nevada - Sep 19, 2007 02:19 PM

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:

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.


360 Degree Spaceship slide question - ThemsAllTook - Sep 19, 2007 02:19 PM

Implement velocity. Example:

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.


360 Degree Spaceship slide question - YetAnotherSuperhero - Sep 19, 2007 02:33 PM

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


360 Degree Spaceship slide question - Nevada - Sep 19, 2007 02:57 PM

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);
    }
}



360 Degree Spaceship slide question - YetAnotherSuperhero - Sep 19, 2007 09:58 PM

Yep! That got it! thank you so much!!!!


LOL


360 Degree Spaceship slide question - DoG - Sep 21, 2007 02:17 AM

Just as an aside, the concept you were looking for is inertia.