Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Like
0Likes
Dislike

Double Buffered State

By Zachary Booth Simpson | Published Jun 19 2001 04:23 PM in General Programming

state physicsstate time temp model controller new using structure
If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource

© 2000 - Zachary Booth Simpson. Copied with permission from http://www.mine-control.com/zack. If you find any of this work useful, please sign Zack's guest book: http://www.mine-cont...i/gbook-zbs.cgi.


Intent

Track an old and new state for multi-Model Controllers.


Problem

Some Controller systems (for example, collision physics) need to track the state of several objects simultaneously and it must be ensured that they always read the same time step state for each Model instance.

If double buffering is not used, then as the physics system traverses the Models, it will become "confused" when it reads a state which has already been updated, thus using a new state instead of an old state.

This is analogous to a variable swap. int temp = a; a = b; b = temp; Without the temp varaible, a = b; b = a; would cause a==b always.

Solution

The necessary state information is typically isolated into a separate structure. For example:

struct PhysicsState {
	Vec3 pos,vel,angles,angVel,forceAccum,torqueAccum
};
Two instances of this class are combined into either the controller or model, often in a two element array. A global or local variable is used to flip between the two states using an accessor method.

For example:

class Model {
	PhysicsState states[2];
	PhysicsState getOldState() { return state[ globalFrame   &1]; }
	PhysicsState getNewState() { return state[(globalFrame+1)&1]; }
};


Structure

Not available at this time.


Examples

None at this time.


Issues and Risks

None at this time.


Related Patterns

Controller


Uses and References

Thanks to Chris Hecker.






Comments

Note: Please offer only positive, constructive comments - we are looking to promote a positive atmosphere where collaboration is valued above all else.




PARTNERS