Best way to make something jump?

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
MuletTheGreat
Posts: 8
Joined: Thu May 06, 2010 7:30 am

Best way to make something jump?

Post by MuletTheGreat »

Hey,

I'm just working on something basic, and am trying to get a player controlled sphere to jump off a surface. I have a triangle mesh loaded up, as well as a few boxes and ball entities around. I want my ball to jump, while in contact with the ground, so when it leaves, it cannot jump and thus will not fly.

This is the simple jump code.

Code: Select all


        public void Jump(Entity sender, Entity other, CollisionPair collisionPair)
        {
            if (g_Controller.GET.IsButtonDown(Button.RB))
            {
                g_NetworkManager.GET.ApplyForce(g_Constants.NetworkControllingPlayer, JumpForce);
            }
        }

I use the events system each entity seems to have to hook this up.

Code: Select all


        public void BuildDefaultBall()
        {
            this.BodyPhysics = new Sphere(MotionState.defaultState, g_Constants.PlayerMarbleRadius,  g_Constants.GET.MarbleSettings.MarbleMass);
            BodyPhysics.eventManager.addEventHook(new EventHandlerCollisionPairCollidingImmediate(Jump));
            ...

But when I use this code, I am able to fly, One single collision with something then anytime I press RB I jump regardless of contact with the ground. Tried the no immediate event as well, and same effect.

Also, after I get this working I would like to take into account the normal of the collision, so that when jumping off a slope, I will not leap straight up.

Anyway, any assistance would be appreciated.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Best way to make something jump?

Post by Norbo »

Given that in the future you want to check a collision normal and that you'll probably have to consider multiple collision pairs in the process (to pick the 'best' support), I'd recommend not using an entity collision event. Instead, on the input event of pressing the jump button, you can examine the player controlled sphere's collision pair list. If a collision pair has more than zero contacts, it is 'colliding.' You could do further tests to decide if it is not only colliding, but also 'supporting.' If there are no supporting contacts, then the sphere should not be able to jump.

As an extra test example, if you want jumping to mainly push you upwards (instead of being able to jump down off ceilings and that sort of thing), you could verify that the contact point is below the center position of the sphere. This would keep it from jumping 'up' into the support and hitting its head instantly. The contact class also has a normal that you could analyze.

However, in your example, the ball shouldn't be able to fly. The colliding event should only ever be issued if there exists a collision pair between that entity and another that has more than zero contact points. If it's truly flying in the air, it shouldn't be called. Did you happen to take a look at what it was supposedly colliding with? There could be something a little strange going on.
Post Reply