Checking for going off the screen

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
User avatar
Ertain
Citizen
Posts: 55
Joined: Fri Nov 19, 2010 9:38 pm
Location: Texas, U.S.A.

Checking for going off the screen

Post by Ertain »

Hello again, LÖVErs.

In my little game I try to determine if some object (picture, enemy, what have you) has left the screen by checking to see if its "x" and "y" components are greater than the length and/or width. Is this a good way of telling this? How about for multiple objects? I would like to find a method for detecting whether they've gone off the screen, yet doesn't take up a whole lot of processing and resources.
Booted, suited, and ready to get executed.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Checking for going off the screen

Post by Robin »

Ertain wrote:Is this a good way of telling this?
Yes.
Ertain wrote:How about for multiple objects?
Use tables and the same method.
Ertain wrote:I would like to find a method for detecting whether they've gone off the screen, yet doesn't take up a whole lot of processing and resources.

Code: Select all

function offscreen(object) -- assuming (x, y) is the top-left position of the object.
    return object.x < -object.width or object.y < -object.height object.x > love.graphics.getWidth() or object.y > love.graphics.getHeight()
end
Help us help you: attach a .love.
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: Checking for going off the screen

Post by Kadoba »

Try this:

Code: Select all

-- This function returns true if the object is out of the screen. 
-- trans_x and trans_y must be what you feed to love.graphics.translate()
-- scale must be what you feed to love.graphics.scale()
-- If scale and translate are not used then you only have to provide the object
function out_of_screen(object, trans_x, trans_y, scale)

local scr_left = -trans_x or 0
local scr_top = -trans_y or 0
local scr_bottom = scr_top +  love.graphics.getWidth() * (scale or 1)
local scr_right= scr_left + love.graphics.getWidth() * (scale or 1)

local obj_left = object.x
local obj_top = object.y
local obj_right = object.x + object.width
local obj_bottom = object.y + object.height

return obj_left > scr_right or obj_top > scr_bottom or obj_right < scr_left or obj_bottom < scr_top
to use it on multiple objects you would just do something like this:

Code: Select all

for key, value in pairs(objects)
     if out_of_screen(value) do 
        --something
    end
end 
User avatar
Ertain
Citizen
Posts: 55
Joined: Fri Nov 19, 2010 9:38 pm
Location: Texas, U.S.A.

Re: Checking for going off the screen

Post by Ertain »

Thanks for the input, guys.
Booted, suited, and ready to get executed.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests