Archived

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

Math question about asteriods physics

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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...

Share this post


Link to post
Share on other sites
I personally use the KeyUp and KeyDown messages to set a set of flags for rotation, thrust, shooting, etc. In other words, when the thrust key is pressed, a flag like is_thrusting is set to true. When the key is released the flag is set to false. Now, in the game loop you just check flag. If it''s true, you add speed to the ship. If it is false, you don''t do anything, or, if you want the ship to slow, you substract speed from the ship.

Share this post


Link to post
Share on other sites
To limit the speed of my asteroids-style spaceship, and to slow it down when not thrusting, I implemented the idea of "friction" in space. Try the following method:

  • Assign your spaceship a "friction" value to reflect how aerodynamic it is.

  • Store your ships''s velocity as a vector.


  • Then, for every frame:

  • If thrusting, calculate the new velocity vector using sin/cos, as in previous messages.

  • Whether thrusting or not, calculate a "friction vector" by taking the opposite of the velocity vector (swap the signs of the X and Y component) and multiplying this vector by the ship''s "friction" constant.

  • Add this friction vector to the velocity vector.

  • Update the ship''s position by adding the velocity vector.


  • This method may sound a bit complicated but it is not too hard to implement, especially if you have a vector class defined. It effectively introduces a force representing friction, acting in the oppostite direction to the ship''s direction of travel, which will slow the ship when the player is not thrusting and will limit the ship''s maximum speed, depending on the values you assign to the ship''s thrust and friction. You can tweak these values until the ship behaves just the way you like, or alter them in-game to simulate different environments or represent "speed boost powerups" for example.

    Hope this helps,

    Moot

    Share this post


    Link to post
    Share on other sites
    And another thing, try and ensure that your calculations are all time-dependent, ie they are calculated according to how much time has elapsed since the last frame. This will ensure that your objects will travel at the same speed no matter how fast or slow the pc on which your game is running.

    For more details search GameDev for phrases like "QueryPerformanceCounter", "TimeGetTime" and "GetTickCount".

    Moot

    Share this post


    Link to post
    Share on other sites