Page 1 of 1

How to use Tiled (or not) with a platformer

Posted: Sun Jan 21, 2018 5:38 pm
by hasen
I want to use the the Stalker X camera library with a platform game and I was planning to use Tiled to make the levels, however, I was informed in another thread that the STI library doesn't work well with camera libraries...so what would be the best way to implement Tiled with a Love2d platformer? Maybe I could do without Tiled but I'm not sure what I would do instead to make the levels?

Re: How to use Tiled (or not) with a platformer

Posted: Mon Jan 22, 2018 2:42 am
by jojomickymack
You should definitely stick with Tiled and STI, it's going to simplify collision detection, level building, and art management massively. The reason STI isn't going to work with camera libraries is because in order to be as stable as possible, it needs to do it's own translation and scaling when it draws the layers.

The tutorial is still good for covering how to get up and running with it.

http://lua.space/gamedev/using-tiled-maps-in-love

Just keep in mind that the last line of the example there isn't correct, since translating and scaling the map is done in the call to draw the map.

Map:draw (tx, ty, sx, sy)
Draw every Layer
Parameters:

tx Translate on X
ty Translate on Y
sx Scale on X
sy Scale on Y

That's the key to scrolling the map around, if you really want some of those features stalkerX provides, like shaking the viewport or allowing leading room in the direction the player is moving in, you can easily implement them with the translation/scaling parameters. It's certainly easiest to just translate based on the player's position like it shows in the example.

Re: How to use Tiled (or not) with a platformer

Posted: Mon Jan 22, 2018 7:25 am
by hasen
jojomickymack wrote: Mon Jan 22, 2018 2:42 am You should definitely stick with Tiled and STI, it's going to simplify collision detection, level building, and art management massively. The reason STI isn't going to work with camera libraries is because in order to be as stable as possible, it needs to do it's own translation and scaling when it draws the layers.
This is all quite confusing. The Stalker X camera is designed for use with platformers, it has a mode for that. Also it has features built in like camera shake all ready to use. Although I was advised not to use STI with a camera module but conversely I'm advised also to make sure I use STI and not use a camera.

Not sure what's the best approach for a platform shooter..aside from programming all or some of the modules myself of course but since I've never used Love2d or even programmed a game before that feels a bit daunting.

Re: How to use Tiled (or not) with a platformer

Posted: Mon Jan 22, 2018 8:23 am
by erasio
I said that STI doesn't play nice with camera libraries because it resets them before drawing the map.

And that you must provide the camera translation (without rotation which you mustn't use without modifying STI) to the sti map if you want to use it.
So you can use a camera. But you are more limited and need to make sure to not use the player location but the camera location as parameter for STI maps. Since that's what you expect to draw.
Admittedly I confused sti with another library which didn't have scaling either. So that's my bad. Scaling works. Only rotation is off limits.

Re: How to use Tiled (or not) with a platformer

Posted: Mon Jan 22, 2018 2:08 pm
by hasen
erasio wrote: Mon Jan 22, 2018 8:23 am Admittedly I confused sti with another library which didn't have scaling either. So that's my bad. Scaling works. Only rotation is off limits.
Ok I see. So it can work but not really recommended.

Re: How to use Tiled (or not) with a platformer

Posted: Mon Jan 22, 2018 5:33 pm
by erasio
It's not so much a recommendation to not use it but rather a limitation to be aware of.

I mean do you need to rotate your screen? Is that important to you? (not characters or objects in your world. You can rotate those just fine)

If it is, then yeah. I would recommend looking into how easy it is to fix that in sti by yourself or look for alternatives.

Realistically. A lot of games don't need camera rotation though.

If this limitation is OK for you it'll just be a bit of extra work to apply the same transformation to both the map and the camera (probably by accessing your camera and reading out it's location and passing that on to the sti map).

Re: How to use Tiled (or not) with a platformer

Posted: Mon Jan 22, 2018 6:34 pm
by hasen
erasio wrote: Mon Jan 22, 2018 5:33 pm It's not so much a recommendation to not use it but rather a limitation to be aware of.

I mean do you need to rotate your screen? Is that important to you? (not characters or objects in your world. You can rotate those just fine)

If it is, then yeah. I would recommend looking into how easy it is to fix that in sti by yourself or look for alternatives.

Realistically. A lot of games don't need camera rotation though.

If this limitation is OK for you it'll just be a bit of extra work to apply the same transformation to both the map and the camera (probably by accessing your camera and reading out it's location and passing that on to the sti map).
No I shouldn't think I'll need screen rotation....screen shake would be good though. I couldn't get STI to work with the Stalker camera at all anyway though, as you noticed from my other thread.

Re: How to use Tiled (or not) with a platformer

Posted: Tue Jan 23, 2018 2:49 pm
by jojomickymack
Give this a shot - add a function to the stalker-x library to just return the x, y and scale.

Code: Select all

function Camera:getCoords()
    return self.x, self.y, self.scale
end
in the love.update function, call that function and consume the coordinates

Code: Select all

cameraX, cameraY, cameraScale = camera:getCoords()
and when it comes time to draw, calculate dx and dy for use in translating the map in its draw call based on cameraX and cameraY

Code: Select all

dx = cameraX - (love.graphics.getWidth() / 2) / cameraScale
dy = cameraY - (love.graphics.getHeight() / 2) / cameraScale
and draw the map like so

Code: Select all

map:draw(-dx, -dy, cameraScale, cameraScale)
So far in testing, I see stalker-x's effects working as expected! See if that works for you, hasen.

Re: How to use Tiled (or not) with a platformer

Posted: Fri Jan 26, 2018 11:02 am
by hasen
jojomickymack wrote: Tue Jan 23, 2018 2:49 pm Give this a shot - add a function to the stalker-x library to just return the x, y and scale.

Code: Select all

function Camera:getCoords()
    return self.x, self.y, self.scale
end
in the love.update function, call that function and consume the coordinates

Code: Select all

cameraX, cameraY, cameraScale = camera:getCoords()
and when it comes time to draw, calculate dx and dy for use in translating the map in its draw call based on cameraX and cameraY

Code: Select all

dx = cameraX - (love.graphics.getWidth() / 2) / cameraScale
dy = cameraY - (love.graphics.getHeight() / 2) / cameraScale
and draw the map like so

Code: Select all

map:draw(-dx, -dy, cameraScale, cameraScale)
So far in testing, I see stalker-x's effects working as expected! See if that works for you, hasen.
I only just noticed your post because it seems I'm not automatically subscribed to my own threads. Thanks I'll see how that works

Re: How to use Tiled (or not) with a platformer

Posted: Tue Jan 30, 2018 6:20 am
by hasen
Forgot to mention - your code works perfectly. Thanks very much Jojomickymack.