Space.Update - Not Updating Some Objects Every Update

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
qbvbsite
Posts: 8
Joined: Fri Oct 24, 2014 3:30 am

Space.Update - Not Updating Some Objects Every Update

Post by qbvbsite »

Hey,
Currently I have setup BePU as an Authoritative Server and running it at 60FPS. When my character is near 0,0 - 50,50 I update no problem every 100ms (sending 10 updates a second to client).... Once my Character hits 100,100 BePu is only updating him once every 200ms... when he hits 200,200 its around 300-400ms. Is there a reason for this? If so is there a setting I can set to stop this from happening? I'm extreme new to this so any help would be great.

Here is my code for the Space:

Code: Select all

 //Create 4 Threads
            _parallelLooper = new ParallelLooper();
            _parallelLooper.AddThread();
            _parallelLooper.AddThread();
            _parallelLooper.AddThread();
            _parallelLooper.AddThread();

            //Create World
            _space = new Space(_parallelLooper)
            {
                ForceUpdater = {Gravity = new Vector3(0, -9.81f, 0)},
                TimeStepSettings = {TimeStepDuration = 1f/60f},
            };

            //Setup World

            //Make Characters Not Collide With Characters
            var collisionPair = new CollisionGroupPair(_charactersCollisionGroup, _charactersCollisionGroup);
            CollisionRules.CollisionGroupRules.Add(collisionPair, CollisionRule.NoBroadPhase);
My Update Thread:

Code: Select all

public void Run(object threadContext)
        {
            var timer = new Stopwatch();
            timer.Start();
            _isRunning = true;

            while (_isRunning)
            {
                //Sleep If No Players
                if (_region.NumberOfPlayers <= 0)
                {
                    Thread.Sleep(1000);
                    timer.Restart();

                    continue;
                }

                //Sleep for X Amount of Time To Save Processer
                var sleepTime = (int) ((1000f / 60f) - timer.Elapsed.Milliseconds);
                if (sleepTime > 0)
                {
                    Thread.Sleep(sleepTime);
                }

                var updateTime = timer.Elapsed;
                timer.Restart();

                Update(updateTime);
            }
        }

        private void Update(TimeSpan elapsed)
        {
            lock (this)
            {
                _space.Update();
            }
        }

My Character:

Code: Select all

            //Create Spawn Point
            var spawnPoint = new Vector3(obj.Position.X, obj.Position.Y, obj.Position.Z);

            //Create Character Controller
            var characterController = new CharacterController(spawnPoint, CharacterHeight, CharacterHeight - 0.6f, CharacterWidth, 0);
            characterController.Body.CollisionInformation.CollisionRules.Group = _charactersCollisionGroup;

            //Set Character Controller
            physicsObject.CharacterController = characterController;

            //Add To World
            _space.Add(characterController);
Thanks,

--James
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Space.Update - Not Updating Some Objects Every Update

Post by Norbo »

How is the update rate being measured?

There is nothing in the engine which would change update rates based on position, and individual objects do not have different update rates. The engine simulates a series of global time steps.

While not related to update rates, the deactivation system can put entities into a low cost state when they are stationary as an optimization. While sleeping, the entity will not fire most events including the Entity.PositionUpdated event. If the character is occasionally sleeping and this is how update rates are measured, this could explain the observation.
qbvbsite
Posts: 8
Joined: Fri Oct 24, 2014 3:30 am

Re: Space.Update - Not Updating Some Objects Every Update

Post by qbvbsite »

Hey,
This is currently the way I have it setup (probably not the best and after looking at it I probably should use PositionUpdated event... not sure how it works as I haven't looked into it).

1 - Physic running in a Thread doing 1/60f
2 - PlayerUpdate thread running at 10 updates a second checking for position changes. This is done by checking last update value and

Code: Select all

CharacterController.Body.WorldTransform
3 - If changed messages are sent out to clients
4 - players update movement about 5 times a second using this

Code: Select all

var referanceVector = (MovementDirection*MoveSpeed);
                CharacterController.HorizontalMotionConstraint.MovementDirection = new Vector2(referanceVector.X, referanceVector.Z);
Thoughts?

--James
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Space.Update - Not Updating Some Objects Every Update

Post by Norbo »

What part was measured to find the 300-400ms interval? Was it just the time between detections of state change by the player thread? Were both threads running at full rate while this was happening?

A few shots in the dark:
1) Make sure the player thread is running when it should.
2) Make sure the physics thread is running when it should.
3) Make sure the player thread reads of physics-related data are thread safe. By default, nothing is; if the Space is permitted to update while things are being read from it (or worse, things are being written to it), stuff won't work like it should.
4) Make sure the mechanism responsible for ensuring that thread safety is not interfering with execution significantly. Big contested locks around the entire physics update and around the player thread's execution are top suspects if the player thread's execution takes much time at all (Thread.Sleep within the lock could cause big problems).

Side notes:
I probably should use PositionUpdated event... not sure how it works as I haven't looked into it
I would not recommend the PositionUpdated in this case, actually. It will fire every single time step in which the character moves, which is far more frequent than network updates need, leading to most of the event invocations being ignored. Polling state on demand seems more natural in this use case.

Code: Select all

var referanceVector = (MovementDirection*MoveSpeed);
CharacterController.HorizontalMotionConstraint.MovementDirection = new Vector2(referanceVector.X, referanceVector.Z);
The MoveSpeed scale will get normalized out; the MovementDirection is just direction, not magnitude. If you'd like to change the speed at which the character tries to move, use the CharacterController's speed-related properties. If you want to give clients the most up-to-date information about the character's state, you'll need to include both the character-level state like the MovementDirection as well as the entity-level LinearVelocity and Position. The character-level stuff describes what the character is trying to do (and will inform client versions of the characters to try the same), while the entity-level stuff describes what is actually physically happening.
qbvbsite
Posts: 8
Joined: Fri Oct 24, 2014 3:30 am

Re: Space.Update - Not Updating Some Objects Every Update

Post by qbvbsite »

Hey,
Thanks for all the information on the character controller. I figured it out it, It was in fact an issue when I was comparing new position with the old position (Old bullet physic's code). I just switched it to check Vector3's instead and all is well in the world. Thanks again for you quick and extremely informative reply.

Thanks,

--James
Post Reply