Rectangle and circle shape within same body causes weirdness

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
raz87
Prole
Posts: 7
Joined: Fri Jul 08, 2016 8:22 am

Rectangle and circle shape within same body causes weirdness

Post by raz87 »

Hey guys,

I've recently discovered that a body can have more than one fixture and therefore more than one shape. This would come in very handy for the current game i'm working on. So I decided to run some tests.

As far as I can see rectangle seems to work fine. I can add as many rectangle shapes and their fixtures within the same parent body/fixture and they all move together as one and respond to forces as one. However I can't get a circle shape to behave the same way.

Some sample code, you can use the up and down keys to move the body, but something weirds going on with the circle shape
version 0.10

Code: Select all

function love.load()
  world = love.physics.newWorld(0, 0, true)
  main_body = love.physics.newBody(world, 400, 300, "dynamic")
  main_shape = love.physics.newRectangleShape(64,64)
  main_fixture = love.physics.newFixture(main_body, main_shape)
  
  rect_shape = love.physics.newRectangleShape(-10,-10,4,4)
  rect_fixture = love.physics.newFixture(main_body, rect_shape)
  
  circle_shape = love.physics.newCircleShape(main_body:getX(), main_body:getY(), 4)
  circle_fixture = love.physics.newFixture(main_body, circle_shape)
end

function love.update(dt)
  world:update(dt)
  if love.keyboard.isDown("down") then
    main_body:applyForce(0,200)
  elseif love.keyboard.isDown("up") then
    main_body:applyForce(0,-200)
  end
end

function love.draw()
  love.graphics.polygon("line", main_body:getWorldPoints(main_shape:getPoints()))
  love.graphics.polygon("line", main_body:getWorldPoints(rect_shape:getPoints()))
  
  local shape_x, shape_y = circle_shape:getPoint()
  love.graphics.circle("line", shape_x, shape_y, circle_shape:getRadius(), 100)
end
Seems to be something either wrong with the way I'm attaching the circle shape OR I might be drawing it wrong. Any help would be greatly appreciated. I'm done something silly dunno what

Best regards,
Thanks!
raz87
Prole
Posts: 7
Joined: Fri Jul 08, 2016 8:22 am

Re: Rectangle and circle shape within same body causes weirdness

Post by raz87 »

Hmm looks like this is the problem.

Code: Select all

local shape_x, shape_y = circle_shape:getPoint()
Is there a way to get the centre coordinates of the circle relative to the position of the parent body rather than a static position
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Rectangle and circle shape within same body causes weirdness

Post by airstruck »

Shape positions are already relative to the body. If you want the circle in the middle of the body, use newCircleShape(0, 0, radius), and circle:getPoint will return 0, 0. If you want the world position, you can use body:getWorldPoint(circle:getPoint()).

What you probably wanted to do in the code in your first post is this:

Code: Select all

circle_shape = love.physics.newCircleShape(0, 0, 4)
-- or this shorthand
circle_shape = love.physics.newCircleShape(4)
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests