How to access information of the parent object of a Fixture

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
casual_dodo
Prole
Posts: 2
Joined: Sun Jan 05, 2025 4:41 pm

How to access information of the parent object of a Fixture

Post by casual_dodo »

So this in theory should be a relatively straight-forward thing, but I'm getting stuck quite hard here.

If I have an object being initialized, that contains a physics body like so:

Code: Select all

local size = math.random(50) + 30
local x = math.random(maxWidth - 200)
local y = math.random(maxHeight - 200)
local box = { size = size, color = { math.random(), math.random(), math.random(), 1 } }

box.body = love.physics.newBody(world, x, y, "dynamic")
box.shape = love.physics.newRectangleShape(size, size)
box.fixture = love.physics.newFixture(box.body, box.shape, 1)
How can I access the object's information when a collision happens between two boxes? I'm using "world:getContacts()" which returns me a list of "Contacts", which in turns gives me access to the fixtures. However, how could I access the parent to change the color of the box, for example?

Am I approaching this the wrong way, should I set the data inside the fixture, instead of outside it?

Thank you!
User avatar
dusoft
Party member
Posts: 676
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: How to access information of the parent object of a Fixture

Post by dusoft »

e.g.:

Code: Select all

fixture:getBody():getWorldPoints(fixture:getShape():getPoints())
or using custom user data:

Code: Select all

function world.collision_start(fixture_a, fixture_b, contact)
    local types = {fixture_a:getUserData().type, fixture_b:getUserData().type}
    interface.type_a = types[1]
    interface.type_b = types[2]
    if world.checkCollision(types, 'player', 'edge') then
        world.state.collision = true
...
getting contact:

Code: Select all

world.state.latest_contact = {contact:getPositions()}
using contact to draw something (crash location...):

Code: Select all

local x1, y1, x2, y2 = unpack(world.state.latest_contact)
        if x1 and y1 then
            love.graphics.setColor(love.math.colorFromBytes(255, 255, 255))
            love.graphics.circle('line', x1, y1, 5)
        end
        if x2 and y2 then
            love.graphics.line(x1, y1, x2, y2)
        end
All snippets are mine, so thread carefully.
casual_dodo
Prole
Posts: 2
Joined: Sun Jan 05, 2025 4:41 pm

Re: How to access information of the parent object of a Fixture

Post by casual_dodo »

Hey there @dusoft! Apologies for the late reply, but unfortunately that doesn't really answer my question. What I want is to see if I can infer the parent object just using a collision. What you showed was how to get more information from the fixture, but not a path to get its parent.

I'm trying to use 'fixture:setUserData()' to see if I can store that value in a table to compare when checking for collisions, but this seems overly complicated, not to mention not performant. Still, I can't even get that working, because the 'fixture:getUserData()' function returns nil.

Are there any other options I'm missing?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 14 guests