• 06/19/01 10:23 PM
    Sign in to follow this  

    Double Buffered State

    General and Gameplay Programming

    Myopic Rhino
    [i](C) 2000 - Zachary Booth Simpson. Copied with permission from [url="http://www.mine-control.com/zack"]http://www.mine-control.com/zack[/url]. If you find any of this work useful, please sign Zack's guest book: [url="http://www.mine-control.com/cgi/gbook-zbs.cgi"]http://www.mine-cont...i/gbook-zbs.cgi[/url].[/i]


    [size="5"][b]Intent[/b][/size]
    [indent][bquote]Track an old and new state for multi-Model Controllers.[/bquote][/indent]
    [size="5"][b]Problem[/b][/size]
    [indent][bquote]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.[/bquote]
    [/indent] [size="5"][b]Solution[/b][/size]
    [indent][bquote]The necessary state information is typically isolated into a separate structure. For example:

    [code]struct PhysicsState {
    Vec3 pos,vel,angles,angVel,forceAccum,torqueAccum
    };[/code]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:

    [code]class Model {
    PhysicsState states[2];
    PhysicsState getOldState() { return state[ globalFrame &1]; }
    PhysicsState getNewState() { return state[(globalFrame+1)&1]; }
    };[/code][/bquote][/indent]
    [size="5"][b]Structure[/b][/size]
    [indent][bquote]Not available at this time.[/bquote][/indent]
    [size="5"][b]Examples[/b][/size]
    [indent][bquote]None at this time.[/bquote][/indent]
    [size="5"][b]Issues and Risks[/b][/size]
    [indent][bquote]None at this time.[/bquote][/indent]
    [size="5"][b]Related Patterns[/b][/size]
    [indent][bquote]Controller[/bquote][/indent]
    [size="5"][b]Uses and References[/b][/size]
    [indent][bquote]Thanks to Chris Hecker.[/bquote][/indent]


      Report Article
    Sign in to follow this  


    User Feedback

    Create an account or sign in to leave a review

    You need to be a member in order to leave a review

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    There are no reviews to display.