Sti box2D collision problem
Posted: Sat Jun 17, 2023 2:03 pm
i am trying to detect a collision on the grass but the collision is very offset
the white is where the collision should be detected
and the red is where is detecting it.
The x and y of the collision is at the middle but it should be top-left
This is the physics variables:
This is the code of crating it
This is the code to check if there is collision:
could someone explain why is this happening.
the white is where the collision should be detected
and the red is where is detecting it.
The x and y of the collision is at the middle but it should be top-left
This is the physics variables:
Code: Select all
function Grass.new(x,y,width,height)
local instance = setmetatable({}, Grass)
instance.x = x
instance.y = y
instance.isContact = false
instance.physics = {}
instance.physics.body = love.physics.newBody(World, instance.x, instance.y, "static")
instance.physics.shape = love.physics.newRectangleShape(width, height)
instance.physics.fixture = love.physics.newFixture(instance.physics.body, instance.physics.shape)
instance.physics.fixture:setSensor(true)
table.insert(ActiveGrass, instance)
end
Code: Select all
for k, v in ipairs(Map.level.layers.entity.objects) do
if v.type == "grass" then
Grass.new(v.x + v.width / 2, v.y + v.height / 2,v.width,v.height)
end
end
Code: Select all
function Grass.beginContact(a, b, collision)
for i,instance in ipairs(ActiveGrass) do
if a == instance.physics.fixture or b == instance.physics.fixture then
if a == Car.physics.fixture or b == Car.physics.fixture then
instance.isContact = true
return true
end
end
end
end