Skip to main content
GameDev.net gamedev.net
🔒 Locked

(C++) Replicating the Physics of Space

Started by Side Winder Jan 23, 2007 at 5:07 AM 6 replies 800+ views
Original Post
Side Winder
Side Winder
I'm making an Asteroids type game and obviously if you thrust in a direction in space, your ship will continue on that course until you thrust elsewhere. How do I do this in C++? Also, so far I've got the ship to move in a very basic motion, i.e. when the cursor is pressed down the ship moves very jerky. It seems to move 1 pixel at a time I guess? And currently the ship cannot move in two directions at a time, so if I press the up cursor and left cursor it'll continue on its up direction. Thanks for any help.
RobN
RobN
You need to control the ship using a velocityx & y variables.So when you draw its new position its: currentposistionx + velocityx and currentpositiony + velocityy.

So to move the ship rigt you set the x velocity to somthing like 40 and it will continiously move it 40 across every frame(giving you a sliding effect) to slow it down you decrease the velocity or for a complete stop set it to 0.
E-Coder
E-Coder
Answer some of your questions :):

I think you are handling your keys something like this:
if(up)    handle_up();else if(right)    handle_right();... etcetera

^ or a switch-statement...

But if someone press the up-button and the right-button and holds it. It will only handle up and not the right key, to fix this; just remove the else-clause.

if(up)    handle_up();if(right)    handle_right();... etcetera
Side Winder
Side Winder
Surely a switch case would work? Or should I *just* use if statements?
arithma
arithma
Quote:
Original post by Side Winder
I'm making an Asteroids type game and obviously if you thrust in a direction in space, your ship will continue on that course until you thrust elsewhere. How do I do this in C++? Also, so far I've got the ship to move in a very basic motion, i.e. when the cursor is pressed down the ship moves very jerky. It seems to move 1 pixel at a time I guess? And currently the ship cannot move in two directions at a time, so if I press the up cursor and left cursor it'll continue on its up direction.

Thanks for any help.


Do you still need the smooth movement things or did you get them right?
Side Winder
Side Winder
I can't get either working. I did the 'if' statements instead of using switch but that doesn't work. And I'm not sure how to apply the velocity thing... Hmmm.. Tricky stuff, this!
templewulf
templewulf
The reason each condition needs to be tested individually is because each key needs to be detected separately. Early-out mechanisms like else clauses or break statements in switches mean you get the first condition (up key), and ignore the rest.

Perhaps posting some code may help.

As for velocity, define a constant acceleration for your ship. Then, determine the direction the ship should go, in Asteroids this is an attribute that exists independent of input. In your game, it sounds like direction of acceleration is based on the currently-held keys.

The reason you should determine direction and acceleration separately is that if you code up to add velocityY += 10 and left to add velocityX -= 10, then you end up traveling faster in diagonals than in cardinal directions.

Once you have your direction determined, adding velocity is simple:

void Ship::Accelerate(){	//determine orientation	//change velocities	this->velocityX += cos(this->direction) * this->acceleration;	this->velocityY += sin(this->direction) * this->acceleration;}void Ship::Update(){	this->positionX += this->velocityX;	this->positionY += this->velocityY;}
XBox 360 gamertag: templewulf feel free to add me!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.