The code at the following link (http://pastebin.com/3BwbxLjn) has routines for:
-
Point in rectangle = pointInRect( pX, pY, rLeft, rTop, rWidth, rHeight )
-
Point in circle = pointInCircle( pX, pY, cX, cY, cRadius )
-
Point in diamond = pointInDiamond( pX, pY, dX, dY, dSize )
-
Rectangle in rectangle = rectInRect( r1Left, r1Top, r1Width, r1Height, r2Left, r2Top, r2Width, r2Height )
-
Circle in circle = circleInCircle( c1X, c1Y, c1Radius, c2X, c2Y, c2Radius )
-
Diamond in diamond = diamondInDiamond( d1X, d1Y, d1Size, d2X, d2Y, d2Size )
-
Two rectangles intersecting = intersectRects( r1Left, r1Top, r1Width, r1Height, r2Left, r2Top, r2Width, r2Height )
-
Two circles intersecting = intersectCircles( c1X, c1Y, c1Radius, c2X, c2Y, c2Radius )
-
Two diamonds intersecting = intersectDiamonds( d1X, d1Y, d1Size, d2X, d2Y, d2Size )
-
A rectangle intersecting a circle = intersectRectWithCircle( rLeft, rTop, rWidth, rHeight, cX, cY, cRadius )
-
A rectangle intersecting a diamond = intersectRectWithDiamond( rLeft, rTop, rWidth, rHeight, dX, dY, dSize )
I hope the parameters you have to pass are obvious!
In the case of circles and diamonds, the X and Y values you pass are of the center of the shape.
In all cases the routines return TRUE to touching results, eg if you pass a point that is on the edge of a rectangle to pointInRect(), then it will return true.
And what is a diamond in my routines? It is a rectangle rotated 45 degrees. Why do I have it? Because it is the simplest and therefore the fastest shape to do a collision test on.
The way to use the code is to stick the pastebin contents into a new lua file, called something appropriate, then require() that file in your own code. Then you call the routines with the . operator, so you might have something like:
local collisions = require( “collisions” )
local isInside = collisions.pointInCircle( 0, 0, 0, 5, 10 )
etc.
Have fun and let me know of any bugs, I haven’t actually tested some of the routines (copy and paste and tweaks) but it *should* be fine. Famous last words, right? [import]uid: 46639 topic_id: 26882 reply_id: 326882[/import]