Page 1 of 1

LinearVelocity and shaking object

Posted: Wed Dec 10, 2014 9:24 am
by chrome
Hello :)
I create space-sim game and have some problems with correct implementation of BEPU. I have some object categories:
- player - we are inside cockpit and can move in all directions
- friendly / enemy ship - can move in all directions
- static objects like asteroids - they don't move
- bullets - move forward
- missiles - move like ships

For all of them I use Entity to represent physics. Is it good for player / ships to use Entity, or maybe I should change it to CharacterController ?
In update method I calculate current speed (on my own) and after that I set Velocity:

Code: Select all

            movement = new Vector3(0, 0, CurrentSpeed);
            movement = Vector3.Transform(movement, Rotation); // movement vector
            Physics.LinearVelocity = Game1.ConvertVector3ToBEPU(movement); // (1)
            //Physics.LinearVelocity = Game1.ConvertVector3ToBEPU(movement * dTime * Game1.SpeedMultiply); // (2)
I suppose it's a bad way to set velocity. I update Physics.LinearVelocity in every frame by movement value. Movement vector is changing when player push W (increase speed), or S (decrease).
When I using (1) my game slow down when have low FPS (some kind like slow motion).
When I using (2) my game work properly but Physics.LinearVelocity are different every frame and my ship, or friendly / enemy ship are shaking.

My space update method look like:

Code: Select all

        private void UpdateSpace()
        {
            space.ForceUpdater.Update();
            space.BoundingBoxUpdater.Update();
            space.BroadPhase.Update();
            space.DeferredEventDispatcher.Update();
            space.NarrowPhase.Update();
            space.PositionUpdater.Update();
            space.BeforeNarrowPhaseUpdateables.Update();
            space.BeforePositionUpdateUpdateables.Update();
            space.BeforeSolverUpdateables.Update();
            space.DeactivationManager.Update();
            space.DuringForcesUpdateables.Update();
            space.EndOfFrameUpdateables.Update();
            space.EndOfTimeStepUpdateables.Update();
            space.EntityStateWriteBuffer.Update();
            space.Solver.Update();
        }

Re: LinearVelocity and shaking object

Posted: Wed Dec 10, 2014 9:11 pm
by kelthar
Hmmm. Why don't you use Space.Update() instead. I've encountered this before. I think it was when I sent a bad elapsed to the Update(dt). Possibly it could have been when I set the velocity. Is the Space.Update() on it's own thread? Then you should set the Entity.BufferedState of LinerarVelocity instead.

Re: LinearVelocity and shaking object

Posted: Thu Dec 11, 2014 8:17 am
by chrome
I don't remember why, but space.Update() worked badly so instead it I use my Update method. Probably SpaceObjectBuffer worked badly, but I don't remember why.
I change my method to this:

Code: Select all

        private void UpdateSpace()
        {
            space.Update(dTime);
        }
and got slow motion. Next try:

Code: Select all

        private void UpdateSpace()
        {
            space.Update(dTime);
        }
and in first frame object is in correct place, but in second is in bad and this loop so I have 50% working physics :)
I read to sent previously dTime so I change it to:

Code: Select all

        private void UpdateSpace()
        {
            space.Update(dTimePrev);
        }
with this same effect :/
dTime and dTimePrev is calculated as below:

Code: Select all

        protected override void Update(GameTime gameTime)
        {
            dTimePrev = dTime;
            dTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

           // rest of code...
        }
Current speed update method doesn't change:

Code: Select all

            movement = new Vector3(0, 0, CurrentSpeed);
            movement = Vector3.Transform(movement, Rotation); // movement vector
            Physics.LinearVelocity = Game1.ConvertVector3ToBEPU(movement); // (1)

Re: LinearVelocity and shaking object

Posted: Thu Dec 11, 2014 8:22 pm
by Norbo
Since I am not clear on exactly what is going wrong under what circumstances, here's some notes to try to clear up some confusion I noticed:

-As mentioned, definitely use some Space.Update overload rather than calling individual stages' updates. There are some very specific use cases where calling individual stages is helpful, but this does not appear to be one of them.
-Space.Update() without any parameter takes a single time step of length Space.TimeStepSettings.TimeStepDuration.
-Space.Update(dt) will take as many time steps of length Space.TimeStepSettings.TimeStepDuration as needed to reach the accumulated simulation time, up to the maximum of Space.TimeStepSettings.MaximumTimeStepsPerFrame. This is referred to as 'internal timestepping'; it is useful when the time between frames varies and you want the engine to update when it needs to based on how much time has accumulated.
-If your game is running with a fixed time step such that the elapsed time between frames is known to be constant, use Space.Update() instead of Space.Update(dt) since internal timestepping won't do anything helpful.
-Leave Space.TimeStepSettings.TimeStepDuration constant during simulation whenever possible.
-If things seem to be in slow motion, either 1) objects are large relative to the velocities and forces involved, or 2) the simulation is not progressing through time fast enough. Make sure that the scale of objects and their velocities are reasonable, and make sure that you're calling Space.Update correctly.
-Velocity is already 'space units per time unit'. That is, an object is displaced by velocity * Space.TimeStepSettings.TimeStepDuration at each time step. So, don't premultiply dt into the velocity.
For all of them I use Entity to represent physics. Is it good for player / ships to use Entity, or maybe I should change it to CharacterController ?
Only use CharacterController for things which behave like characters, like the player in Half-Life.

I would recommend checking out the BEPUphysicsDemos in the source as well as the demos associated with the documentation to see working configurations.

Re: LinearVelocity and shaking object

Posted: Thu Dec 11, 2014 10:33 pm
by chrome
Norbo wrote:Since I am not clear on exactly what is going wrong under what circumstances
Here are screens:

Good:
Image
Bad:
Image

The object distance from screen on "Bad image" is proportional to velocity and doesn't change until I change velocity so I have two states: good one and bad one. This two screens are in loop and change every frame.

Re: LinearVelocity and shaking object

Posted: Thu Dec 11, 2014 10:40 pm
by Norbo
Assuming that the camera and the graphic are positioned in world space, it appears that the camera and graphics are sampling their positions at different times. For example, if the camera's transform is set before the frame's physics simulation runs and the model's transform is set after, the camera and model will be separated proportional to velocity.

Re: LinearVelocity and shaking object

Posted: Sat Dec 13, 2014 1:40 pm
by chrome
Norbo wrote:Assuming that the camera and the graphic are positioned in world space, it appears that the camera and graphics are sampling their positions at different times. For example, if the camera's transform is set before the frame's physics simulation runs and the model's transform is set after, the camera and model will be separated proportional to velocity.
Thank you for all tips. I changed place of calling space.Update() method and everything works great now. You are amazing ! :)