[Solved] Using Box2D with STI

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
Paltze
Prole
Posts: 10
Joined: Mon Aug 09, 2021 9:33 am

[Solved] Using Box2D with STI

Post by Paltze »

I am trying to make a platformer in which instead of going from left to right you go from bottom to top. I am using Tiled and STI for map making. Please forgive me if this kind of question is answered before, I searched for a long time but found no solution. For physics I tried using Box2D but it is generating wierd results, somtimes not colliding at all (I have added the custom property "collidable" on my layer, and set it to true), sometimes after few seconds. And I have also set the player offset to half the width and half the height (thus centre) to match with x,y of Box2D. Please point out what am I doing wrong and how can I correct it. I have attached a minimal .love file to recreate the problem below. (Left, Right keys for sideways movement, Up arrow key for 'jumping')
Game.love
(20.15 KiB) Downloaded 219 times
Last edited by Paltze on Thu Aug 12, 2021 7:00 am, edited 1 time in total.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Using Box2D with STI

Post by pgimeno »

Paltze wrote: Mon Aug 09, 2021 10:30 am I am trying to make a platformer in which instead of going from left to right you go from bottom to top. I am using Tiled and STI for map making. Please forgive me if this kind of question is answered before, I searched for a long time but found no solution. For physics I tried using Box2D but it is generating wierd results, somtimes not colliding at all (I have added the custom property "collidable" on my layer, and set it to true), sometimes after few seconds. And I have also set the player offset to half the width and half the height (thus centre) to match with x,y of Box2D. Please point out what am I doing wrong and how can I correct it. I have attached a minimal .love file to recreate the problem below. (Left, Right keys for sideways movement, Up arrow key for 'jumping')
Game.love
I believe that the problem is that the player is not drawn in the centre of the screen or correctly scaled. It should be centred. If you imagine a box right at the centre of the screen, it works.

This fixes that part for me:

Code: Select all

-- change this:
    love.graphics.translate(-tx, -ty)
    player:draw()
-- to this:
    love.graphics.translate(-tx+width/2, -ty+height/2)
    player:draw()
There's also an issue with the collision box of the player, which is much bigger than the sprite, and another issue is that the tiles at the edges of the platforms seem to be collidable, making the platform collision box bigger than what they visually look like.
Paltze
Prole
Posts: 10
Joined: Mon Aug 09, 2021 9:33 am

Re: Using Box2D with STI

Post by Paltze »

Thank you for helping me. I noticed, after you said, that there is a lot of extra transparent space in sprite. After cropping it out and removing scale, it is working fine. However after applying scale it starts behaving wierdly again. How can I use scale without wierd results?
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Using Box2D with STI

Post by pgimeno »

Not sure from just the description. I'd suggest to use a box2d visualizer such as https://github.com/airstruck/sketchy to see where the collision boxes are and how they mismatch your expectations.
Paltze
Prole
Posts: 10
Joined: Mon Aug 09, 2021 9:33 am

Re: Using Box2D with STI

Post by Paltze »

Thank you for your help, I was looking for something like that for a long time. It helped in fixing the issues.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests