Simple tiled implementation & hump camera are compatibles?

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
valba
Prole
Posts: 2
Joined: Sun Nov 05, 2017 9:52 pm

Simple tiled implementation & hump camera are compatibles?

Post by valba »

Hi,

I'm trying to use the STI and Hump camera libraries in the same project, and seems to be impossible to make them work together. I can load a tiled map and I can define a Hump camera with a level of zoom of 2, but the result is that only seems to affect the sprites "outside" the tiled map. I put a screenshot with the effect. And of course changing the camera's position doesn't move the map. I think I'm followed the examples and documentation point by point, and I don't know if there is something I'm missing or maybe both libraries are not compatibles...

This is the image, as you can see only the sprite is resized by the hump camera:
Screen Shot 2017-11-05 at 22.59.59.png
Screen Shot 2017-11-05 at 22.59.59.png (122.05 KiB) Viewed 5450 times
The source code is pretty simple:

Code: Select all

function love.load()
	
	initPos = HumpVector(200,300)
	theHero = Hero(initPos, 'media/hero/adventurer_tilesheet.png')
	theHero:walkFront()
	levelMap = Tiled("media/levels/level_01.lua")
	lastUpdateSpeed = 0.0
	camera = HumpCampera(love.graphics.getWidth()/2,love.graphics.getHeight()/2, 2)
end

Code: Select all

function love.update(dt)
	if love.keyboard.isDown('right','d') then
		canMove = true
		currentMovement = (theHero.speed*dt)
	end

	theHero.pos.x = theHero.pos.x + currentMovement

	theHero:update(dt)
	levelMap:update(dt);
	camera:move(currentMovement,0)
end

function love.draw()
	camera:attach()
	levelMap:draw()
	theHero:draw()
	camera:detach()
end
The "hero" is draw using the library anim8 without problems.

Any help with this would be appreciated. Thanks in advance.
User avatar
erasio
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

Re: Simple tiled implementation & hump camera are compatibles?

Post by erasio »

You need to provide the translation data to sti via the draw function.

I'm on mobile atm so I'll keep this short. There are good reasons for that. But for now just know that sti is calling graphics.origin. Resetting all translation and doing its own thing.

Therefore using Hump camera by itself can not work (since you're just setting translation data by attaching the camera).

You need to directly provide the camera data to sti.
valba
Prole
Posts: 2
Joined: Sun Nov 05, 2017 9:52 pm

Re: Simple tiled implementation & hump camera are compatibles?

Post by valba »

Ok, I see the problem. I've changed the love.draw() contents and now is working. I leave the solution here in case it helps someone in the future:

Code: Select all

function love.draw()
	
	local tx = camera.x - love.graphics.getWidth() / 2
	local ty = camera.y - love.graphics.getHeight() / 2

	if tx < 0 then 
		tx = 0 
	end
	if tx > levelMap.width  * levelMap.tilewidth  - love.graphics.getWidth()  then
		tx = levelMap.width  * levelMap.tilewidth  - love.graphics.getWidth()  
	end
	if ty > levelMap.height * levelMap.tileheight - love.graphics.getHeight() then
		ty = levelMap.height * levelMap.tileheight - love.graphics.getHeight()
	end

	tx = math.floor(tx)
	ty = math.floor(ty)

	levelMap:draw(-tx, ty, camera.scale, camera.scale)

	camera:attach()
	theHero:draw()
	camera:detach()

end
And the result
walk.gif
walk.gif (1.41 MiB) Viewed 5411 times
Thanks for your help
User avatar
joedono
Prole
Posts: 40
Joined: Mon Mar 04, 2013 6:58 pm

Re: Simple tiled implementation & hump camera are compatibles?

Post by joedono »

I had been wrestling with this problem for days!! You are a scholar for coming up with this solution.

Found one issue. My game is top-down, so I noticed that vertical camera movement causes the tilemap to move in the opposite direction. I had to change this line:

Code: Select all

levelMap:draw(-tx, ty, camera.scale, camera.scale)
to this:

Code: Select all

levelMap:draw(-tx, -ty, camera.scale, camera.scale)
User avatar
jojomickymack
Prole
Posts: 45
Joined: Tue Dec 26, 2017 4:52 pm

Re: Simple tiled implementation & hump camera are compatibles?

Post by jojomickymack »

I had some trouble with this and solved the problem by not using a camera at all and just scaling/translating using love.graphics.

I hope this might help others trying to do the same thing, since these libraries are all really great together. If you have a map layer with a boolean property called 'collidable' set to 'true', the plugin will handle collision detection automatically.
collidable.png
collidable.png (5.6 KiB) Viewed 5090 times

I basically took the love.physics tutorial and adapted it to use sti and the box2d plugin.
https://love2d.org/wiki/Tutorial:Physics

Note: I was able to use the excellent 'loveballs' soft body physics here too (https://github.com/exezin/loveballs)
follow.gif
follow.gif (161.93 KiB) Viewed 5102 times
Attachments
physics_test005.love
(20.21 KiB) Downloaded 186 times
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 62 guests