Trouble with bounce

Hello, we are having some trouble with setting up character bounce equally off the platform. Just setting character  bounce does make him bounce but it doesn`t bounce at equal height.

Here is the code from physics, platform and character setup

[ local physics = require(“physics”)
–physics.setScale(40)
    physics.start()
    physics.setGravity(0, 50)
    physics.setPositionIterations( 20 )
    physics.setVelocityIterations( 8 )

local function heroJump(event)

    if ( hero.inJump == false and event.phase == “began” ) then
        hero.inJump = true
        vx = (event.x - hero.x) / 13
        hero:applyLinearImpulse( vx, -115, hero.x, hero.y )

    elseif (event.phase == “ended”) then

    end
end

local function makePlatform()
    local platform = display.newImageRect(“woodplatform.png”, 300, 32)
    platform.isSensor = true
    platform.collType = “passthru”
    --platform.isVisible=true
    physics.addBody( platform, “static”, { density=1.0, bounce=0.2, friction=1} )
    return platform
end

hero = display.newImageRect(“egg5.png”, 80 , 100)
        hero.x = centerX
        hero.y = screenH - 180
        physics.addBody( hero, “dynamic”, { radius=40, density=1, friction = 0.7, bounce = 0.8 })
        hero.isFixedRotation = true
        --hero.linearDamping = 25
        hero.isSensor = false
        hero.inJump = false
        gameGroup:insert(hero)

function hero:collision( event )
        local vx,vy = self:getLinearVelocity()
        if ( event.phase == “began” ) then
            local collType = event.other.collType
            if ( vy >= 0 and self.inJump == true ) then self.inJump = false end
                if ( collType == “enemy” ) then
                    self.inJump = true
                    print(“gfdgh”)
                    transition.cancel()
                end
                self:applyLinearImpulse( nil, vy*0.0001, self.x, self.y )
                if ( self.setAsSensor == true ) then
                    if ( collType == “solidV” ) then self:setLinearVelocity( -vx,vy )
                    elseif ( collType == “solidH” ) then self:setLinearVelocity( vx,-vy*0.5 )
                    elseif ( collType == “enemy” ) then self:setLinearVelocity( -vx,-vy )
                    end
                end

]

So is there something we overlooked.

Hi @igor_deletic,

Can you please be more specific on what is happening? How is the bounce inconsistent? You are setting the linear velocity in some cases, but applying linear impulse in other areas. These are actually different methods from a physics standpoint. setting the velocity is setting a direct, immediate, constant velocity in a direction. Applying an impulse is like putting a force on the object from a direction, and thus it must “fight against” any current force that the object already has.

Anyway, please describe in more detail what is occurring and what the desired behavior should be.

Take care,

Brent

Thank you for replaying. Yea I just thought also that linear velocity and linear impulse might collide. Just dont know what to do. Basically we want to have same jump when character jumps on platform like doodle jump. In our case when our character jumps on platform he just jumps slightly from platform then gradually jump is increasing, or when he falls from height down he bounce off very high. So he doesnt have consistent bounce whether he jumps to platform or falls from higher ground to platform. I hope I was able to explain this properly.

Hi @igor_deletic,

Yes, this is expected. When the character has a higher velocity downward, and you apply an impulse upward (to jump), that impulse is reduced because it must attempt to oppose the downward velocity. In contrast, if the player is standing in place, you will get a much higher jump because there is no opposing downward velocity.

The typical solution is to set the character’s Y linear velocity to 0 when it hits a platform. Then, apply the impulse after a very short timer (10 milliseconds or so).

[lua]

local vx,vy = self:getLinearVelocity()

self:setLinearVelocity( vx, 0 )

– Execute your jump function after 10 milliseconds

– This ensures that the physics engine has time to perform its calculations

timer.performWithDelay( 10, heroJump )

[/lua]

Brent

Hi @igor_deletic,

Can you please be more specific on what is happening? How is the bounce inconsistent? You are setting the linear velocity in some cases, but applying linear impulse in other areas. These are actually different methods from a physics standpoint. setting the velocity is setting a direct, immediate, constant velocity in a direction. Applying an impulse is like putting a force on the object from a direction, and thus it must “fight against” any current force that the object already has.

Anyway, please describe in more detail what is occurring and what the desired behavior should be.

Take care,

Brent

Thank you for replaying. Yea I just thought also that linear velocity and linear impulse might collide. Just dont know what to do. Basically we want to have same jump when character jumps on platform like doodle jump. In our case when our character jumps on platform he just jumps slightly from platform then gradually jump is increasing, or when he falls from height down he bounce off very high. So he doesnt have consistent bounce whether he jumps to platform or falls from higher ground to platform. I hope I was able to explain this properly.

Hi @igor_deletic,

Yes, this is expected. When the character has a higher velocity downward, and you apply an impulse upward (to jump), that impulse is reduced because it must attempt to oppose the downward velocity. In contrast, if the player is standing in place, you will get a much higher jump because there is no opposing downward velocity.

The typical solution is to set the character’s Y linear velocity to 0 when it hits a platform. Then, apply the impulse after a very short timer (10 milliseconds or so).

[lua]

local vx,vy = self:getLinearVelocity()

self:setLinearVelocity( vx, 0 )

– Execute your jump function after 10 milliseconds

– This ensures that the physics engine has time to perform its calculations

timer.performWithDelay( 10, heroJump )

[/lua]

Brent