I'm trying to write something I believe is straightforward but wanted to check that I'm doing it right.

All I want is to update the position of a 2D sprite based an acceleration and velocity. I'm using time based modelling so I have time deltas at my disposal. My current approach looks a little like this:

int rotDuration = btnRight.GetDuration() - btnLeft.GetDuration();  
ship.Facing += (ANGULAR_VELOCITY * (rotDuration)); //angular displacement (rads?)

Rise::Vector2D forwardDir(sin(ship.Facing), cos(ship.Facing));  
Rise::Vector2D finalVelocity = ship.Velocity + (elapsedTime * btnAcc.GetDuration() * MAX_ACC * forwardDir);
ship.Position += finalVelocity;  

Hopefully that's pretty straightforward. I get the duration that rotation was occurring for, then multiply this by the speed I wish to allow my ships to rotate. For the velocity, I applied the equation from here. I'm very unsure as to why it says to scale the whole thing by max acceleration, though...

Does this look correct and will it produce the sort of rotation you would expect? For context I'm simulating space- or ice-like inertia.


This bit:

elapsedTime * btnAcc.GetDuration() * MAX_ACC

looks suspicious to me. I don't think there should be two different times being multiplied in; either use btnAcc.GetDuration() (I'm guessing this measures how long you were pressing a button during the last frame?) or just elapsedTime if the acceleration was occurring for the whole frame. (FWIW, I've rarely seen button presses sampled more than once per frame in a game...usually you just use elapsedTime for everything and put if-statements around it that check the buttons.)

Anyway, MAX_ACC makes sense as it's converting from the time spent accelerating to the actual velocity change due to that acceleration, and therefore tweaks how punchy the ship will feel.

Other than that it looks like correct outer-space Newtonian physics to me.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.