Games [XUI2D] JumpTest Physics

Discussion in 'Game Development' started by Gunther, Oct 17, 2018.

  1. Gunther

    Gunther Active Member Licensed 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.

    Code:
    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 = FalseThen ' 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(0Min(4, bw.Body.LinearVelocity.Y))
        
    '
        If GS.ShouldDraw Then
            bw.UpdateGraphic(GS, 
    True)
        
    End If
     
    End Sub
    [code]
     

    Attached Files:

    Last edited: Oct 18, 2018
  2. Erel

    Erel Administrator Staff Member Licensed User

    Why? You should let the physics engine do its work. What was the problem with the simple jump behavior?
     
  3. Gunther

    Gunther Active Member Licensed User

    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: Oct 18, 2018
  4. Erel

    Erel Administrator Staff Member Licensed User

    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.
     
  5. Gunther

    Gunther Active Member Licensed 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.
    Code:
    bw.mWorld.Gravity.Set(0 , bw.mWorld.Gravity.Y * fallMultiplier)
    Here the Jump from original MarioBros.:
    MarioJump.PNG
     
    Last edited: Oct 18, 2018
  6. Erel

    Erel Administrator Staff Member Licensed User

    You are changing the gravity of the complete world.

    You should instead change bw.Body.GravityScale.
     
  7. Gunther

    Gunther Active Member Licensed User

    Oh, ok. Thanks for that correction and advices.

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

    Code:
    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 = FalseThen ' 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
     

    Attached Files:

    Last edited: Oct 18, 2018
    Alexander Stolte and Erel like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice