Games [XUI2D] JumpTest Physics

Gunther

Active Member
Licensed User
Longtime User
Hi,

I was playing around with jumping of characters of the examples. I want to create a better feeling/looking jump physics instead of giving the character just an impuls and wait what will happen. I splitted the inair part in two sections and modified the body gravity in the parts:

1) movement up
2) movement down

In addition I put in the possiblility to give the character a higher jump if a long press is done

3) movement up is haveing two body gravity values

When the player is pressing we don't know if this will be only a short press or a long press, therefore we cannot use a different impuls value for a low or high jump. So we change the gravity underway.

JumpTest.PNG


Finally I came up with the attached Test.

B4X:
Public Sub Tick (GS As X2GameStep)
 
    ' push the ball if pressed 
    If x2.mGame.Jump = True Then
        x2.mGame.Jump = False
        bw.Body.ApplyLinearImpulse(x2.CreateVec2(0, impulsValue), bw.Body.Position)
        x2.SoundPool.PlaySound("wing")
    End If
     

    '
    ' ******************************************************************
    ' *  Modify the BodyGravity for nicer Jumps                        *
    ' *                                                                *
    ' *  1) jump upwards and downwards have different Gravity settings *
    ' *  2) shot press done with normal Gravity = normal jumps         *
    ' *  3) long press done Gravity will be lower = higher jumps       *
    ' *                                                                *
    ' ******************************************************************
    '
    ' Gravity normal = normal jump hight
    bw.mWorld.Gravity.Set(0,-10)
    '
    If bw.Body.LinearVelocity.Y < 0 Then ' are in up- or down direction?
        ' body is in falling direction = raise of Gravity
        bw.mWorld.Gravity.Set(0 , bw.mWorld.Gravity.Y * fallMultiplier)
        '
    Else if bw.Body.LinearVelocity.Y > 0 And (x2.mGame.Up = False) Then ' in up-direction and long pressing
        ' Gravity lower = higher Jump
        bw.mWorld.Gravity.Set(0 , bw.mWorld.Gravity.Y / highJumpMultiplier )
    Else
        ' non of all = normal Gravity remains (this if-part can be removed)
    End If
    '
    bw.Body.LinearVelocity = x2.CreateVec2(0, Min(4, bw.Body.LinearVelocity.Y))
    '
    If GS.ShouldDraw Then
        bw.UpdateGraphic(GS, True)
    End If
 
End Sub
[code]
 

Attachments

  • JumpTest.zip
    42.3 KB · Views: 210
Last edited:

Gunther

Active Member
Licensed User
Longtime User
Why? You should let the physics engine do its work. What was the problem with the simple jump behavior?
The jump by setting an impuls is fine, but in some cases this parable jump looks and feels not that dynamically as it could.

Like the Mario Jump in the original Version is also not a symetrically parable. The up direction takes 18 frames and the down 10 frames.

Well, it is only a matter of taste except the part of higher jumping with long press.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
The up direction takes 10 frames and the down 8 frames.
This is most probably related to the linear damping which causes it to slow down.
You can play with it. It is set to 1 which is quite high in the example (to cause Mario to stop quickly).

Changing the world gravity to change the jumping behavior is not the correct approach. You need to trust the physics engine and let it do its work.
Setting the linear velocity is also not recommended.

It takes time to get used to box2d. You need to build the scenario, configure the parameters and then trust it to simulate the physical world as best as possible.
 

Gunther

Active Member
Licensed User
Longtime User
I mixed the frame figures in my reply but I corrected them to 18 up and 10 down. In addition I meant the really original Mario Version not the one out of the Examples. This is a symetrically jump there.

I didn't changed the World Gravity, only for the Charater, correct? Since the World should run as expected.
B4X:
bw.mWorld.Gravity.Set(0 , bw.mWorld.Gravity.Y * fallMultiplier)

Here the Jump from original MarioBros.:
MarioJump.PNG
 
Last edited:

Gunther

Active Member
Licensed User
Longtime User
Oh, ok. Thanks for that correction and advices.

So, I changed the ball.bas according to your hints:

B4X:
Sub Class_Globals
    Public bw As X2BodyWrapper
    Private x2 As X2Utils 'ignore
    Dim impulsValue As Float = 1.5
    Dim FallGravityScale As Float = 2.4
    Dim HighJumpGravityScale As Float = 0.5
End Sub

Public Sub Initialize (wrapper As X2BodyWrapper)
    bw = wrapper
    x2 = bw.X2
End Sub

Public Sub Tick (GS As X2GameStep)
 
    ' push the ball if pressed 
    If x2.mGame.Jump = True Then
        x2.mGame.Jump = False
        bw.Body.ApplyLinearImpulse(x2.CreateVec2(0, impulsValue), bw.Body.Position)
        x2.SoundPool.PlaySound("wing")
    End If
    '
    ' ******************************************************************
    ' *  Modify the BodyGravity for nicer Jumps                        *
    ' *                                                                *
    ' *  1) jump upwards and downwards have different Gravity settings *
    ' *  2) shot press done with normal Gravity = normal jumps         *
    ' *  3) long press done Gravity will be lower = higher jumps       *
    ' *                                                                *
    ' ******************************************************************
    '
    '
    If bw.Body.LinearVelocity.Y < 0 Then ' are in up- or down direction?
        ' body is in falling direction = raise of Gravity
        bw.Body.GravityScale = FallGravityScale
        '
    Else if bw.Body.LinearVelocity.Y > 0 And (x2.mGame.Up = False) Then ' in up-direction and long pressing
        ' Gravity lower = higher Jump
        bw.Body.GravityScale = HighJumpGravityScale
    Else
        ' non of all
        ' Gravity normal = normal jump hight
        bw.Body.GravityScale = 1
    End If
    '
    '
    If GS.ShouldDraw Then
        bw.UpdateGraphic(GS, True)
    End If
 
End Sub
 

Attachments

  • JumpTest 2.zip
    42.3 KB · Views: 229
Last edited:
Top