Advertisement

Math question about asteriods physics

Started by April 24, 2001 10:21 AM
11 comments, last by Lethian 23 years, 9 months ago
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?
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.
<span class="smallfont">That is not dead which can eternal lieAnd with strange aeons even death may die.   -- "The Nameless City" - H. P. Lovecraft</span>
Advertisement
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
Momentum.y+=factor*sin(rad);//momentum in accelleration
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.
As humans we are all gods, just to different extents of power.
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.
<span class="smallfont">That is not dead which can eternal lieAnd with strange aeons even death may die.   -- "The Nameless City" - H. P. Lovecraft</span>
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?
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
<span class="smallfont">That is not dead which can eternal lieAnd with strange aeons even death may die.   -- "The Nameless City" - H. P. Lovecraft</span>
Advertisement
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
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.
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??
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...

This topic is closed to new replies.

Advertisement