## Bepu v2 Character Controllers

Discuss any questions about BEPUphysics or problems encountered.
parapoohda
Posts: 92
Joined: Fri May 31, 2019 6:30 am

### Re: Bepu v2 Character Controllers

Thank you. It work. Is it magic? How could it work

parapoohda
Posts: 92
Joined: Fri May 31, 2019 6:30 am

### Re: Bepu v2 Character Controllers

Is view direction is rotational direction of object? I munipulate target velocity(x-y) but I can't pair vector 2 to 2/3 axis of vector3.
If i change view direction it also shuffle pair of x or y to vector3. I can't pair x of vector2 to x of vector3 and y of vector2 to z of vector3.
How can I use bepu with bird eye view. Should i pair x to x and -y to z in view direction (0,0,-1)?

Norbo
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

### Re: Bepu v2 Character Controllers

View direction is sorta-kinda rotational direction. It is the starting point from which a frame of reference is created. It is projected down onto the surface that currently supports the character to create the 'forward' direction, and then the character's up direction and that computed forward direction are used to compute the 'right' direction.

A target velocity of (1,0) will try to move along the positive 'right' direction at a speed of 1, while a target velocity of (0, 1) will try to move along the positive 'forward' direction at a speed of 1.

It's a bit redundant for a top down game, so you have a couple of options:
1) Arbitrarily choose the view direction, and then just assign the movement direction in ways that accomplish the motion you want.
2) Always set the target velocity to (0,desiredSpeed) and then set the 3d view direction in the direction you want the character to move.

There is no single 'correct' way to do this, it's just arbitrary, so pick whatever is easiest.

parapoohda
Posts: 92
Joined: Fri May 31, 2019 6:30 am

### Re: Bepu v2 Character Controllers

Oh thank you. I think i will try second option.

parapoohda
Posts: 92
Joined: Fri May 31, 2019 6:30 am

### Re: Bepu v2 Character Controllers

I have 3 problem.

1 I tried option 1 that fix view direction axis. In main project it move correctly for a second before change direction. I try to build stand alone for you to look at. But not success to replicate problem. So today and maybe tomorrow I will try to replicate problem.

2 I in update character goal it have

Code: Select all

``````Debug.Assert(characterBody.Awake, "Velocity changes don't automatically update objects; the character should have already been woken up before applying air control.");
``````
which It throw error. It think it is because support is false. But what is bool support mean?

3 I tried to console writeline handle
So in cogufigure manifold i print

Code: Select all

``````Console.WriteLine(pair.A.Handle);
Console.WriteLine(pair.B.Handle);``````
But it show 0 is it working correctly. And also when add to simulation I tried to print handle but it show nothing

_(_ _) _ Thank you. I don't know what to say to appreciate your help

Norbo
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

### Re: Bepu v2 Character Controllers

It think it is because support is false. But what is bool support mean?
Support means the character is standing on something. That assert is meant to protect against modifications to the velocity when the character is asleep, since directly modifying velocities does not automatically wake bodies up. In the original version of that function, there's a section before the air control block that ensures the character is awake if anything relevant about the control state has changed.
But it show 0 is it working correctly. And also when add to simulation I tried to print handle but it show nothing
Printing zero may be correct- handles can be zero, and it's possible that a body with a handle of 0 is colliding with a static with a handle of 0. Bodies and statics can have overlapping handle values since they refer to different systems (similar to how constraint handles can overlap body or static handles).

parapoohda
Posts: 92
Joined: Fri May 31, 2019 6:30 am

### Re: Bepu v2 Character Controllers

Thank you.

parapoohda
Posts: 92
Joined: Fri May 31, 2019 6:30 am

### Re: Bepu v2 Character Controllers

After stan alone it seem player fall flat to the ground. I will post link to stand alone if I see it upload. Maybe I done something wrong with github.
update

this is stand alone

https://github.com/parapoohda/Why-it-is-fall-flat

Norbo
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

### Re: Bepu v2 Character Controllers

Nothing is stopping the body from falling over, so it does. For characters, a common approach is to set the inverse inertia to all zeroes; this gives it effectively infinite rotational inertia and it can't be rotated by any force anymore. So, in your AddCharacter function:

Code: Select all

``````            var characterShape = character.Shape();
characterShape.ComputeInertia(1, out var characterInertia);
characterInertia.InverseInertiaTensor = default;
Setting characterInertia.InverseInertiaTensor = default stops the character from falling.

Any other approach can work too- if you did want the character to rotate sometimes, using a constraint or velocities to control its orientation could work.

parapoohda
Posts: 92
Joined: Fri May 31, 2019 6:30 am

### Re: Bepu v2 Character Controllers

You are avery helpful. Thank you.

tomweiland
Posts: 99
Joined: Wed May 08, 2019 12:17 am

### Re: Bepu v2 Character Controllers

Hey, I just had another question about how the character controller works, specifically when it comes to standing on moving objects.

I've been writing a RigidBody player controller in Unity recently, and one thing I've noticed is that when the player stands on another RigidBody that's moving, it will just slide off, which is not what I want. Changing friction doesn't seem to help, and before I start raycasting down to check if the player is standing on a moving object (and adding the velocity of said object or something), I thought I'd ask how this works out of the box in Bepu.

Is players moving with whatever they're standing on just a result of how Bepu calculates things internally, or is there some kind of method that I could potentially apply in Unity as well?

Norbo
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

### Re: Bepu v2 Character Controllers

The character collects contacts from the body and checks if the source of those contacts is a body. If the chosen support is a body, then a two-body motion constraint is created between the character and the body.

The goal of that motion constraint is to reach the goal relative velocity between the character and support at the chosen support point in the support surface tangent space. The constraint applies an equal and opposite force to both the character and support body to try to reach that goal relative velocity.

So there's not really anything special about a character moving with its support- that's just the natural result of striving for a velocity relative to the support, rather than world space. In principle, you can do the same thing in unity, although solving the constraint does require doing a bit of math in the fully general case of dynamic supports capable of rotation.

tomweiland
Posts: 99
Joined: Wed May 08, 2019 12:17 am

### Re: Bepu v2 Character Controllers

Norbo wrote:
Wed Apr 22, 2020 7:27 pm
In principle, you can do the same thing in unity, although solving the constraint does require doing a bit of math in the fully general case of dynamic supports capable of rotation.
Hmm alright—that doesn't exactly sound trivial to implement, so I think I'll try to find a different solution...although I find it strange that something like this isn't built-in to Unity (there's questions about it all over Google ).

tomweiland
Posts: 99
Joined: Wed May 08, 2019 12:17 am

### Re: Bepu v2 Character Controllers

So upon some further investigation, it turns out that a Rigidbody in Unity does actually move with whatever it's standing on. The problem was just that my player movement code was overriding the player body's velocity without me realizing it (although it's super obvious in hindsight).

I would assume that similarly to Bepu there's some sort of internal constraint being created. So what I'm wondering now is how Bepu's character controller avoids overriding the velocity from its support. I've tried using similar code to the air movement (from earlier in this thread) for the ground movement, and although that prevents my player from sliding off the back of a moving object, I still can't move forward on it.

Short of using GetComponent to grab the Rigidbody of the support (which I was hoping to avoid doing every frame) and incorporating its velocity into the movement acceleration calculation, I really don't see a way of allowing the player to move normally on a moving object without overriding the velocity. How does Bepu controller pull this off? Is it just a benefit of how the constraints work or is there some extra magic going on?

I was a bit reluctant to ask here since I know you're probably busy and this isn't exactly Bepu-related (so no rush!), but I'm not sure where else to go—while the internet is full of similar questions about Unity, pretty much all of the answers involve making the player a child of the object he's standing on, which isn't really an option for me

Norbo
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

### Re: Bepu v2 Character Controllers

So what I'm wondering now is how Bepu's character controller avoids overriding the velocity from its support.
It targets a velocity relative to the support. This does require knowing the velocity of the support at the support point. In terms of the velocity constraint formulation, it's very similar to a friction constraint, except targeting arbitrary velocities rather than just zero.

To be more explicit, the constraint being satisfied can be written as:

Code: Select all

``````dot(worldVelocityOfSupportPointOnCharacter - worldVelocityOfSupportPointOnSupport, movementBasisX) = targetVelocityAlongBasisX
dot(worldVelocityOfSupportPointOnCharacter - worldVelocityOfSupportPointOnSupport, movementBasisY) = targetVelocityAlongBasisY``````
where

Code: Select all

``worldVelocityOfSupportPointOn______ = bodyAngularVelocity x (supportPoint - bodyCenter) + bodyLinearVelocity``
and the basis is a 2D orthonormal basis on the surface that the character is moving on. In the demos, the basis is created by projecting the character's view direction onto the support plane (support point + support normal) for the forward movement direction, then completing the basis by taking the cross product of the tangent forward direction and the support normal.

The disagreement between the computed constraint space velocity (left side) and the target velocity (right side) is the 'error', which the constraint corrects by applying impulses. There are some subtleties remaining in how the impulses are derived (how much impulse is applied to each body to reach the relative velocity goal), but that's the core idea.

The specific constraint in question can be found here, although it is a bit dense due to vectorization and whatnot: https://github.com/bepu/bepuphysics2/bl ... nt.cs#L441