cocos creator3D中怎么实现在主角在敌人的扇形攻击区域会受到攻击?
1赞
先加一个圆形碰撞,再计算角度
好的,我试试
点在扇形的代表的圆内 && 点在扇形的开口角度内
如果主角是圆形的话参考这个(变量名别笑)
public static collideArcCircle(cx: number, cy: number, cr: number, ax: number, ay: number, ar: number, adirx: number, adiry: number, aah: number): boolean
{
//扇形圆形指向圆心向量
let dx = cx - ax;
let dy = cy - ay;
let dlen = Math.sqrt(dx * dx + dy * dy);
//分离不相交
if (dlen > cr + ar)
{
return false;
}
let c = Math.cos(aah);
//如果扇形指向圆心的向量和扇形朝向的夹角小于扇形角度的一半, 说明相交
if (this.dot(dx / dlen, dy / dlen, adirx, adiry) > c)
{
return true;
}
let s = Math.sin(aah);
//将圆转换到扇形指向坐标系
//x轴:(adirx, adiry)
//y轴:(-adiry, adirx)
let cax = dx * adirx + dy * adiry;
let cay = dx * -adiry + dy * adirx;
//扇形上边缘的点
let px = c * ar;
let py = s * ar;
if (P5.collideLineCircle(0, 0, px, py, cax, cay, cr))
{
return true;
}
//扇形下边缘的点 对称的缘故只需要把圆关于x轴对称就好了
if (P5.collideLineCircle(0, 0, px, py, cax, -cay, cr))
{
return true;
}
return false;
}
大佬,可以把this.dot和p5.collideLineCircle函数的代码也给出看看吗
public static collideLineCircle(x1:number, y1:number, x2:number, y2:number, cx:number, cy:number, cr:number):boolean
{
// is either end INSIDE the circle?
// if so, return true immediately
var inside1 = this.collidePointCircle(x1, y1, cx, cy, cr);
var inside2 = this.collidePointCircle(x2, y2, cx, cy, cr);
if (inside1 || inside2) return true;
// get length of the line
var distX = x1 - x2;
var distY = y1 - y2;
var len = Math.sqrt((distX * distX) + (distY * distY));
// get dot product of the line and circle
var dot = (((cx - x1) * (x2 - x1)) + ((cy - y1) * (y2 - y1))) / Math.pow(len, 2);
// find the closest point on the line
var closestX = x1 + (dot * (x2 - x1));
var closestY = y1 + (dot * (y2 - y1));
// is this point actually on the line segment?
// if so keep going, but if not, return false
var onSegment = this.collidePointLine(closestX, closestY, x1, y1, x2, y2);
if (!onSegment) return false;
// draw a debug circle at the closest point on the line
if (this._collideDebug)
{
this.ellipse(closestX, closestY, 10, 10);
}
// get distance to closest point
distX = closestX - cx;
distY = closestY - cy;
var distance = Math.sqrt((distX * distX) + (distY * distY));
if (distance <= cr)
{
return true;
}
return false;
}
public static dot(x1: number, y1: number, x2: number, y2: number): number
{
return x1 * x2 + y1 * y2;
}