## 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
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.
Moderator
Posts: 1,563
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.
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
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);     } }```
YetAnotherSuperhero
Unregistered

Post: #6
Yep! That got it! thank you so much!!!!

Moderator
Posts: 869
Joined: 2003.01
Post: #7
Just as an aside, the concept you were looking for is inertia.