What I've just written is still valid but after reading you again, and reminiscing our previous discussions, I better understand your problem. So I hope that now, it will perfectly be addressed.
Quote:Original post by Hurp
Sorry about that, I can see how what I said can be confusing.
I want to apply a force that would directly affect my players position over one time slice. However, if I directly add a force to just the players position, than it will move him in a direction that could move him through an object if I did not properly account for that in some how.
I was under the impression that only velocity should be directly added to a players position, is this correct?
Well, if my assumption is right, you're still using the "algorithm" and the initial updayte function I provided in a previous topic...
What you want to do is not adding a "force" to a player's position, but incrementing the player's position with a certain quantity (of course a vector).
This is what you want:
position += delta_position;
between t and t+dt.
Now, the equations I provided earlier don't take into account the velocity at time t+dt to compute the position at time t+dt, but the velocity at time t.
So as you've well guessed, affecting the velocity doesn't directly affect the position. It will at t+2*dt. But you want to directly affect position by affecting the velocity. It's simple: use another integration scheme.
This one offers a better accuracy than the one I proposed earlier (in another topic):
acceleration = force / mass;position += velocity * dt + 0.5 * acceleration * dt * dt;velocity += acceleration * dt;
This is equivalent to:
acceleration = force / mass;Vector3 old_velocity = velocity;velocity += acceleration * dt;position += 0.5 * ( velocity + old_velocity ) * dt;
You should be able to demonstrate it.
If you chose that integration scheme, then if you want the player's position to be incremented by
dx after the next update, apply the following force(*):
Vector3 force = mass * ( 2 * dx / dt - 2 * velocity ) / dt;
Then call the function that update the player's state, with that value:
void update( Rigid_body & body, Vector3 const & force, float dt ){ Vector3 old_velocity = body.velocity; body.velocity += force / body.mass * dt; body.position += 0.5 * ( old_velocity + body.velocity ) * dt;}int main( int argc, char ** argv ){ ... //dx is what you incorrectly call "impulse" Vector3 force = body.mass * ( 2 * dx / dt - 2 * body.velocity ) / dt; update( body, force, dt );}
This way you can continue using what you're using right now.
P.S:
(*) is derived this way:
x - xo = dx = ( vo + v )dt/2
<=>v = 2dx/dt - vo (1)
dv = v - vo
=>dv = 2dx/dt - 2vo (2)
v = vo + adt
<=>a = ( v - vo )/dt
<=>a = dv/dt (3)
(2) in (3)=>
a = ( 2dx/dt - 2vo )/dt (4)
F = Ma (5)
(4) in (5)=>
F = M( 2dx/dt - 2vo )/dt (6)
v = velocity at time t+dt
vo = velocity at time t
x = position at time t+dt
xo = position at time t
a = acceleration in time interval[t,t+dt]
dt = timestep of the simulation
[Edited by - johnstanp on July 6, 2010 8:56:51 AM]