360 Degree Spaceship slide question

YetAnotherSuperhero
Unregistered
 
Post: #1
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
Quote this message in a reply
Member
Posts: 100
Joined: 2006.05
Post: #2
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.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #3
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.
Quote this message in a reply
YetAnotherSuperhero
Unregistered
 
Post: #4
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
Quote this message in a reply
Member
Posts: 100
Joined: 2006.05
Post: #5
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);
    }
}
Quote this message in a reply
YetAnotherSuperhero
Unregistered
 
Post: #6
Yep! That got it! thank you so much!!!!


LOL
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #7
Just as an aside, the concept you were looking for is inertia.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Anyone who has a degree in computer programming please help me!!! sg225746 3 4,685 Sep 19, 2012 11:30 AM
Last Post: blot-blaqksheep