#### Archived

This topic is now archived and is closed to further replies.

# Math question about asteriods physics

This topic is 6291 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to rewrite asteriods as my first attempt at writting a game. I have question about the physics. I can move the ship foward, rotate it and move it more. But currently I cant figure out how to give the ship momentum. Meaning the ship should travel in the direction he was going for a short period of time after he has changed directions. Can anybody help me out?

##### Share on other sites
Your question is a bit vague about your ship''s behavior, but assuming the classic Asteroids behavior I would do the following. I would give the ship a velocity vector. This vector would describe both the speed and direction that the ship is drifting, not the direction it is facing though. Now, when you press the "thrust" key, you add, to the velocity, a certain amount of speed in the direction the ship is facing. This in effect means that the ship will continue travelling in the same direction until enough thrust is applied to alter the direction of travel.

##### Share on other sites
Just to put the idea that Microdot said into basic math and code.
And a few ideas of my own, that I''ve used successfully before.

To make this simple lets put into 2d.

typedef struct{
float X,Y;
} Axis2; //2d structure, helps organization

Axis2 Accel(Axis2 Momentum, float rad, float factor)
{
Momentum.x+=factor*cos(rad);//maybe factor in weight for better
return Momentum;
}

//somewhere in the event loop
if user pushes thrust{
Userobject.Momentum= Accel(Userobject.Momentum,
Userobject.Direction,
Magnitude);
//Other stuff that goes with accerating like fuel levels.
}

Of course their are thousands of variations on this theme, I personally Perfer overloading the operators for the structs to allow for +-*/, and put this kind of function as a member of the struct, but this manor will surfice.

##### Share on other sites
Also, if you can find it, there''s a pretty good freeware Asteroids clone called Hyperoid. It was written in Win32 and comes with the source code.

##### Share on other sites
Thanks both, microdot Sulgurth. Sulgurth that seems to be working fairly well but now the thing never slows downs in fact it keeps getting faster and faster. I dont know if im updating the X and Y position of the ship correctly

xPos = xPos + momentum.x;

if (xPos > WINDOW_WIDTH)
xPos = xPos - WINDOW_WIDTH;

if (xPos < 0)
xPos = xPos + WINDOW_WIDTH;

yPos = yPos + momentum.y;

if (yPos < 0)
yPos = yPos + WINDOW_HEIGHT;

if (yPos > WINDOW_HEIGHT)
yPos = yPos - WINDOW_HEIGHT;

Any suggestions?

##### Share on other sites
I've run into this kind of problem before. How does the code for responding to the trust key look. It could be that when you press the thrust key you put the ship into perpetual acceleration. I've done this before.

Edited by - microdot on April 24, 2001 1:14:03 PM

##### Share on other sites
That doesn''t look incorrect, I mean it shouldn''t increase the speed of your ship. It will however, keep it at the same speed as when you let go of the thrust key.

Are you increasing the momentum repeatedly or something?

-Mezz

##### Share on other sites
If you want the ship to slow down you just need to decrease (or increase if negative) that momentum value towards zero every frame that the player isn''t pressing a thrust key.

##### Share on other sites
currently im usung the windows message queue to manage my input. How do i trigger an event off of a user not doing something.

if (no key down)
decrease momentum;

How do i trigger the decrease??

##### Share on other sites
quote:
Original post by Lethian

currently im usung the windows message queue to manage my input. How do i trigger an event off of a user not doing something.

if (no key down)
decrease momentum;

How do i trigger the decrease??

You don''t !

Do it in your main loop or something...

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631706
• Total Posts
3001833
×