#include <math.h>
#include <malloc.h>
#include <graphics.h>
#include <stdio.h>
struct Vector
{
double x;
double y;
};
struct OBBRect
{
int centerX; //矩形中心点坐标
int centerY; //矩形中心点坐标
double rotation; //矩形旋转角度
Vector yAxis,xAxis;//OBB的两个投影轴方向的单位向量,通过这个单位向量,其它矩形半径向此向量方向的进行投影。
double halfWidth; //矩形宽度的一半长度
double halfHeight; //矩形宽度的一半高度
};
//设置OBB的单位向量。
void setRotation(OBBRect* obbRect, double rotation)
{
obbRect->xAxis.x = cos(rotation);
obbRect->xAxis.y = sin(rotation);
obbRect->yAxis.x = -sin(rotation);
obbRect->yAxis.y = cos(rotation);
obbRect->rotation = rotation;//设置OBB矩形的旋转角度。
}
//点积,用来计算投影距离
double DotMultiply2(double x1,double y1,double x2,double y2)
{
double temp=(x1*x2+y1*y2);
if(temp<=0)
return -temp;
else
return temp;
}
//得到投影半径
double getProjectionRadius(OBBRect* obbRect, Vector* axis)
{
// 参与运算的向量都是单位向量(unit vector)
// obbRect->xAxis 投影到 axis
double projectionAxisX = DotMultiply2(axis->x, axis->y, obbRect->xAxis.x, obbRect->xAxis.y);
// obbRect->yAxis 投影到 axis
double projectionAxisY = DotMultiply2(axis->x, axis->y, obbRect->yAxis.x, obbRect->yAxis.y);
//得到矩形投影半径
return obbRect->halfWidth * projectionAxisX + obbRect->halfHeight * projectionAxisY;
}
//判断是否相交
bool isCollision(OBBRect* obbRect, OBBRect* otherObb)
{
// two OBBRect center distance vector两个OBB矩形中心的连线(矢量)
Vector distanceVec ={obbRect->centerX - otherObb->centerX,
obbRect->centerY - otherObb->centerY,