# Is there a problem with my update loop?

This topic is 3791 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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
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
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
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
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
Force should not accumulate over time.

force = gravity * mass + springForce + windForce;

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631775
• Total Posts
3002275
×