boundingBox curiosity

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
dad
Prole
Posts: 3
Joined: Thu Jul 25, 2024 6:02 pm

boundingBox curiosity

Post by dad »

Anyone know why the bounding box doesn't match the size of the rectangle (or polygon) shape that is applied? It seems to add 0.6m to both the width and the length of the shape to the bounding box (regardless of the width and height used).

Code:

Code: Select all

local x, y = 20, 20
local w, h = 10, 8

local world = love.physics.newWorld(0, 0, true)
local body = love.physics.newBody(world, x, y, "static")
local shape = love.physics.newRectangleShape(w, h)
-- local shape = love.physics.newPolygonShape(0, 0, w, 0, w, h, 0, h)
-- local shape = love.physics.newEdgeShape(0, 0, w, h)
local fixture = love.physics.newFixture(body, shape)
fixture:setUserData("test")
fixture:setSensor(true)

local x1, y1, x2, y2 = fixture:getBoundingBox()
print("Bounding box:", x1, y1, x2, y2)
print("Width:", x2 - x1, "Height:", y2 - y1)
print("Expected X bounds:", x - w / 2, x + w / 2)
print("Expected Y bounds:", y - h / 2, y + h / 2)
Output:

Code: Select all

Bounding box:   14.700000762939 15.700000762939 25.300001144409 24.299999237061
Width:  10.60000038147  Height: 8.5999984741211
Expected X bounds:      15      25
Expected Y bounds:      16      24
RNavega
Party member
Posts: 462
Joined: Sun Aug 16, 2020 1:28 pm

Re: boundingBox curiosity

Post by RNavega »

It looks like a RectangleShape is a convenience for a 4-vertex PolygonShape, and when you create it, it recalculates the vertices.
My guess is this may introduce some imprecision.

The source code chain is this:
1) https://github.com/love2d/love/blob/11. ... #L126-L155

2) https://github.com/love2d/love/blob/11. ... pp#L79-L94

3) https://github.com/love2d/love/blob/11. ... pp#L44-L67

The only thing i can think of is to try creating a rectangular (i.e. 4-vertex) PolygonShape manually, so it avoids that SetAsBox() overload that recalculates things.
See if that gives you the exact geometry you're going for.

Edit: another thing to test is setting the meter to 10, instead of the default of 30. All coordinates and sizes are divided by this meter, and dividing something by 30 leads to more fractional numbers than simply dividing by 10.
https://love2d.org/wiki/love.physics.setMeter
dad
Prole
Posts: 3
Joined: Thu Jul 25, 2024 6:02 pm

Re: boundingBox curiosity

Post by dad »

Thanks for the reply RNavega. You got me on the right track.

I found the source of the issue between these two chunks of code:
https://github.com/love2d/love/blob/680 ... ings.h#L80
https://github.com/love2d/love/blob/680 ... e.cpp#L317

Box2D applies a small 0.01 unit "skin" onto the Shape, this skin is then scaled up by Love (default meter of 30) which results in the 0.3m offsets observed.

Now, as a follow up question, is this "skin" really meant to be scaled by Love2D? I can't think of any reason for it. Am I better off creating a new issue on Github for this?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 5 guests