(Resolved)Basic camera stuff (was: tile scrolling 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
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

(Resolved)Basic camera stuff (was: tile scrolling problem)

Post by stout »

EDIT: this part is solved, see post further down for current issue

The problem: I'm a dummy, and am missing something incredibly obvious.

Code: Select all

for rowIndex,row in ipairs(worldMap) do
	for columnIndex,quadID in ipairs(row) do
      		local x,y = (columnIndex-1)*TileW, (rowIndex-1)*TileH
      		love.graphics.drawq(Tileset3, Quads1[quadID], x, y)
	end
end
This is code I'm using from kikito's tutorial. I understand how it works! Great for me. Gets the entirety of an array, and fills the screen appropriately.

What I DON'T understand is how to make this work with a pseudo-camera system, so I can have arrays that draw larger than the screen size. I can't even figure out how to do a simple line of quads on its own. I can write out the logic, something like: " draw worldMap[cam.y][cam.x] to worldMap[cam.y + somelimit][cam.x + somelimit] " but no amount of looping makes that happen. I thought something like this would work:

Code: Select all

cam = { x = 0, y = 0} -- there are keypressed(keys) that alter these values

	for i = 1,25 do
				local x = (cam.x) * TileW -- TileW and TileH are defined elsewhere with the quads
				local y = (cam.y) * TileH
				local quadID = worldMap[cam.y + i][cam.x + i]
				love.graphics.drawq(Tileset3, Quads1[quadID],x,y)
			
	end
..but that only draws a single tile at a time. Which I guess means it's drawing a single tile 25 times, but I don't understand why it'd be doing that. I tried adding a second for loop but that didn't appear to do a damned thing. I am vexed. VEXED. I looked at the simple tile scrolling tutorial and tried to combine that with the code I already have, but it doesn't use drawq and.. ugh. Frustrated.

So: how do you do simple tile scrolling using drawq? What loop do I need to do something like " draw worldMap[cam.y][cam.x] to worldMap[cam.y + somelimit][cam.x + somelimit] " ?
Last edited by stout on Sun Nov 04, 2012 2:30 am, edited 3 times in total.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Basic tile scrolling problem

Post by Roland_Yonaba »

Hi,
Maybe you can make use of love.graphics.translate.

Code: Select all

function love.graphics.draw()
  -- cam_x,cam_y represents the amount of which you want to translate the viewpoint from the origin.
  love.graphics.push()
  love.graphics.translate(cam_x,cam_y)
  -- draw map here
  love.graphics.pop()
  -- draw things that are not affected by the camera here (i.e static things)
end
This tutorial is really nice, and might provide an answer to the problem.
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Re: Basic tile scrolling problem

Post by stout »

Translate is not doing it for me, or I don't understand how to use it. But I came up with something so-so.

Code: Select all

cam = {x = 1, y = 1}

camera = { }

function cameraSet()

	local cameraSize = 20

	for y = 1, cameraSize do
		camera[y] = {}            
		for x = 1, cameraSize do
			camera[y][x] = worldMap[y + cam.y][x + cam.x]
		end
	end

end

function love.update(dt)
	cameraSet()
end
Draws a (20 * tilesize) x (20 * tilesize) window, fills the camera{} table with the appropriate values from the master map. It'll do for now.
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Re: Basic tile scrolling problem

Post by stout »

Well it's not quite there yet. The bottom row (so, worldMap[50]) and the rightmost column (worldMap[anything][50])) are not displaying in the zoomed mode.

Controls:
1 and 2 change the zoom level
Arrow keys move the view around when zoomed in
r will give you a new world

You can use the pink corners to tell where the edges are.

I sort-of know what the problem is, but not why. If you change the < 25 on lines 41 and 53 to 26, it scrolls all the way.. except then my cursors drop off the screen. The tiles are 32x32, and 800 (resolution) / 32 = 25, so.. it seems like it should all fit perfectly. What's the deal?
Attachments
genworldmap.love
(8.15 KiB) Downloaded 45 times
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Re: (open)Basic camera stuff (was: Basic tile scrolling prob

Post by stout »

Any ideas? =(
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: (open)Basic camera stuff (was: Basic tile scrolling prob

Post by Santos »

I think it may be cursors position which is incorrect. Try this:

Code: Select all

if key == "right" then
	if cam.x <= 25  then
		cam.x = cam.x + 1
	end
end

if key == "down" then
	if cam.y <= 25  then
		cam.y = cam.y + 1
	end
end

Code: Select all

love.graphics.draw(cursor2, 0, (cam.y-1) * (800-32) / 25)
love.graphics.draw(cursor2, cam.x * (800-32) / 25, 792, math.rad(90))
It's sometimes useful to draw stuff like this out on paper. The minimum y of the cursor should be 0, the maximum y of the cursor should be the one tile away from the end of the screen (800 - 32), and there are 25 positions in this range (800-32) / 25).

I hope this helps! :)
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Re: (open)Basic camera stuff (was: Basic tile scrolling prob

Post by stout »

Santos: thanks, that at least makes this appear right. I'm curious as to why it's going to 26 when I think it should only go to 25, but I'll diagram it out so I understand.
Post Reply

Who is online

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