2DのXNA視野フィールド
-
29-09-2019 - |
質問
私は群れに基づいてXNAの2Dゲームに取り組んでいます。 Craig ReynoldのFlocking Techniqueを実装しましたが、今ではリーダーをグループに動的に割り当てて、ターゲットに向かって導きたいと考えています。
これを行うには、その前に他のエージェントがいないゲームエージェントを見つけてリーダーにしたいのですが、このための数学はわかりません。
現在私は持っています:
Vector2 separation = agentContext.Entity.Position - otherAgent.Entity.Position;
float angleToAgent = (float) Math.Atan2(separation.Y, separation.X);
float angleDifference = Math.Abs(agentContext.Entity.Rotation - angleToAgent);
bool isVisible = angleDifference >= 0 && angleDifference <= agentContext.ViewAngle;
agentContext.Viewangleは、私がプレーしたラジアンの値であり、正しい効果を得るためにプレイしましたが、これは主にすべてのエージェントがリーダーとして割り当てられます。
誰かが私を正しい方向に向けて、エンティティが別のエンティティの視界の「コーン」内にあるかどうかを検出できますか?
解決
ATAN2関数への入力を正規化する必要があります。また、結果は範囲PIから-PIの外側にある可能性があるため、角度を減算するときは注意する必要があります。角度ではなく方向ベクターを使用することを好むので、この種のものにはDOT製品操作を使用できます。これは、より速くなる傾向があり、標準範囲外の角度を心配する必要がないためです。
次のコードは、次の結果を達成する必要があります。
double CanonizeAngle(double angle)
{
if (angle > Math.PI)
{
do
{
angle -= MathHelper.TwoPi;
}
while (angle > Math.PI);
}
else if (angle < -Math.PI)
{
do
{
angle += MathHelper.TwoPi;
} while (angle < -Math.PI);
}
return angle;
}
double VectorToAngle(Vector2 vector)
{
Vector2 direction = Vector2.Normalize(vector);
return Math.Atan2(direction.Y, direction.X);
}
bool IsPointWithinCone(Vector2 point, Vector2 conePosition, double coneAngle, double coneSize)
{
double toPoint = VectorToAngle(point - conePosition);
double angleDifference = CanonizeAngle(coneAngle - toPoint);
double halfConeSize = coneSize * 0.5f;
return angleDifference >= -halfConeSize && angleDifference <= halfConeSize;
}
他のヒント
+/-角度をテストしたいと思う +(すなわち angleDifference >= -ViewAngle/2 && angleDifference <= ViewAngle/2
)。または絶対値を使用します。
所属していません StackOverflow