Page 1 of 1

Scallability messes up collision detection

Posted: Mon Mar 20, 2017 3:51 pm
by NickRock
I'm making an android game and because I want it to be compatible with many devices of different sizes I made it resizable and also scale-able using the love.graphcs.scale function in the love.draw function

Code: Select all

function love.draw()
	love.graphics.scale(Width/720, Height/1280)
end
The width and height of my game is 720 x 1280

In the main menu, I have a button that is set to be in the middle of the screen when you press it, it is supposed to get you to the next screen, my problem is that when I resize the game from conf.lua for example change 720 to 360 and 1280 to 640 the "collision" detection with the mouse coordinates and the button coordinates don't mach. Here's the code:

Code: Select all

function menu:mousepressed(x,y,bytton,istouch)
	if AABB(x,y,1,1, playBtn.x-playBtn.w/2,playBtn.y-playBtn.h/2,playBtn.w,playBtn.h) then
		print("button pressed")
		print(x .. " " .. y)
	end
end

--On an different file
function AABB(x1,y1,w1,h1, x2,y2,w2,h2)
  return x1 < x2+w2 and
         x2 < x1+w1 and
         y1 < y2+h2 and
         y2 < y1+h1
end
The button is drawn on the draw function and I also drew a rectangle around it just to test things out

Code: Select all


function menu:draw()
	love.graphics.draw(bg,0,0)
	love.graphics.draw(title.img,title.x,title.y,r,1,1,360,250)
	love.graphics.draw(playBtn.img,playBtn.x,playBtn.y,0,1,1,playBtn.w/2,playBtn.h/2)
	love.graphics.rectangle("line",playBtn.x-playBtn.w/2,playBtn.y-playBtn.h/2,playBtn.w,playBtn.h)
end

If anyone could help me out with this that would be awesome, thanks!

Re: Scallability messes up collision detection

Posted: Tue Mar 21, 2017 11:41 am
by raidho36
Of course it doesn't, the graphics.scale function only affects subsequent rendering, nothing more. The normal multi-argument draw function is a shortcut for calling shear, scale, translate and rotate in correct sequence. You scaled your graphics alone, don't expect input coordinates to be scaled, you need to do that yourself.

At this time might I shill for my own Android/iOS library that does exactly what you need it to do? There it is: https://love2d.org/forums/viewtopic.php?f=5&t=82892 . You can scale master layer by same value as you do with the screen and all input coordinates will scale to match.

Re: Scallability messes up collision detection

Posted: Tue Mar 21, 2017 12:11 pm
by NickRock
Oh well, that makes a lot of sense now. Thanks!