Game Development Reference
In-Depth Information
function math.sign(theValue)
return theValue > 0 and 1 or theValue < 0 and −1 or 0
end
The preceding function is equivalent to
function math.sign(theValue)
if theValue > 0 then
return 1
elseif theValue < 0 then
return −1
else
return 0
end
end
Collisions
One of the basic things that you will need for your games is the ability to check if two objects on the
screen have collided. For example, in PAC-MAN, you would need to determine collisions between
PAC-MAN and the ghosts and fruit. In Space Invaders, you would need to determine collisions
between alien bullets and the player's shield and the player itself. However one point to note is that
real world objects are not rectangular stamps, and therefore are not always rectangular in shape.
Therefore, we have various methods, described in this section, which we can use to check for
collisions between two circles, two rectangles, a rectangle and a circle, and so on.
Using isPointInRect
This is the most commonly used function in the UIs that we use every day. Each button in a UI has
a rectangular boundary, and programs check whether a touch point is inside of this boundary to
determine if that button was tapped or not.
The way it works is that the point defined by x and y should be contained in the rectangle—that is
the x value should be greater than or equal to the value of the rectangle's x-coordinate, and the
point x should be less than or equal to the rectangle's x point plus the width (and similarly on the
y axis too).
function isPointInRect( x, y, rectX, rectY, rectWidth, rectHeight )
if x >= rectX and x <= rectX + rectWidth and
y >= rectY and y<= rectY + rectHeight then
return true
end
return false
end
 
Search WWH ::




Custom Search