Help Integrating bEPU

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Firefly09
Posts: 10
Joined: Mon Feb 15, 2010 6:23 pm

Help Integrating bEPU

Post by Firefly09 »

Hi,

I am trying to switch from my own collision system to bEPU but I am having some problems. I am trying to get my Character to collide with my mesh level but it just isn't working as expected. My character slides all over the place, when he jumps he only moves very little, and he gets stuck against my level. This is the first time iv'e tried using a 3D physics library so I am unsure of what to do. I have just taken the code from the examples and added it in the necessary places. Any advice or links to articles on how to integrate bEPU would be much appreciated!!

Thanks,

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

Re: Help Integrating bEPU

Post by Norbo »

Most likely I'm going to need some more information about the situation. The more specific the question, the better I can answer it.

If you are seeing generally poor collision behavior, a common cause is excessive scale. The engine likes things to be in a certain size range; between 0.5 units to 10 units in each dimension for an entity works well. You can go beyond this soft suggested range, but massive 500x500 triangles will make numerical errors noticeable.

The reason for this is mostly the general case collision detection system. It is more sensitive to error than some other systems, and it's best to make it happy. A little more information can be found in the "Applying Physics Engines" presentation on the documentation page (http://www.bepu-games.com/BEPUphysics/documentation.htm)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Help Integrating bEPU

Post by Norbo »

Note that each triangle in a StaticTriangleGroup is an entity, and should follow the scale guidelines as well. Overscaled world geometry is usually the culprit in these situations.
Firefly09
Posts: 10
Joined: Mon Feb 15, 2010 6:23 pm

Re: Help Integrating bEPU

Post by Firefly09 »

Thanks for your reply,

I managed to fix the problem with my player getting stuck in walls (used CharacterController instead of SimpleCharacterController, not sure why this fixed it). My problem is when I stop holding down the forwards key is instead of my player stopping he slows down gradually but not fast enough which can cause him to slide off of the level, can I make him slow down faster? Also when I walk into the walls of my level my character slows down to almost a stop instead of sliding along them, can I make him slide along the walls faster? One more thing, is it possible to make the player move while he is in mid air, so that when he jump's the player can alter his position slightly while he is in the air?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Help Integrating bEPU

Post by Norbo »

can I make him slow down faster?
The character controller's tractionDeceleration field controls how fast a character can stop.
can I make him slide along the walls faster
You can change the character body's friction (dynamicFriction/staticFriction) to make it slide easier. By default, the system will average the wall's friction with the body's friction. If you want it to have absolutely no friction, you could attach a EventHandlerCollisionPairCreated to the character body entity that sets the collision pair's dynamicFriction/staticFriction value to zero.
is it possible to make the player move while he is in mid air, so that when he jump's the player can alter his position slightly while he is in the air?
It is possible; you'll need to modify the character controller. In the updateAtEndOfUpdate method, there is an if statement. The first block is for when the character has something to stand on. The second block (currently containing isSupported = false; hasTraction = false;) is when the character is in the air. Just like how the first block calls handleHorizontalMotion, you could create a method to handleAirborneMotion.
Firefly09
Posts: 10
Joined: Mon Feb 15, 2010 6:23 pm

Re: Help Integrating bEPU

Post by Firefly09 »

Ok, I have tried changing tractionDeceleration to different values but my player still looks and feels like he is walking on ice. I really would like it so he behaves like he did before using the character controller, when the stick is forward : walk forward. when the stick is centred : stop moving etc. without accelerating to terminal velocity or slowly de-accelerating before coming to a stop. Have you got any other suggestions on which variables I could alter?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Help Integrating bEPU

Post by Norbo »

It sounds like there's a problem. If you set tractionDeceleration arbitrarily high (float.MaxValue), it will let the system apply immense forces which should instantly stop any unwanted motion. If you're observing otherwise, something is not working right. I suggest putting in a breakpoint and triggering it when your character is trying to move around. Check to see if it has support and if it has traction.
Firefly09
Posts: 10
Joined: Mon Feb 15, 2010 6:23 pm

Re: Help Integrating bEPU

Post by Firefly09 »

Setting it to float.MaxValue made my player stop immediately however it has not fixed the problem I am having with my player sliding sidewise when he turns too fast.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Help Integrating bEPU

Post by Norbo »

The traction deceleration should remove all horizontal velocity not in the current horizontal direction of movement. If after changing the traction deceleration to MaxValue you still see movement in horizontal directions not aligned with your current goal movement direction, something is wrong.

If, however, you just want your character to speed up faster in the current movement direction, you can increase the character's acceleration field.
Firefly09
Posts: 10
Joined: Mon Feb 15, 2010 6:23 pm

Re: Help Integrating bEPU

Post by Firefly09 »

ok, I think I have got my character to move in a way that I am happy with. One thing I am struggling with is implementing a handleAirborneMotion method. I am not sure what I should be doing in this method. Should it be similar to handleHorizontalMotion but without the support stuff?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Help Integrating bEPU

Post by Norbo »

It'll be similar in concept, but quite a lot simpler. Basically, you should decide on the kind of behavior you want. Do you want to have a maximum 'airborne velocity?' Do you want to have a smooth acceleration up to that velocity?

