Quote:Original post by Kasya
Hello,
I just changed the prev_position = temp - velocity * dt;
*** Source Snippet Removed ***
But i shocked here because the Results Are Absolutely same with: prev_position = temp - velocity * dt
What can i do?
Thanks,
Kasya
Kasya, your original code, prev_position = temp, was correct. Change it back. Check my reply, as you have an error in your position integration.
I explained in my other post why it is possible for position to increase for a while until velocity (implicitly) becomes negative.
But, in your case, there
is another problem. cannonicus identified it, but you did not address it correctly. You did not initialize prev_position to a meaningful value. You appear to have initialized prev_position to (0,0,0). So, when you begin your integration for the first time, you have:
prev_position = (0,0,0)
position = (0,50,0)
BEFORE you call Integrate. So, for your first integration, you get:
position += ((0,50,0) - (0,0,0)) + (0,-10,0) * .001;
Or, position += (0,50,0) + (0,-.1,0) = (0,99.9,0)
The issue here is...since you did not compute a prev_position that reflects your desired initial velocity of (0,5,0), you get a much higher initial velocity. Actually, for the time step you use, dt = 0.1, you have an initial velocity of (0,5000,0)!!! Rather large! It'll take many, many, many time steps before your velocity goes negative and the position starts decreasing!
So, here is the solution. Before you begin the simulation loop,
compute prev_position based on your desired starting position and velocity, using a simple two point difference:
desired_velocity = (position - prev_position) / dt;
So,
prev_position = position - (desired_velocity * dt);
Or,
prev_position = (0,50,0) - 0.01*(0,5,0);
Or,
prev_position = (0,49.95,0);
So, then, if you START your simulation with prev_position having been set to (0,49.95,0) and position = (0,50,0), you will be working with values that properly represent your initial velocity of (0,5,0). Your simulation (with my correction as shown in the other post) should go fine. It will still take a few steps before position will start to reduce, but it will happen.
Hope this helps.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net