motion and velocity to increment amount and vice versa
I don't really know math well beyond Algebra 1, however I can usually "hack" a math book and make the equations work in software I write - I'm trying to write a program that has objects moving onscreen, with momentum, velocity, mass, thrust, etc which seems to be called Vector Math.
How would I go about converting between the world of cartesian coordinates x & y to the world of physics? For instance, how do I determine the increment amount per iteration of a loop in order to create a flying objec开发者_JAVA技巧t that simulates a (2d) spacecraft in terms of behavior with simple left-right-up-down thrusters?
An example of what I"m trying to figure out:
x=x+getnextstep(thrust, direction).x
y=y+getnextstep(thrust, direction).y
so I would input an amount, say 0-127 for thrust and 0-360 as direction, and get back the amount to increment
Please answer in psuedocode if possible and if you feel patient enough, explain the how the conversion is done between the cartesian step-wise and momentum/mass/velocity world.
Start with 1D.
Velocity is the rate of change of position over time. At time t1, an object is at position x1. You let it coast at constant velocity v, from time t1 to time t2. The distance it's traveled (x2-x1) is velocity (v) multiplied by elapsed time (t2-t1):
(x2-x1) = v (t2-t1)
x2 = x1 + v (t2-t1)
Or in code:
x += v dt
where dt is the amount of (simulated) time since you last updated everything.
Acceleration is the rate of change of velocity over time. If acceleration is constant, you have to keep track of x and v, and we get
x += v dt + a(dt)/22
v += a dt
(Note that you can't reverse the order of those steps, not without changing the algebra.)
That's enough to simulate an object sliding back and forth with a single thruster. You control the "a" which is supplied by the thruster. If you want vertical motion in a gravity field, gravity applies its own acceleration; just add gravity to the acceleration of the thruster, and that's your a. (The acceleration of gravity points down, by definition, and by convention it's called "g".)
Don't worry about momentum or energy for now. Or friction or air resistance. Or collisions.
2D: motion in x and motion in y are basically independent, so you can just treat them as two 1D motion simulations: {x, vx, ax} and {y, vy, ay}.
If you want a thruster you can point (e.g. 0-360), you must break up that vector into an x component and a y component, then apply the above. Breaking up a vector like that is trigonometry; if you're not familiar with it, you'll have to crack a book (or wikipedia). It's not that complicated, but it's hard to explain here.
That should keep you busy for a while. If it's too easy and you want a greater challenge, look at the first few chapters in an introductory physics text.
精彩评论