# Games[XUI2D] JumpTest Physics

#### Gunther

##### Active Member
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.

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: 186
Last edited:

#### Erel

##### B4X founder
Staff member
I want to create a better feeling/looking jump physics instead of giving the character just an impuls and wait what will happen.
Why? You should let the physics engine do its work. What was the problem with the simple jump behavior?

#### Gunther

##### Active Member
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
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
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.:

Last edited:

#### Erel

##### B4X founder
Staff member
I didn't changed the World Gravity, only for the Charater, correct?
You are changing the gravity of the complete world.

#### Gunther

##### Active Member
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: 201
Last edited:

Replies
10
Views
4K
Replies
96
Views
11K
Replies
11
Views
4K
Replies
23
Views
3K
Replies
14
Views
4K