2D Toolkit Forum

2D Toolkit => Support => Topic started by: mukarillo on September 27, 2015, 07:03:13 pm

Title: Advanced Collision with angle
Post by: mukarillo on September 27, 2015, 07:03:13 pm
Hi there,

I'm using Advanced Collision to make a fighting game. I've managed to compare two rects to check for collision using this code:

Code: [Select]
float originX = (currentSprite.FlipX) ? -currentSprite.CurrentSprite.customColliders[i].origin.x : currentSprite.CurrentSprite.customColliders[i].origin.x;
Rect collisionRect = new Rect(
(gameObject.transform.localPosition.x+originX)-currentSprite.CurrentSprite.customColliders[i].Size.x/2,
(gameObject.transform.localPosition.y+currentSprite.CurrentSprite.customColliders[i].origin.y)-currentSprite.CurrentSprite.customColliders[i].Size.y/2,
currentSprite.CurrentSprite.customColliders[i].Size.x,
currentSprite.CurrentSprite.customColliders[i].Size.y);

But now i'm facing another problem, i got a sprite with a 45 degree angle, like this:
(http://s28.postimg.org/4pt35aky4/Screen_Shot_2015_09_27_at_2_58_34_PM.jpg)
But when I run that code into this kind of image, this happen:
(http://s14.postimg.org/58ot6qy34/Screen_Shot_2015_09_27_at_3_01_39_PM.jpg)

I coudn't find a way to rotate a Rect, how can i do that?

Thanks! :D
Title: Re: Advanced Collision with angle
Post by: unikronsoftware on October 04, 2015, 12:47:27 pm
Hi,

You can't rotate a unity Rect class, it doesn't carry any rotation information. I don't think Unity has any built in box-box collision without using physics which will be overkill. You could write your own overlap test class (http://www.gamedev.net/page/resources/_/technical/game-programming/2d-rotated-rectangle-collision-r2604) somethign like that will do the job.