Movement - move the player AND the world?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Movement - move the player AND the world?

Post by hasen »

I've never made a game before and so I'm not sure how this is all supposed to work. I'm using Tiled with STI and am trying to get the Stalker-X camera to work with it. It seems to work just fine but it's quite apparent that both the world and the player are moving...ie doubling his movement speed. Despite looking fine I imagine this is not the way it should be working.

Code: Select all

function love.draw()
	camera:attach()
	-- Draw map
	love.graphics.setColor(255, 255, 255)
	map:draw(-player.x, -player.y)
  --
	-- -- Draw physics objects
	love.graphics.setColor(255, 0, 255)
	map:box2d_draw(-player.x, -player.y)

	love.graphics.draw(player.img, player.x, player.y, 0, 1, 1, 0, 32)

	camera:detach()
end
If I do it any other way either nothing moves or the map doesn't move at all etc.
User avatar
Schwender.exe
Prole
Posts: 25
Joined: Mon Oct 16, 2017 6:07 pm
Location: the moon
Contact:

Re: Movement - move the player AND the world?

Post by Schwender.exe »

from what I see it might be because you're drawing the map at player.x,player.y instead of 0,0
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Movement - move the player AND the world?

Post by hasen »

Ok but then the player moves but the map doesn't...?

Code: Select all

map:draw(0, 0)
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Movement - move the player AND the world?

Post by Tjakka5 »

Are you following the player with the camera?

Code: Select all

function love.update(dt)
    camera:update(dt)
    camera:follow(player.x, player.y)
end
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Movement - move the player AND the world?

Post by hasen »

Yeah of course.

Code: Select all

function love.update(dt)
	world:update(dt)
	map:update(dt)
	camera:update(dt)
	camera:follow(player.x, player.y)
	-- Move map
	local kd = love.keyboard.isDown
	local l  = kd("left")  or kd("a")
	local r  = kd("right") or kd("d")

	player.x = l and player.x - player.speed * dt or player.x
	player.x = r and player.x + player.speed * dt or player.x

end
User avatar
erasio
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

Re: Movement - move the player AND the world?

Post by erasio »

STI doesn't work well with other cameras.

It calls love.graphics.origin before drawing anything meaning any effect applied beforehand (location, scale or rotation) is completely ignored.

While it does handle everything for you in regards to drawing, it's very limited in how it can draw the environment.

In other words. It can only work without rotation and you have to supply the appropriate translation yourself as parameters.

It is intended that you move both the player and the world. This should have no effect on the speed or visuals.

I'm guessing you've been scaling with your camera which would explain the speed differences.

Edit: 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.
Last edited by erasio on Mon Jan 22, 2018 8:20 am, edited 1 time in total.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Movement - move the player AND the world?

Post by hasen »

erasio wrote: Sun Jan 21, 2018 1:53 pm STI doesn't work with other cameras.

It calls love.graphics.origin before drawing anything meaning any effect applied beforehand (location, scale or rotation) is completely ignored.

While it does handle everything for you in regards to drawing, it's very limited in how it can draw the environment.

In other words. It can only work without rotation or scaling and you have to supply the appropriate translation yourself.

It is intended that you move both the player and the world. This should have no effect on the speed or visuals.

I'm guessing you've been scaling with your camera which would explain the speed differences.

Edit: 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.
OK I see, that explains a lot then. I thought I was just doing it all wrong. But I thought normally people use STI with Tiled? At least I noticed Tiled is extremely popular. I want to use that camera so what should I use with Tiled? Thanks for your help.
User avatar
erasio
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

Re: Movement - move the player AND the world?

Post by erasio »

hasen wrote: Sun Jan 21, 2018 2:04 pm OK I see, that explains a lot then. I thought I was just doing it all wrong. But I thought normally people use STI with Tiled? At least I noticed Tiled is extremely popular. I want to use that camera so what should I use with Tiled? Thanks for your help.
Truth be told. I have no idea.

Never looked closely enough at tiled. I tried removing graphics.origin() but it kinda broke for no reason I understood at that time and I just dealt with that limitation (and added scaling myself which was just extra parameters to love.draw and some extra code)
Last edited by erasio on Sun Jan 21, 2018 2:58 pm, edited 1 time in total.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Movement - move the player AND the world?

Post by hasen »

erasio wrote: Sun Jan 21, 2018 2:10 pm
hasen wrote: Sun Jan 21, 2018 2:04 pm OK I see, that explains a lot then. I thought I was just doing it all wrong. But I thought normally people use STI with Tiled? At least I noticed Tiled is extremely popular. I want to use that camera so what should I use with Tiled? Thanks for your help.
Trust be told. I have no idea.

Never looked closely enough at tiled. I tried removing graphics.origin() but it kinda broke for no reason I understood at that time and I just dealt with that limitation (and added scaling myself which was just extra parameters to love.draw and some extra code)
Hmm I see. So you do the levels in code? I wouldn't know how to do that. But it seems many people use Tiled with Love2d still though.
User avatar
erasio
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

Re: Movement - move the player AND the world?

Post by erasio »

Once I fixed the scaling and simply didn't use rotation.

Other times I implemented my own tile or chunk system which i did write my own map formats for. (Well one was picture based with color codes)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 85 guests