Bepu v2 Character Controllers

Discuss any questions about BEPUphysics or problems encountered.
Norbo
Site Admin
Posts: 4722
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu v2 Character Controllers

Post by Norbo » Sun Jun 23, 2019 9:57 pm

a link to a demo that deals with PointOnLineServos specifically
The PointOnLineServo is used in the CarDemo for wheel suspensions and in the ConstraintTestDemo for isolated tests.
Do the bodies need to be within a certain distance of each other in order to be able to link up with the constraint?
Nope, but if there's a lot of constraint violation you could end up with a rather noticeable jerk as it pulls the character into position.

Code: Select all

servo = new PointOnLineServo()
{
    LocalDirection = new Vector3(0, 1, 0),
    ServoSettings = new ServoSettings(1, 1, 50),
    LocalOffsetA = new Vector3(0, -4, 0),
    LocalOffsetB = new Vector3(0, 5, 0),
};
This doesn't assign the SpringSettings to anything which leaves it with zero-initialized values. Zero stiffness isn't a valid constraint configuration, so it causes an NaNsplosion.

The SpringSettings type itself catches this kind of incorrect configuration, but since the constructor isn't actually being run it doesn't have a chance. I'm going to add some validation to the constraint description ApplyDescription functions to act as a second line of defense and to catch configuration errors sooner.

The LocalOffsets seem a little bit suspicious too, even though they won't cause a NaN. Each LocalOffset is the point on the body that will be constrained to the line, so if the character is body A, then it will have an attachment point 4 units down along its local Y axis.

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

Re: Bepu v2 Character Controllers

Post by tomweiland » Sun Jun 23, 2019 10:36 pm

Norbo wrote:
Sun Jun 23, 2019 9:57 pm
This doesn't assign the SpringSettings to anything which leaves it with zero-initialized values. Zero stiffness isn't a valid constraint configuration, so it causes an NaNsplosion.
After some more testing I managed to figure this one out. There's no longer an error being produced, but now nothing happens at all.
Norbo wrote:
Sun Jun 23, 2019 9:57 pm
The LocalOffsets seem a little bit suspicious too, even though they won't cause a NaN. Each LocalOffset is the point on the body that will be constrained to the line, so if the character is body A, then it will have an attachment point 4 units down along its local Y axis.
I may have understood how these are designed to work. I assumed that the local offsets defined the start and end of the "ladder". So if I wanted a ladder to go from -4 to 5 (in local space), I would set them up like I did. Is that wrong?

Also, is it possible to attach anywhere along the ladder? Say if I wanted to allow the player to jump and grab a ladder midair, would attaching him in the middle also work, or can I only attach him at one of the two ends?

Norbo
Site Admin
Posts: 4722
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu v2 Character Controllers

Post by Norbo » Sun Jun 23, 2019 10:53 pm

I may have understood how these are designed to work. I assumed that the local offsets defined the start and end of the "ladder". So if I wanted a ladder to go from -4 to 5 (in local space), I would set them up like I did. Is that wrong?
The local offsets describe the offset from each body to their own attachment point to the line. Points on the line can be expressed as: LocalOffsetA * orientationA + positionA + (LocalDirection * orientationA) * t, for any value of t.

The attachment on body B will not be pulled to a specific point on the line, just the closest one. The constraint's only job is to ensure that the attachment points are somewhere along the line. In other words, the PointOnLineServo controls only two degrees of freedom (the axes perpendicular to the line axis), leaving one linear degree of freedom along the line axis. It will not resist motion along the line at all.

If you'd like to control where along the line an object is, the LinearAxisMotor or LinearAxisServo (depending on if you want a velocity or position target) would be needed. The LinearAxisLimit can also bound how far an object can move along the line. Note that the LinearAxis* constraints are not fundamentally related to the PointOnLineServo, they just happen to be a natural complement to it since they control one linear degree of freedom.

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

Re: Bepu v2 Character Controllers

Post by tomweiland » Sun Jun 23, 2019 11:29 pm

So I switched the PointOnLineServo out for a LinearAxisMotor:

Code: Select all

