# Simple intersection checking between rotated rectangles and circles/points

(click to interact) (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.

# GameMaker: Finding collision_line’s point of collision

(mouseover/click to play GIF)

This is a small post about a method for figuring out collision_line's "contact point" in GameMaker - in other words, obtaining not just whether there's something on the way, but also the nearest point on the nearest matching entity.

Most commonly asked about for "hitscan" weapons and laser-sights, but has many uses.

# Rectangle-circle intersection test

This is a blog post about handling circle-rectangle collisions. For some reason, these seem to be generally regarded as something complicated, even though they aren't.

# 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.

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
```

# Love2d: Shooting things

At some point of time people were asking me about how bullets were implemented in love.blast() so I wrote this post.

While the algorithms in the actual [mini]game were slightly more complex than what is described here, the difference is not apparent until you have thousands of bullets.

# 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.

# GameMaker: collision_* into array

While working with GameMaker, you might have noticed that all collision_ functions return only first instance to intersect given collision shape, while in some cases you'd want to get all instances that collide with one.
This set of simple scripts adds such missing functionality...