On 12/21/2017 at 4:36 PM, JoeJ said:p += v * timestep; // update position with velocity
How is this done, please?
Sincerely,
Josheir
On 12/21/2017 at 4:36 PM, JoeJ said:p += v * timestep; // update position with velocity
How is this done, please?
Sincerely,
Josheir
46 minutes ago, Josheir said:How is this done, please?
Sincerely,
Josheir
With position p and velocity v being vectors, this is just simple vector math. I would recommend reading up on vectors and vector math if this is unknown territory for you currently.
If timestep is a float, v * timestep is just a scalar multiplcation of the vector -- scaling all components of the vector by the same amount.
v * timestep is the same as "delta offset this timestep" (and the result of this is a vector). This resultant vector can then be added onto p to change the position.
Hello to all my stalkers.
If you're more visual of a learner Jorge Rodriguez on YouTube is a good instructor on the subject of vector maths (along with a ton of other subjects once you've learned the basics, he even covers character movements in 2D and 3D):
"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin
22 minutes ago, Josheir said:How is this done, please?
Probably you need to refine your question, do you mean how to do it in 2D without a vec2 struct? That would be simply:
vx += ax * timestep; vy += ay * timestep;
px += vx * timestep; py += vy * timestep;
Or is it a question about physics? I'd say that ignoring air resistance and external forces like gravity a body travels at constant velocity.
If we add constant external force like gravity or a thruster, this force creates acceleration that increases velocity over time.
Note how the linear increasing velocity causes squared increase of position. If we plot numbers, we get something like
velocity / position
10 1
20 2
30 5
40 18
This is because timestep is a factor for both velocity and position, so we get a quadratic equation of motion.
For the simple case of constant acceleration, we could use a exact analytic solution for our example:
position_after_time = initial_position + 0.5 * constant_acceleration * time*time; // assuming initial velocity is zero
With this equation you get position at any time without the need to do many integration iterations using small timestep as above.
Unfortunately this rarely works in practice as many forces may affect a body at any time (collisions, thrusters, bullet hit etc), so we use integration instead which is only an approximisation but good enough if we are careful. (careful means using small timesteps to keep error small as well.)
But the analytic equation can answer difficult questions like: How long does it take until a falling stone hits the ground, or at what angle and velocity do i need to launch a projectile to hit a distant target under natural trajectory.
However, notice that squared time appearing in both methods. If we choose a timestep of 1, 1*1 is still 1 so this is a common source of mistakes and confusion if you try to learn this stuff with little math background. Looking at your code snippet i guess you're on your way to pitfalls like that. I'm self taught as well and i remember... So using established terminology helps to communicate and using correct math helps to understand. (But realistic physics might not be what you want for your current game.)