3
votes

I am trying to do a basic version of space war(http://en.wikipedia.org/wiki/Spacewar_%28video_game%29) but I cannot figure out how to do the inertia part

that is my code : I should let the ship accelerate or slow down based on where it faces

model is the ship vx and vy are velocity of x and y direction theta are rotate degree 20 is for make it move slow

 vx=model.vx+(cos (degrees model.theta))/20,
 vy=model.vy+(sin (degrees model.theta))/20

but it does not seem right Can someone help me? I am horrible in physics!

1
So this is the bit that's supposed to add new thrust to the velocity? You've got the basic Euler integration stuff of summing velocity into position and reducing it? - Tommy
yes, consider this as : up arrow give a force to push the ship at the tail to the head down arrow give a force to push the ship at the head to the tail - SwordW
Also, what is Euler integration? I try to get the x direction force branch and y direction force branch from the one of the force from above comment and use the data to change the vx and vy - SwordW
Oh, Euler integration is just approximating the integral of f(t) between 0 and 1 by slicing and adding, e.g. 0.25*f(0) + 0.25*f(0.25) + 0.25*f(0.5) + 0.25*f(0.75). Which is like pretending that f has the same value between 0 and 0.25, then the same value between 0.25 and 0.5, etc. So the approximation is pretending that time is discrete. Normally expressed in games as just position += velocity; velocity *= 0.99; or whatever that looks like in your language (admittedly possibly quite different in functional ELM, since that's a destructive state mutation, etc) - Tommy
... otherwise: what effect do you see? E.g. does the ship always travel sideways (you've probably got sin and cos the wrong way around), go in the right direction if aligned to one axis, exactly the wrong direction if aligned to the other, to values in between otherwise (you've probably got a clockwise/anticlockwise error), just seem to go in some random direction (possibly sin/cos actually want radians)? - Tommy

1 Answers

1
votes

A very accurate and efficient integration is to compute: PosNext = 2 * PosCurrent - PosPrevious + Acceleration*Timestep^2

It is called Verlet integration scheme. For Velocities you just update by: VelocityNext = (PosNext-PosCurrent)/TimeStep.

You can use your sine and cosine with the acceleration constant. Euler forward is not very accurate, try to avoid it.