Physics - bounce on collision instead of sliding

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
leovingi
Prole
Posts: 1
Joined: Sat Nov 20, 2021 9:27 am

Physics - bounce on collision instead of sliding

Post by leovingi »

I've been playing around with Love2D physics over the last day, following the tutorial on the wiki. I'm trying to achieve bounce physics without any gravity or reduction in velocity, as in - when a ball hits a barrier at an angle it bounces off and keeps it's current velocity.

It works fine when the ball is approaching the barrier at a more direct angle, but if the angle is wide enough, the ball starts sliding instead.

How would I achieve a perfect bounce, without any sliding, even if the ball approaches a barrier at an angle of 1 degrees? Is it possible to change some of the Fixture (or any other) parameters? I've tried setting Friction to 1 for all objects, tried setting Friction to 0, but the outcome is the same - slide on a wide approach angle.

Simplified version of my current code attached

Code: Select all

Gamestate = {
    objects = {
        barrier = nil,
        ball = nil
    },
    world = nil
}

function love.load()
    math.randomseed(os.time())

    love.physics.setMeter(64)
    Gamestate.world = love.physics.newWorld(0, 0, true)

    Gamestate.objects.ball = {}
    Gamestate.objects.ball.body = love.physics.newBody(Gamestate.world, 650/2, 650/2, "dynamic")
    Gamestate.objects.ball.shape = love.physics.newCircleShape(10)
    Gamestate.objects.ball.fixture = love.physics.newFixture(Gamestate.objects.ball.body, Gamestate.objects.ball.shape, 0.1)
    Gamestate.objects.ball.fixture:setRestitution(1)
    Gamestate.objects.ball.fixture:setFriction(0)
    Gamestate.objects.ball.body:applyLinearImpulse(0, -0.7);

    Gamestate.objects.barrier = {
        body = love.physics.newBody(Gamestate.world, 650/2, 100, "static"),
        shape = love.physics.newRectangleShape(500, 20)
    }
    Gamestate.objects.barrier.fixture = love.physics.newFixture(Gamestate.objects.barrier.body, Gamestate.objects.barrier.shape)

    Gamestate.objects.barrier2 = {
        body = love.physics.newBody(Gamestate.world, 650/2, 550, "static"),
        shape = love.physics.newRectangleShape(500, 20)
    }
    Gamestate.objects.barrier2.fixture = love.physics.newFixture(Gamestate.objects.barrier2.body, Gamestate.objects.barrier2.shape)
    Gamestate.objects.barrier.fixture:setFriction(0)
    Gamestate.objects.barrier2.fixture:setFriction(0)

    love.window.setMode(650, 650)
end

function love.update(dt)
    Gamestate.world:update(dt)
end

function love.draw()
    love.graphics.print( love.timer.getFPS(), 10, 10 );

    love.graphics.setColor(0.76, 0.18, 0.05)
    love.graphics.circle("fill", Gamestate.objects.ball.body:getX(), Gamestate.objects.ball.body:getY(), Gamestate.objects.ball.shape:getRadius())

    love.graphics.setLineWidth(20)
    love.graphics.setLineStyle("smooth")
    love.graphics.setColor(1, 1, 1)
    love.graphics.line(Gamestate.objects.barrier.body:getX()-250, Gamestate.objects.barrier.body:getY(), Gamestate.objects.barrier.body:getX()+250, Gamestate.objects.barrier.body:getY())
    love.graphics.line(Gamestate.objects.barrier2.body:getX()-250, Gamestate.objects.barrier2.body:getY(), Gamestate.objects.barrier2.body:getX()+250, Gamestate.objects.barrier2.body:getY())
end
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: Physics - bounce on collision instead of sliding

Post by pgimeno »

Thanks for the test code, but unfortunately it doesn't demonstrate the problem. This might be a box2d limitation, not sure until I see it. If so, you might need custom physics.
User avatar
idbrii
Prole
Posts: 34
Joined: Sat Jun 12, 2021 5:07 am

Re: Physics - bounce on collision instead of sliding

Post by idbrii »

You might be able to solve with World:setCallbacks.

See Tutorial:PhysicsCollisionCallbacks for details.
Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests