Simple intersection checking between rotated rectangles and circles/points

(click to interact)Intersection checking between rotated rectangle and point/circle
(mouseover/click to play GIF) (click and drag to adjust rectangle size/position/rotation; distance to a circle is shown)

Suppose you have a rotated rectangle and a basic shape that rotation doesn't matter for (such as a point, circle, or a line segment), and you want to check whether the two are intersecting - be that for collision handling, hit testing, or whatever else.

On a glance this might seem like a bother because things are rarely too simple with rotated rectangles, but in this case it isn't - because you can "unrotate" the rectangle.

This small post is about that.

Continue reading

Love2d: Haxe: Ray-circle intersection test

By user request on Love2d IRC channel, yesterday I've made this nice function to do intersection/collision check between a ray (for clearance, here, a ray is a infinite line with starting point but no end point) and a circle.

Underlying code is fairly simple, though it does not even require understanding to use the function.

Love2d version takes advantage of multi-return values.
Haxe version has slightly longer code due for more optimal implementation of interface.

Download Haxe .hx Download Love2d .love

Snip:

--[[
Does a ray-circle intersection test.
Parameters:
	x1, y1, di - x, y, direction (radians) of ray
	x, y, r - position & radius of circle
Returns:
	result - whether collision occurred
	x - collision X
	y - collision Y
	distance - distance from ray start to collision point
]]
function ray2circle(x1, y1, di, x, y, r)
	local vx = math.cos(di)
	local vy = math.sin(di)
	
	-- get relative XY of circle (relative to ray origin):
	x = x - x1
	y = y - y1
	
	-- rotate it based on ray direction (as if ray starts at XY=0 and goes at +X):
	local tx = x * vx + y * vy
	local ty = x * vy + y * -vx
	
	-- clear misses:
	if (tx < -r) or (ty > r) or (ty < -r) then
		return false, nil, nil, nil
	end
	
	-- find X coordinate that line hits rotated circle at
	th = math.abs(math.cos(math.asin(ty / r))) * r
	
	-- too far behind
	if (tx + th < 0) then
		return false, nil, nil, nil
	end
	
	-- line start is inside the circle:
	tx = tx - th
	if (tx < 0) then
		return true, x1, y1, 0
	end
	
	--
	return true, x1 + tx * vx, y1 + tx * vy, tx
end

GameMaker: collision versus 3d array


Can be seen as a part of those block-building games

So, today comes with an example of collision between point, box, and line versus a 3d array.

3d array is presented as ds_list (z) with ds_grid's (xy).

Functions include management of this 3d array (creation, destruction, modification - no memory leaks included) and actual functions to check for different collisions.

All of these also include error handling, so attempting to check for collision outside the 'map' will threat area as 'air' (0) and not crash the game.

Also note that no optimization was applied to process of block rendering - you'd likely have to change that if basing game with large level on this example.

Download GMK

GameMaker: line between objects

You might have met such situations where you need to find nearest coordinate at object's edge towards a point, or find actual (non-bounding box) distance between objects, or other things of this kind. If so, you may find this example useful.
Used method is relatively simple - first find approximate distance with distance_to_point function, and then 'fix precision' by using a 'while not position_meeting' loop.
This way maximum amount of calculations will be limited to cP * iR + dP, where
cP is time needed to check object for collision against a single point
iR is instance 'radius' (maximum distance from origin to corner of bounding box)
dP is time needed to find distance between object's bounding box and point
So it should be acceptable for many cases of usage. If you need to reduce amount of calculations further, could alter distance decrementation 'rate' in two while loops. Removing while() loops entirely would give rather approximate distance calculation.

Download GMK