# Is there a problem with my update loop?

## Recommended Posts

juxie    122
Hi, Just want to make sure my logic is right. 1. get current state position and velocity
_pos = _currPos;
_vel = _currVel;


2. calculate forces using current state
_force += gravity * mass;
_force += springForce;
_force += windForce;


3. calculate velocity and attempted position

_attVel = _currVel + (_force / mass) * dt;
_attPos = _currPos + _attVel * dt


4. check collision detection using _attPos 5. if collision, response, put _attPos to the point of collision

_attPos = pointOfCollision;


6. get collision normal
normal = _attPos - spherePos;


7. calculate impulse
Vn = dot(_attVel, normal) * normal;
Vt = _attVel - Vn;

_attVel = Vt - 0.1f * Vn;


8. Finalize step
_currVel = _attVel;
_currPos = _attPos;



##### Share on other sites
chipmeisterc    268
Why do you need step one:- To get the current position and velocity??...looking at your steps you never use these local variables anyways :) Anywhere where you have to syncronise local variables to member varaibles is generally a bad idea and can lead to bugs.

##### Share on other sites
juxie    122
Quote:
 Original post by chipmeistercWhy do you need step one:- To get the current position and velocity??...looking at your steps you never use these local variables anyways :) Anywhere where you have to syncronise local variables to member varaibles is generally a bad idea and can lead to bugs.

The step one is used in the calculation of spring force but it is also a member variable.
That means it is member of the class that contains currPos.
And _pos and _vel are never used to synchronise anything.

So i guess this is logic is correct?

##### Share on other sites
chipmeisterc    268
surely _currentPos and _currentVel are your member variables, if so why do you need to assign these to _pos and _vel ?

##### Share on other sites
juxie    122
Quote:
 Original post by chipmeistercsurely _currentPos and _currentVel are your member variables, if so why do you need to assign these to _pos and _vel ?

I need to implement RK4 so _pos and _vel will be used as my temporary position as i move each step of RK4.

##### Share on other sites
Zahlman    1682
Force should not accumulate over time.

force = gravity * mass + springForce + windForce;