CharacterController sliding on start/stop

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Naterfrost
Posts: 5
Joined: Thu Jul 13, 2017 1:59 pm

CharacterController sliding on start/stop

Post by Naterfrost »

Hello Norbo and thanks for the awesome library!
I'm using a CharacterController and i'm moving it using the MovementDirection of the HorizontalMotionConstraint property as displayed in the user input handler snippet bellow.
What i'm experiencing is that my character "slides" like he is accelerating/decelerating when he starts/stops moving and nothing that i do changes this..
Is this a normal and expected behavior? If it is not, can you check my configuration below please?
I have tried several things to counteract this (mainly by searching the forums) and i'm posting them below..
The space and character controller initialization scripts are:

Code: Select all

Space.ForceUpdater.Gravity = new Vector3(0, -10f, 0);
Space.TimeStepSettings.TimeStepDuration = 1f / 60f;
Character Controller initializer:

Code: Select all

 var cc =  new CharacterController(
                new Vector3(PlayerSpawnPoint.Center.X, PlayerSpawnPoint.Center.Y, PlayerSpawnPoint.Center.Z), //position
                CharacterHeight, //height
                CharacterHeight / 2f, //crouching height
                CharacterHeight / 4f, //prone height
                CharacterWidth,//radius
                0.1f, //margin
                10);//mass
cc.Body.CollisionInformation.CollisionRules.Group = characters;
Space.Add(cc);
The CharacterHeight in question is set to 1.75f and the Width to 0.75f


User Input handler:

Code: Select all

 
Moving = false;
Direction = MoveDirection.None;
CharacterController.Body.Orientation = Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), (float)-Math.PI * (_playerMovement.Heading * 0.0055f) / 100f);
Vector3 forwardDir = CharacterController.Body.WorldTransform.Forward;
Vector3 upDir = CharacterController.Body.WorldTransform.Up;
Vector3 strafeDir = CharacterController.Body.WorldTransform.Right;
forwardDir.Normalize();
upDir.Normalize();
strafeDir.Normalize();
WalkDirection = new Vector3(0, 0, 0);
if (_playerMovement.RightDirection < 0)
{
	WalkDirection -= strafeDir;
	Direction |= MoveDirection.Left;
	Moving = true;
}
if (_playerMovement.RightDirection > 0)
{
	WalkDirection += strafeDir;
	Direction |= MoveDirection.Right;
	Moving = true;
}
if (_playerMovement.ForwardDirection < 0)
{
	WalkDirection -= forwardDir;
	Direction |= MoveDirection.Backward;
	Moving = true;
}
if (_playerMovement.ForwardDirection > 0)
{
	WalkDirection += forwardDir;
	Direction |= MoveDirection.Forward;
	Moving = true;
}
WalkDirection.Normalize();

Vector3 refVector = WalkDirection * _moveSpeed; //_moveSpeed is 10f in this example
CharacterController.HorizontalMotionConstraint.MovementDirection = Vector2.Normalize(new Vector2(refVector.X, refVector.Z));
I tried doing the following when the entity is not moving: (not all simultaneously)

Code: Select all

CharacterController.Body.LinearMomentum = Vector3.Zero;
CharacterController.Body.AngularMomentum = Vector3.Zero;
CharacterController.HorizontalMotionConstraint.MovementDirection = new Vector2(0, 0);
CharacterController.Body.AngularVelocity = Vector3.Zero;
CharacterController.HorizontalMotionConstraint.MaximumForce=float.Max;
CharacterController.HorizontalMotionConstraint.MaximumAccelerationForce=float.Max;
I have even tried modifying it to be a Kinematic object when it stops moving and re-enabling it to dynamic(providing it's previous mass and local inertia tensor) but to no effect..
In the debugger, i see that the object has a traction support (it's not floating)
I'm losing my sanity so any idea is much appreciated.. :P
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: CharacterController sliding on start/stop

Post by Norbo »

The CharacterController itself modifies the HorizontalMotionConstraint.MaximumForce every update according to the current stance and CharacterController.(Air|Sliding|Traction)Force properties. So, setting CharacterController.TractionForce = float.MaxValue should do you want.

(Admittedly, the public API ain't great, I had to scratch my head for a minute before I remembered how it worked :P)
Naterfrost
Posts: 5
Joined: Thu Jul 13, 2017 1:59 pm

Re: CharacterController sliding on start/stop

Post by Naterfrost »

Actually setting the TractionForce of the CharacterController to float.MaxValue did exactly the opposite of what i wanted, now the character keeps floating around without stopping :P
Is the sliding (deceleration i suppose) of the character normal or i've done something wrong? :/
EDIT: In case i was not clear of what i wanted: I want my character to instantly stop without any deceleration (and start at maxspeed if able), but setting the MaxAcceleration & MaxAccelerationForce like you hinted in another simillar post, is not working for me..
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: CharacterController sliding on start/stop

Post by Norbo »

Adding these two lines to the WallDemo:

Code: Select all

            character.CharacterController.TractionForce = float.MaxValue;
            character.CharacterController.StandingSpeed = 25; //just to make the instant acceleration easier to see
results in this kind of behavior:
speedyfeet.mp4
(1.92 MiB) Downloaded 240 times
If you still observe sliding, there could be some other extra tuning beyond what's done in the demos that is interfering.
Naterfrost
Posts: 5
Joined: Thu Jul 13, 2017 1:59 pm

Re: CharacterController sliding on start/stop

Post by Naterfrost »

wow it actually worked when i set the standing speed to 25 like you pointed out.. but it still slides like before when it is 8.. any tips? Or to be more productive, why does increasing the speed, removes the sliding effect?
EDIT: well actually i'm seeing things.. it's a bit late i guess! The problem still occurs.. I attached a video of the case. I stop moving the character (take my hands of the keyboard) when he reaches the non-pink area in the left. Instead of "freezing" in place, he skiis to the left. This actually gets worse the more i increase the standing speed. Is the TractionForce supposed to "slow" down the entity?
Thanks for your time :)
Attachments
Sliding.mp4
(400.33 KiB) Downloaded 237 times
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: CharacterController sliding on start/stop

Post by Norbo »

Lower speeds should actually make sliding harder to see, not easier, just because the time between minimum and maximum speed is smaller. (And at float.MaxValue traction force, it should be instant either way.)

For example, if the character controller's linear velocity magnitude is output every frame with its default speed of 8 and a TractionForce of MaxValue, the transition from stationary to moving looks like this:
speed: 9.2095E-05
speed: 9.181187E-05
speed: 9.2095E-05
speed: 9.181187E-05
speed: 8
speed: 8
speed: 8
And going from moving to stationary looks like this:
speed: 8
speed: 8
speed: 8
speed: 8
speed: 2.441157E-05
speed: 0.0001633733
speed: 3.513391E-05
speed: 0.0001384765
speed: 6.856909E-05
There's no sliding going on there- just a tiny bit of micro-oscillation after stopping that damps itself out after a few frames.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: CharacterController sliding on start/stop

Post by Norbo »

Regarding the edit: I can't quite tell what's happening from the video- are you referring to the very last run to the left? If so, is it possible that it lost traction somehow at that point? Nothing else in that video looks like what I'd expect from insufficient control force, though.

I recommend trying to replicate the behavior in the demos. That way, we can rule out external influences, and I should be able to very quickly figure it out.

(Is that rubber bandy movement intended? If it's not, it could be a mistuned HorizontalMotionConstraint.PositionAnchorDistanceThreshold. Setting it to zero and comparing the result would be a good diagnostic.)

Edit:
And the TractionForce is just how much force the character can apply to its support when it has traction. That limits both how fast it can accelerate from stopped, and how fast it can decelerate from moving.
Naterfrost
Posts: 5
Joined: Thu Jul 13, 2017 1:59 pm

Re: CharacterController sliding on start/stop

Post by Naterfrost »

Well the rubber banding you are seeing is because it's an authoritative server - client proof of concept, where i removed the client side prediction when i saw the sliding effect and i wanted to see if it was the clients fault or the server.
Indeed the video is not clear of what i wanted to show you. To be honest when i replicate the same space geometry in the demos, i do not see any sliding but i do not mess with the demo character controller or space update..
Maybe i'm not doing something right with the update of the space? Take a look if you can!

Code: Select all

            Stopwatch timer = new Stopwatch();
            timer.Start();
            _isRunning = true;
            while (_isRunning)
            {
                try
                {
                    if (timer.Elapsed < TimeSpan.FromSeconds(1 / 60f))
                    {
                        if ((int)(1000f / 60f - timer.Elapsed.Milliseconds) > 0)
                        {
                            Thread.Sleep((int)(1000f / 60f - timer.Elapsed.Milliseconds));
                        }
                        continue;
                    }
                    Space.Update()
                    timer.Restart();
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Error in Physics background thread: {0}", ex.StackTrace);
                }
            }
EDIT: I have set the PositionAnchorDistanceThreshold to 0 but nothing has changed (not that i have any idea what it is supposed to do :P)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: CharacterController sliding on start/stop

Post by Norbo »

Nothing there would directly explain it, but on an unrelated note, beware Thread.Sleep- for values of 1 or higher, it merely guarantees that the thread won't resume until at least that much time has elapsed. Thread.Sleep(1) could last 15+ milliseconds.

Edit:
Small correction/clarification- Thread.Sleep can put the thread under for some amount of time between two system clock 'ticks', so it could actually be less in some circumstances too. The core issue is that it's operating on a resolution that defaults to ~15+ ms on Windows, which isn't great for game timing.
Naterfrost
Posts: 5
Joined: Thu Jul 13, 2017 1:59 pm

Re: CharacterController sliding on start/stop

Post by Naterfrost »

I see, thanks very much for your time and effort :) I will try to narrow down the problem and will repost here if i find something new!
Post Reply