Now I would like to resize the ball when I move the mouse wheel up but I can't figure out how to do that without overwriting the already existing ball with a new one with a different radius. Currently my code is something like that:
-- initial ball setting
ball_radius = 20
objects.ball = {}
objects.ball.body = love.physics.newBody(world, 650/2, 650/2, "dynamic")
objects.ball.shape = love.physics.newCircleShape(ball_radius)
objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 2)
-- other code here...
-- mouse events
function love.mousepressed(x, y, button)
-- if (wheel up), increase the ball size
if button == 'wu' then
ball_radius = ball_radius +10
objects.ball = {}
objects.ball.body = love.physics.newBody(world, x, y)
objects.ball.shape = love.physics.newCircleShape(ball_radius)
objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 2)
end
end
So, what I would like to know is if this is the only way to do that or if I am missing something and there is a method that allows to resize an object (body, shape and fixture) without replacing it.
-- initial ball setting
ball_radius = 20
objects.ball = {}
objects.ball.body = love.physics.newBody(world, 650/2, 650/2, "dynamic")
objects.ball.shape = love.physics.newCircleShape(ball_radius)
objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 2)
-- solution here -------------------------------------------------------------
ball_shape = objects.ball.fixture:getShape()
--------------------------------------------------------------------------------
-- other code here...
-- mouse events
function love.mousepressed(x, y, button)
-- if (wheel up), increase the ball size
if button == 'wu' then
ball_radius = ball_radius +10
ball_shape:setRadius(ball_radius)
end
end
This code does the same job of the code posted before but it works directly on the shape of the fixture (see the variable ball_shape that is a reference obtained with fixture:getShape()), avoiding the needing of create a new body/shape/fixture every time that you want to resize a shape.