motor = new LinearAxisMotor()
{
    LocalOffsetA = new Vector3(0, 0, 0),
    LocalOffsetB = new Vector3(0, 0, -1),
    LocalAxis = new Vector3(0, 1, 0),
    TargetVelocity = 5,
    Settings = new MotorSettings(100, 1)
};
The rest of the setup remains the same, but when I attempt to attach to the ladder, nothing happens. I'm still able to move like normal, and it seems like there's no other forces in play. Could the character's motion constraints be interfering somehow? If so, what's the best way to disable/re-enable them?

Norbo
Site Admin
Posts: 4722
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu v2 Character Controllers

Post by Norbo » Sun Jun 23, 2019 11:40 pm

Code: Select all

new MotorSettings(100, 1)
A softness of 1 is extremely soft. Softness is equivalent to the inverse damping for a motor. Higher softness values allow the constraint to be violated more. Zero is perfectly rigid. If you want a pretty rigid response, try values near zero like 0.001.

Also, note that the LinearAxisMotor only controls one linear degree of freedom. Without a PointOnLineServo, the character will be completely free to roam perpendicular to the axis.
Could the character's motion constraints be interfering somehow? If so, what's the best way to disable/re-enable them?
Yes, the character constraints will interfere unless disabled. Whether that interference matters is another question. Making the PointOnLineServo strong enough to overpower the character's MaximumHorizontalForce and the LinearAxisMotor strong enough to overpower the character's MaximumVerticalForce is one option. (You might find that reducing the MaximumVerticalForce helps here, although that will make the character more prone to going airborne when walking off bumps.)

If you want to fully disable the character constraints, it would require a modification in the CharacterControllers. There's no built in support for that sort of thing, so you'd just need to do some custom work. Possibly a "DisableMotionConstraints" flag on the Character that prevents the character from generating motion constraints in AnalyzeContactsForCharacterRegion.

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

Re: Bepu v2 Character Controllers

Post by tomweiland » Mon Jun 24, 2019 12:05 am

Norbo wrote:
Sun Jun 23, 2019 11:40 pm
Yes, the character constraints will interfere unless disabled. Whether that interference matters is another question. Making the PointOnLineServo strong enough to overpower the character's MaximumHorizontalForce and the LinearAxisMotor strong enough to overpower the character's MaximumVerticalForce is one option. (You might find that reducing the MaximumVerticalForce helps here, although that will make the character more prone to going airborne when walking off bumps.)
My character's MaximumVerticalForce is set to 100 and I'v changed the MotorSettings to look like this:

Code: Select all

new MotorSettings(1000, 0.001f);
Surely that should be enough to overpower the vertical force...right? I'm still not seeing anything happen, no upwards movement at all. I'd like to just get the motor working for now, the servo can wait.

Also, if I were to simply disable input while the player is on the ladder, I don't think I'd even need the servo (assuming the ladder doesn't move).

Norbo
Site Admin
Posts: 4722
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu v2 Character Controllers

Post by Norbo » Mon Jun 24, 2019 12:55 am

Surely that should be enough to overpower the vertical force...right? I'm still not seeing anything happen, no upwards movement at all.
How about downwards movement? :P
https://github.com/bepu/bepuphysics2/co ... 9f97fa26d1

The LinearAxisMotor.TargetVelocity had an unintuitive sign. I flipped it around so that an axis of (0,1,0) attached to A with a positive TargetVelocity will make B go in the positive direction along A's axis.

Hopefully not too many existing projects get busted by that change, but I think the behavior was odd enough to warrant it.

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

Re: Bepu v2 Character Controllers

Post by tomweiland » Mon Jun 24, 2019 11:38 pm

Norbo wrote:
Mon Jun 24, 2019 12:55 am
How about downwards movement? :P
https://github.com/bepu/bepuphysics2/co ... 9f97fa26d1
Yep, it's working now! I should be able to take it from here, but I have one more question.
Is it possible to use a specific piece of a compound body as the "ladder" collider? I'd need to get the handle of that specific part, but since the compound as a whole has one handle, I suspect this will be problematic. If I can't do that, what would be the best way to make a separate dynamic body behave as though it's a part of the compound (position and orientation change accordingly, I don't care about the center of mass)?

Norbo
Site Admin
Posts: 4722
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu v2 Character Controllers

Post by Norbo » Tue Jun 25, 2019 10:31 pm

