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

HaxeNME version. Love2d version works equally to this.
Click and drag different parts for interaction.

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

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