For the basics, you can just consider the X and Z components of velocity since Y is up and down. If you take the dot product of that two dimensional vector and the 2d movement direction, you get the current speed your character is moving in the movement direction.

If you want to use an acceleration and maximum speed scheme, you can compute a velocity correction which will accelerate the character towards the goal without exceeding the maximum speed. If you had an 'airborne acceleration', you could multiply it by the timestep (dt).

Before you multiply the frame acceleration by the 3d movement direction (movementDir2D.X, 0, movementDir2D.Y), you should ensure that your acceleration won't exceed your maximum velocity. You can do this like so:

Code: Select all

previousSpeed = speed;
speed = Math.Min(maximumAirborneSpeed, speed + airborneAcceleration * dt);
float changeInSpeed = speed - previousSpeed;
Then you can just apply the change in velocity:

Code: Select all

body.internalLinearVelocity += changeInSpeed * new Vector3(movementDir2D.X, 0, movementDir2D.Y);
This is just one approach; there's lots of different ways you could do it depending on what kind of behavior you want.
Firefly09
Posts: 10
Joined: Mon Feb 15, 2010 6:23 pm

Re: Help Integrating bEPU

Post by Firefly09 »

Hi,

I have been playing around with some of the things you have suggested but I'm still not able to get my character to move in the way I want it to. I'm not sure if you have ever played any of the jack and daxter games, but the way jack moves in those games is what I am trying to achieve in mine (http://www.youtube.com/watch?v=onGtvO8C ... re=related) but I just can't seem to make my character move like that now that I'm using bEPU. Any more suggestions on how to achieve this type of movement would be much appreciated!

Thanks for all your help,

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

Re: Help Integrating bEPU

Post by Norbo »

Try this implementation of handleAirborneMotion:

Code: Select all

        /// <summary>
        /// The maximum speed that the character can achieve by itself while airborne.
        /// A character can exceed this by launching off the ground at a higher speed, but cannot force itself to accelerate faster than this using air control while airborne.
        /// </summary>
        public float maximumAirborneSpeed = 2;
        /// <summary>
        /// Change in airborne speed per second.
        /// </summary>
        public float airborneAcceleration = 30;
        /// <summary>
        /// Manages the character's air control.
        /// </summary>
        /// <param name="dt">Timestep of the simulation.</param>
        void handleAirborneMotion(float dt)
        {
            //Compute the current horizontal speed, two dimensional dot product.
            float speed = body.internalLinearVelocity.X * movementDirection.X + body.internalLinearVelocity.Z * movementDirection.Y;

            float previousSpeed = speed;
            speed = Math.Min(maximumAirborneSpeed, speed + airborneAcceleration * dt);
            float changeInSpeed = MathHelper.Max(0, speed - previousSpeed);

            body.internalLinearVelocity += new Vector3(changeInSpeed * movementDirection.X, 0, changeInSpeed * movementDirection.Y);
        }
And call it from the updateAtEndOfUpdate like this:

Code: Select all

...
            }
            else
            {

                isSupported = false;
                hasTraction = false;
                handleAirborneMotion(dt);
            }
...
I can't really tell the exact behavior you're going for in that video. In order for you to program it, you will need to first figure out the precise rules that govern the motion. It looks like most of it can be handled by the above airborne acceleration/maxspeed model.
Firefly09
Posts: 10
Joined: Mon Feb 15, 2010 6:23 pm

Re: Help Integrating bEPU

Post by Firefly09 »

Hi,

Thanks for that, this has helped greatly. Now I am noticing a problem (both on ground and in air) that whenever my player jumps against a wall he get's stuck against it or slides around it a bit. Hmm, I don't think that I should be having all of these problems and I think I'm getting them because of my own incompetence. Maybe bEPU is overkill for what I'm trying to do. What do you think? I am trying to create a simple 3D platformer.

Thanks,

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

Re: Help Integrating bEPU

Post by Norbo »

It depends. The simpler the game, the less reason to use a physics engine. If you just wanted to hop around some simple predictable geometry levels, then you don't really need a physics engine.

If you wanted to have boxes or other shapes bouncing around, 3D 'realistic' physics puzzles, and generally other things a physics engine is good for, then a physics engine can certainly help.

Keep in mind that most character controllers are implemented in a very non-physical way. The open source character controller in the demo uses a dynamic body to help out with some collision detection and response, but many approaches to character controllers have no physical body. Instead they explicitly program every desired interaction. So, you could use a very simple character controller that does what you want in a physics engine as well. You would just need to program the interaction between the world and character in the cases where your game needed it.

As for the latest problem you're seeing, it could be a scaling issue like I outlined in the first post. The character controller also has some specific behavior issues that can sometimes crop up where it can 'feel' the boundary between two triangles composing a wall. The SimpleCharacterController does not have that particular issue, but is overall less powerful. It may be that the SimpleCharacterController does everything you need, though. Its concept could also be easily applied without a physics engine since it's just a shape floating on a raycast. The powerpoint mentioned in the first post goes over character controllers a little, too.
Post Reply