It depends on what you mean- if you're casting a ray to find the collider, the Simulation.RayCast function doesn't distinguish between compound children, but there's nothing special about Simulation.RayCast. It's a convenience function that can be bypassed for more custom work. As a replacement, you can use the BroadPhase.RayCast function to find collidables with intersecting bounding boxes, then do child-aware casts on any hit children.

That said, child-aware casts are common enough that I'll probably add a path for them.

If you mean in response to collisions, there's a INarrowPhaseCallbacks.ConfigureContactManifold which passes the children involved in a compound collision.

If you're concerned only with attaching constraints to the child, then it's equivalent to attaching to the parent compound since the child is associated with the same body. It would just require taking into account the child's relative location.

There's no way to achieve the absolutely rigid relationship of compound children without bundling the children into the same shape. The Weld constraint, however, can approximate it.

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

Re: Bepu v2 Character Controllers

Post by tomweiland » Tue Jun 25, 2019 11:44 pm

Norbo wrote:
Tue Jun 25, 2019 10:31 pm
If you're concerned only with attaching constraints to the child, then it's equivalent to attaching to the parent compound since the child is associated with the same body. It would just require taking into account the child's relative location.
That's what I had in mind, and I suspected this would be the case.

As always, thank you for the help!

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

Re: Bepu v2 Character Controllers

Post by tomweiland » Wed Jun 26, 2019 1:50 am

Since my compound isn't entirely symmetrical, the center of mass is slightly offset, which makes positioning the ladder in the middle a bit of a pain (especially since I'll have to adjust it every time I modify the compound).

Is there any way to pass a shape with a mass of 0 to CompoundBuilder.Add()? That way I could set the mass of all the pieces to 0 except for one which I can use to define the center. Or maybe there is already a way to give the compound a center of mass without it being calculated?

Norbo
Site Admin
Posts: 4722
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu v2 Character Controllers

Post by Norbo » Wed Jun 26, 2019 10:24 pm

Is there any way to pass a shape with a mass of 0 to CompoundBuilder.Add()
Yes, just not the Add overload that takes a IConvexShape parameter and computes an inertia tensor for itself. You can also bypass the CompoundBuilder convenience type entirely and just set up your own children list and inertia directly.
Or maybe there is already a way to give the compound a center of mass without it being calculated?
BuildDynamicCompound has two overloads. One outputs the computed center, the other does not. The one that outputs a center computes the weighted average of all child centers and recenters them. The one that does not output a center leaves the child poses unmodified. In other words, if you use the overload that does not output a center, then the center of mass is just the origin in the local space that you defined child poses in.

However, changing the center of mass will change how the compound rotates, so it might produce undesirable results. Instead, you can use the BuildDynamicCompound's outputted center to help position things precisely. The recentering process is just a simple translation, so you can easily compute where things would have been without the recentering.

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

Re: Bepu v2 Character Controllers

Post by parapoohda » Fri Jul 12, 2019 10:59 am

Norbo wrote:
Fri May 24, 2019 9:47 pm
Conceptually, the handle is the identity of of the character, while the index is just a location in memory. It's the difference between saying "hey John, come here" and "hey you in the 7th chair from the left, come here". People might swap chairs over time. If you want to reliably beckon John, you should use his name rather than where he was sitting at some arbitrary time. But if you really do just want the person in the 7th chair from the left, then you can do that too.
I want to add many players to scene. Where I can find handle of the character. I want to keep handle in class character which is in dictionary of class map. When I add character to scene where i can find handle.

Thank you.

Sorry for my bad English. It isn't my first language.

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

Re: Bepu v2 Character Controllers

Post by parapoohda » Fri Jul 12, 2019 11:16 am

ok i find it

bodyHandle = characters.Simulation.Bodies.Add(BodyDescription.CreateDynamic(initialPosition, new BodyInertia { InverseMass = 1f / mass }, new CollidableDescription(shapeIndex, speculativeMargin), new BodyActivityDescription(shape.Radius * 0.02f)));

in CharacterDemo.cs

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

Re: Bepu v2 Character Controllers

Post by parapoohda » Tue Jul 23, 2019 1:09 am

I try to make game with server. I try to make point and click movement. Now I want to know is what is character controller advantage. So character controller only make better quality of life? (Easier coding) Or it is have other advantage.

Now according to my understandings of code and this topic I think I can manipulate velocity and view direction directly. And character controller also contains fast search for character.

Thank you.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests