Game Development Reference
In-Depth Information
X1, Y1
X, Y
Width
Height
X2, Y2
Two ways of defining a rectangle
pointX=startX+width
pointY=startY+height
function getRectDimensions(startX, startY, endX, endY)
local width=endX - startX
local height=endY - startY
return startX, startY, width, height
end
Now we need to find if a point or location either gotten from the position of an object or from the
touch coordinates is within the rectangle or not. The logic behind this is simple: if the x-coordinate
of the point is within the range of startX and endX and the y-coordinate of the point is within in the
range of startY and endY , then we can conclude that the point is inside of the rectangle supplied as
startX , startY , endX , and endY .
function isPointInRect(pointX pointY, startX, startY, endX, endY)
if (pointX>startX and pointX<endX) and
(pointY>startY and pointY<endY) then
return true
else
return false
end
end
Generally, most developers use the 2D math and stop with the x,y-coordinates. If you extend this a
bit further and make use of vector math, though, you can perform rotations and calculate distances
much more easily. Some frameworks (e.g., Codea) actually integrate vector math as part of the
framework.
Search WWH ::




Custom Search