Collision detection with rotating crosses (or similar)

Hi,

everywhere i read, “Don’t do pixel perfect collision, because it’s ineffective / time consuming” … but, when I have an image, let’s say a rotating cross, where collision should only count when the player hits one of the four rotating ‘blades’ of the cross and not the empty space in-between, how can this be done otherwise?

When I put bounding boxes around the four ends of the cross, how can I take rotation into account then?

Any tips, example code, or whatever?

let’s say the middle point of your rotating fan/cross is at _origin and the fan’s blades have a length of 10.

Let’s you have 4 blades ( a cross) and right now it is unrotated. One blade is top, one is right bottom left etc.

So the blade to the right should have the position 10, 0 at the start right?

But for rotation we need to calculate the sins and cosines, I tried to make it into a simple function:

_origin = new Vector2(0,0);
_bladeLength = 10;
_cross.angle = 0;

(…)

//rotate!
_cross.angle += 0.1f;

// Calculate the end points of the blades!

//Calculate the position from the angle
float sin0 = (float) Math.sin(_cross.angle);
float cos0 = (float) Math.cos(_cross.angle);

_blade1.position.x = sin0 * bladeLength + _origin.x;
_blade1.position.y = cos0*bladeLength + _origin.y;

//The opposite blade simply has the negative sin/cos values

_blade2.position.x = -sin0 * bladeLength + _origin.x;
_blade2.position.y = -cos0*bladeLength + _origin.y;

//Now for the other 2 blades we need to take the angle and add 90°, aka half pi

sin0 = Math.sin(_cross.angle + Math.PI/2);
cos0 = Math.cos(_cross.angle + Math.PI/2);

_blade3.position.x = sin0 * bladeLength + _origin.x;
_blade3.position.y = cos0bladeLength + _origin.y;
_blade4.position.x = -sin0 * bladeLength + _origin.x;
_blade4.position.y = -cos0
bladeLength + _origin.y;

Hope this helps

1 Like

Thanks for the detailled answer. :wink:

Alas, it doesn’t help me much… your example shows how to calculate the end points of a rotating cross, but not how to do proper collision detection with a player sprite/texture. What if the player doesn’t touch the ‘end point’ of one of the blades, but somewhere between the center/origin of the rotating cross and the end point? (assuming it’s a bigger rotating cross / a smaller player sprite)

Two rectangles that go along the blade?

Yeah, use rectangles that rotate around the center. You can apply the transformation kosmonaut explained to the rectangles and use them for collision detection.

Umm… what’s a transformation kosmonaut?

Alas, I don’t really know any geometry math… or it has been a too long time since I last used it.

So as a result you only have points to go off?

Do you then need to create virtual rectangles and check if objects are in between all the points you have?