Stairs handling for character controller?

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
demiurghg
Posts: 14
Joined: Mon Jun 04, 2012 10:02 am

Stairs handling for character controller?

Post by demiurghg »

Hello,

I've implemented my own character controller based on dynamic body.
It is very simple, but bahaves pretty well. Even handles walking on sharp edges, between convergent walls and so on. See attachment.

What is a best method to handle stairs for this kind of character controller?

Thanks for advance!
Attachments
CharacterController.cs
(6.26 KiB) Downloaded 289 times
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Stairs handling for character controller?

Post by Norbo »

Stepping is one of the hardest behaviors to make robust in a wide range of situations. Check out the CharacterController and its supporting systems in the BEPUphysicsDemos to see an example of how to do stepping robustly.

Making a world that just doesn't need stepping is a good approach. For example, instead of stairs, use a ramp for the physics and stairs for the graphics. If stepping is really, really required, I'd recommend just using the CharacterController from the demos.
demiurghg
Posts: 14
Joined: Mon Jun 04, 2012 10:02 am

Re: Stairs handling for character controller?

Post by demiurghg »

I need little bit different dynamic "feel" of character controller.
And I continued my experiments :)

Method, that seems to me to be ok is following:
1. Character body (capsule or cylinder) hovers at height equals to stepHeight.
2. On each Update() I perform convex cast from bottom of the character to the ground (with small margin).
3. If distance between body and ground is less then stepHeight , I apply vertical force to the body, that equals to: F = ( dh * k1 )tension - ( max(V.Y, 0) * k2 )damping + ( m*g )weight. If k1 == 2*k2 oscillations become completely eliminated. Function max() is required to make positive vertical motions more rapid, otherwise jumps and climbs can damp out.

Code (with some magic numbers):

Code: Select all

			float t;
			Vector3 n,p,pu;
			Vector3 p0 = Position + Vector3.Up * (0.2f+StepHeight);
			Vector3 p1 = Position + Vector3.Down * 0.2f + Vector3.Up * MathHelper.Clamp( Body.LinearVelocity.Y * dt, -100,0 );

			bool r = SweepTest( p0, p1, out n, out p, out t ); 
			//bool r = RayCast( p0, p1, out n, out p );

			if (r) {
				contactNum++;
				float h = Position.Y - p.Y;
				if (h<0) {
					float dh = -h + 0.2f;
					float vv = MathHelper.Clamp( Body.LinearVelocity.Y, -100, 0);
					float f  = dh * 4000 - vv * 1500;
					Body.ApplyImpulse( Vector3.Zero, (Vector3.Up * dt * f) - (Gravity * mass * dt) );
				}
			}
1. I have encountered small vertical jitter. How can I solve it? (Of cource, I can filter this value, but I'm looking for more accurate method).
2. Can I implement this method more sophistically using some constrain or jointed body or something else that provided by BEPU physics library?
3. Whether this method can have pitfalls that I have not met?

Thanks for advance!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Stairs handling for character controller?

Post by Norbo »

1. I have encountered small vertical jitter. How can I solve it? (Of cource, I can filter this value, but I'm looking for more accurate method).
Jitter can arise from a variety of sources. Here's an incomplete sampling:
-It might be that the vertical force continually pushes the character beyond the reach of the force, allowing it to fall back down repeatedly.
-It might be excessive spring rigidity combined with a long time step.
-it might be numerical issues with the convex cast and the environment.
-It might be some order of operations issue.
I can't really say for sure without spending more time analyzing it and seeing it in action (which I would prefer to avoid :)).
2. Can I implement this method more sophistically using some constrain or jointed body or something else that provided by BEPU physics library?
It can be implemented more robustly in the form of a constraint like the VerticalMotionConstraint in the CharacterController. Doing so requires greater familiarity with the solver. It may not be very useful to spend the extra time doing this unless you plan to have lots of physical structures dependent on the the character. There is no premade 'hover constraint' in in the main library.
3. Whether this method can have pitfalls that I have not met?
There's a fundamental tradeoff with stepping methods based on forces and velocity:
-If it moves too quickly, the velocities will be high enough such that it can cause explosions. Stepping near some object could send that object flying. In certain cases where the character loses contact with the ground, the character might go flying.
-If it moves too slowly, the character won't climb fast enough. The body will bump into the stairs, halting the character repeatedly.

That tradeoff can be manageable depending on the environment design.

However, there's a more dangerous combination of problems:
-Velocity-based stepping does not avoid the need for step validation in the general case. The character will still happily step into invalid states unless explicitly prevented.
-"Hovercharacter" methods, where the character has open air below it for stepping, make it extremely difficult or impossible to stop invalid steps. Since there's nothing stopping the character from moving horizontally onto the invalid support, they will happily scoot into an invalid state. At that point, there's not really any good way to recover.

While it's possible to address the second problem by removing the hover margin, other problems are introduced. Without the hover margin to smooth out steps, the character will just slam up against the stairs repeatedly while waiting for the force to push it up and over. A more complex approach built around conditionally active collision constraints could address this too, but there will be a swarm of corner cases to handle.
I need little bit different dynamic "feel" of character controller.
Before going through the hurricane of corner cases associated with full-featured character controller development, I would strongly recommend seeing if the existing robust CharacterController could be modified to do what you need. Keep in mind, a lot of the 'feel' of a character controller can be defined by factors unrelated to the actual physical representation of the character. For example, smoother stepping can be accomplished by loosening the camera interpolation.
demiurghg
Posts: 14
Joined: Mon Jun 04, 2012 10:02 am

Re: Stairs handling for character controller?

Post by demiurghg »

Today I have almost achieved necessary "feel" with standard character controller.
One issue is left: how to add friction/reduce speed when character touches the wall even while being on air?

It looks unrealistic, because when I jump into the wall at an angle (about 45 degrees) some strange, but a noticeable force pulls me aside.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Stairs handling for character controller?

Post by Norbo »

The specifics depend on your character implementation. It all comes down to modifications to velocity. One option: scan the contacts, analyze the normals, compute tangential velocity, apply desired friction velocity change.
It looks unrealistic, because when I jump into the wall at an angle (about 45 degrees) some strange, but a noticeable force pulls me aside.
Sorry, I don't really understand what you mean.
demiurghg
Posts: 14
Joined: Mon Jun 04, 2012 10:02 am

Re: Stairs handling for character controller?

Post by demiurghg »

I'm about character controller that is used in "Character Playground Demo".
When I hit the wall while running or jumping a sliding velocity is too high. See picture.

Is it ok to check side contacts in SupportFInder and reduce linear velocity of the body?
Does it make character controller less robust?
Attachments
wall.png
wall.png (7.54 KiB) Viewed 6516 times
Last edited by demiurghg on Fri Jul 27, 2012 8:43 pm, edited 1 time in total.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Stairs handling for character controller?

Post by Norbo »

If the sliding motion is faster than the character can run under normal circumstances, it would be best to identify where the source of extra velocity is rather than applying friction to hide it. If it's running exactly as fast as it would if the velocity normal to the surface was removed, and you want it to move slower, then some kind of friction is indeed warranted.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Stairs handling for character controller?

Post by Norbo »

I'm about character controller that is used in "Character Playground Demo".
When I hit the wall while running or jumping a sliding velocity is too high
The demos CharacterController simply has no friction against walls; you're not seeing any extra velocity beyond frictionlessness with the demos CharacterController, are you?
Is it ok to check side contacts in SupportFInder and reduce linear velocity of the body?
Does it make character controller less robust?
Using the side contacts would be perfectly fine. It will not negatively affect the core behavior of the character.
Post Reply