Sti box2D collision problem

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
Yani354
Prole
Posts: 13
Joined: Wed May 24, 2023 5:21 pm

Sti box2D collision problem

Post by Yani354 »

i am trying to detect a collision on the grass but the collision is very offset
Image

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
This is the code of crating it

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
This is the code to check if there is collision:

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
could someone explain why is this happening.
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Sti box2D collision problem

Post by darkfrei »

Please provide the .love file without any not relevant code and content.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Yani354
Prole
Posts: 13
Joined: Wed May 24, 2023 5:21 pm

Re: Sti box2D collision problem

Post by Yani354 »

darkfrei wrote: Sat Jun 17, 2023 3:35 pm Please provide the .love file without any not relevant code and content.
the game is meant for touchscreen so it might not work that well with mouse but it still does the job
game.love
(62.4 KiB) Downloaded 143 times
sorry for the long respond i just accidentally replied to myself
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Sti box2D collision problem

Post by darkfrei »

Yani354 wrote: Sun Jun 18, 2023 8:13 pm
darkfrei wrote: Sat Jun 17, 2023 3:35 pm Please provide the .love file without any not relevant code and content.
the game is meant for touchscreen so it might not work that well with mouse but it still does the job
game.love

sorry for the long respond i just accidentally replied to myself
Sorry, I've reworked some stuff that I cannot understand, I've never used box2d before and I m not sure that the solution is good.

But for this purpose will be enough any custom collision detection.
Attachments
game-sti-box2d-01.love
(64.2 KiB) Downloaded 106 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests