Advertisement

When to apply physics update in game?

Started by January 21, 2016 05:17 AM
0 comments, last by Hodgman 8 years, 7 months ago

I'm using Farseer Physics in XNA, using C#. The physics engine updates through a step function with a time value as a parameter. Currently I have the step happening at the beginning of the frame, but I just realized that, if I press a button for the player to move, the physics engine doesn't know about it until the next frame, and I am drawn stationary the first frame a button is pressed. This has led me to believe that placing the physics update last every frame will solve all my problems, but I now have an additional worry. There are some occasions when I want to respond to a collision with an instantaneous velocity change. My code listens for a collision, and then responds to it by adding an additional velocity to one of the colliding objects. The problem with this is that the velocity change isn't seen by the physics engine until the next engine update.

I am trying to create a gravity lift that directs an object's velocity a certain direction at a certain magnitude in response to a collision, and I need the change to be immediate. I can't really figure out a way to do it, short of altering the code of the physics engine itself.

Can you apply user input before the physics update, and respond to physics events after?

inputs = GetInputs()
events, worldState = UpdatePhysics(inputs)
UpdateGameRules(events)
DrawScene(worldState)

This topic is closed to new replies.

Advertisement