namespace BattleBumperCars
{
class RotatedRectangle
{
protected Vector2 corner1, corner2, corner3, corner4;
protected Vector2 axis1, axis2, pCorner1Axis1, pCorner1Axis2, pCorner3Axis1, pCorner3Axis2;
protected float min1, max1, min2, max2;
public RotatedRectangle()
{
corner1 = new Vector2(0 , 0);
corner2 = new Vector2(0 , 0);
corner3 = new Vector2(0 , 0);
corner4 = new Vector2(0 , 0);
axis1 = new Vector2(0, 0);
axis2 = new Vector2(0, 0);
}
public RotatedRectangle(Rectangle rect, double rot)
{
corner1 = new Vector2((rect.Center.X + rect.Width / 2) * (float)Math.Cos(rot) - (rect.Center.Y + rect.Width / 2) * (float)Math.Sin(rot), (rect.Center.Y + rect.Width / 2) * (float)Math.Cos(rot) + (rect.Center.X + rect.Width / 2) * (float)Math.Sin(rot));
corner2 = new Vector2((rect.Center.X - rect.Width / 2) * (float)Math.Cos(rot) - (rect.Center.Y + rect.Width / 2) * (float)Math.Sin(rot), (rect.Center.Y - rect.Width / 2) * (float)Math.Cos(rot) + (rect.Center.X + rect.Width / 2) * (float)Math.Sin(rot));
corner3 = new Vector2((rect.Center.X + rect.Width / 2) * (float)Math.Cos(rot) - (rect.Center.Y - rect.Width / 2) * (float)Math.Sin(rot), (rect.Center.Y + rect.Width / 2) * (float)Math.Cos(rot) + (rect.Center.X - rect.Width / 2) * (float)Math.Sin(rot));
corner4 = new Vector2((rect.Center.X - rect.Width / 2) * (float)Math.Cos(rot) - (rect.Center.Y - rect.Width / 2) * (float)Math.Sin(rot), (rect.Center.Y - rect.Width / 2) * (float)Math.Cos(rot) + (rect.Center.X - rect.Width / 2) * (float)Math.Sin(rot));
axis1 = new Vector2(corner1.X - corner3.X, corner1.Y - corner3.Y);
axis2 = new Vector2(corner3.X - corner4.X, corner3.Y - corner4.Y);
pCorner1Axis1 = new Vector2((corner1.X * axis1.X + corner1.Y * axis1.Y) / (float)(Math.Pow(axis1.X, 2) + Math.Pow(axis1.Y, 2)) * axis1.X, (corner1.X * axis1.X + corner1.Y * axis1.Y) / (float)(Math.Pow(axis1.X, 2) + Math.Pow(axis1.Y, 2)));
pCorner1Axis2 = new Vector2((corner1.X * axis2.X + corner1.Y * axis2.Y) / (float)(Math.Pow(axis2.X, 2) + Math.Pow(axis2.Y, 2)) * axis2.X, (corner1.X * axis2.X + corner1.Y * axis2.Y) / (float)(Math.Pow(axis2.X, 2) + Math.Pow(axis2.Y, 2)));
pCorner3Axis1 = new Vector2((corner3.X * axis1.X + corner3.Y * axis1.Y) / (float)(Math.Pow(axis1.X, 2) + Math.Pow(axis1.Y, 2)) * axis1.X, (corner3.X * axis1.X + corner3.Y * axis1.Y) / (float)(Math.Pow(axis1.X, 2) + Math.Pow(axis1.Y, 2)));
pCorner3Axis2 = new Vector2((corner3.X * axis2.X + corner3.Y * axis2.Y) / (float)(Math.Pow(axis2.X, 2) + Math.Pow(axis2.Y, 2)) * axis2.X, (corner3.X * axis2.X + corner3.Y * axis2.Y) / (float)(Math.Pow(axis2.X, 2) + Math.Pow(axis2.Y, 2)));
min1 = Vector2.Dot(pCorner1Axis1, axis1);
max1 = Vector2.Dot(pCorner3Axis1, axis1);
min2 = Vector2.Dot(pCorner1Axis2, axis2);
max2 = Vector2.Dot(pCorner3Axis2, axis2);
if (min1 > max1)
{
float temp = min1;
min1 = max1;
max1 = temp;
}
if (min2 > max2)
{
float temp = min2;
min2 = max2;
max2 = temp;
}
}
public bool Intersects(RotatedRectangle rr)
{
if (rr.max1 <= max1 || rr.max1 >= min1 && rr.max2 <= max2 || rr.max2 >= min2)
return true;
else
return false;
}
}
}
As an addition, it compiles fine but I get pretty random results in the game. For testing I have a playable car and a car that just sits there and will reset it's position and rotation if there is collision. I look forward to becoming an active member in this community and contributing to other peoples problems

New Topic/Question
Reply



MultiQuote








|