Resizing of love.physics objects

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
User avatar
Khepri
Prole
Posts: 5
Joined: Fri Mar 22, 2013 4:19 pm

Resizing of love.physics objects

Post by Khepri »

Hi everybody,
I'm doing some experiments with love.physics starting from the basic tutorial (http://love2d.org/wiki/Tutorial:Physics).

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:

Code: Select all

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

Thank you in advance for your support! ^^
User avatar
Khepri
Prole
Posts: 5
Joined: Fri Mar 22, 2013 4:19 pm

Re: Resizing of love.physics objects

Post by Khepri »

I still have not found a solution.. no one has a suggestion? :cry:
User avatar
Khepri
Prole
Posts: 5
Joined: Fri Mar 22, 2013 4:19 pm

Re: Resizing of love.physics objects

Post by Khepri »

Hi all,
I think I have found a solution:

Code: Select all

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

I hope this can be useful.

Bye! :ultraglee:
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 3 guests