我怎樣才能計算出一個精靈的「最遠」的邊緣來創建一個圍繞一個具有原點的變換精靈的矩形輪廓 ?XNA二維框大綱/邊界框?
我想要實現這樣的 http://oi43.tinypic.com/14l39k0.jpghttp://i42.tinypic.com/2m62v41.png 其中紅色框是「綱要」和黑盒子是 轉換精靈。盒子需要根據角落展開 - 真的只有 包圍盒子。
我已經嘗試了各種方程式喜歡這個找 轉換精靈的座標:
Transformed.X = pos.X * (float)Math.Cos(angle) - pos.Y * (float)Math.Sin(angle); Transformed.Y = pos.X * (float)Math.Sin(angle) + pos.Y * (float)Math.Cos(angle);
,但我似乎無法使它發揮作用。任何想法如何我可以實現這一目標?
任何幫助,將不勝感激。
胡安
與感謝Zenchovey我能解決我的問題。下面是我使用的代碼:
啓動變量
Vector2 TransformPos = Vector2.Zero;
Vector2 TransformPos2 = Vector2.Zero;
float[] px = new float[2];
float[] py = new float[2];
float[] pxl = new float[2];
float[] pyl = new float[2];
float ox;
float oy;
更新方法
// Vars
ox = pos.X;
oy = pos.Y;
// top left
pxl[0] = pos.X - Origin.X;
pyl[0] = pos.Y - Origin.Y;
// bottom left
pxl[1] = pos.X - Origin.X;
pyl[1] = pos.Y + Origin.Y;
// top right
px[0] = pos.X + Origin.X;
py[0] = pos.Y - Origin.Y;
// bottom right
px[1] = pos.X + Origin.X;
py[1] = pos.Y + Origin.Y;
if (rot <= MathHelper.ToRadians(90) && rot >= MathHelper.ToRadians(0))
{
TransformPos.X = (float)Math.Cos(rot) * (pxl.Min() - ox) - (float)Math.Sin(rot) * (pyl.Max() - oy) + ox;
TransformPos.Y = (float)Math.Sin(rot) * (pxl.Min() - ox) + (float)Math.Cos(rot) * (pyl.Min() - oy) + oy;
TransformPos2.X = (float)Math.Cos(rot) * (px.Max() - ox) - (float)Math.Sin(rot) * (py.Min() - oy) + ox;
TransformPos2.Y = (float)Math.Sin(rot) * (px.Max() - ox) + (float)Math.Cos(rot) * (py.Max() - oy) + oy;
}
else
if (rot <= MathHelper.ToRadians(270) && rot >= MathHelper.ToRadians(180))
{
TransformPos2.X = (float)Math.Cos(rot) * (pxl.Min() - ox) - (float)Math.Sin(rot) * (pyl.Max() - oy) + ox;
TransformPos2.Y = (float)Math.Sin(rot) * (pxl.Min() - ox) + (float)Math.Cos(rot) * (pyl.Min() - oy) + oy;
TransformPos.X = (float)Math.Cos(rot) * (px.Max() - ox) - (float)Math.Sin(rot) * (py.Min() - oy) + ox;
TransformPos.Y = (float)Math.Sin(rot) * (px.Max() - ox) + (float)Math.Cos(rot) * (py.Max() - oy) + oy;
}
else
if (rot <= MathHelper.ToRadians(180) && rot >= MathHelper.ToRadians(90))
{
TransformPos2.X = (float)Math.Cos(rot) * (pxl.Max() - ox) - (float)Math.Sin(rot) * (pyl.Min() - oy) + ox;
TransformPos.Y = (float)Math.Sin(rot) * (pxl.Max() - ox) + (float)Math.Cos(rot) * (pyl.Max() - oy) + oy;
TransformPos.X = (float)Math.Cos(rot) * (px.Min() - ox) - (float)Math.Sin(rot) * (py.Max() - oy) + ox;
TransformPos2.Y = (float)Math.Sin(rot) * (px.Min() - ox) + (float)Math.Cos(rot) * (py.Min() - oy) + oy;
}
else
if (rot <= MathHelper.ToRadians(360) && rot >= MathHelper.ToRadians(270))
{
TransformPos.X = (float)Math.Cos(rot) * (pxl.Max() - ox) - (float)Math.Sin(rot) * (pyl.Min() - oy) + ox;
TransformPos2.Y = (float)Math.Sin(rot) * (pxl.Max() - ox) + (float)Math.Cos(rot) * (pyl.Max() - oy) + oy;
TransformPos2.X = (float)Math.Cos(rot) * (px.Min() - ox) - (float)Math.Sin(rot) * (py.Max() - oy) + ox;
TransformPos.Y = (float)Math.Sin(rot) * (px.Min() - ox) + (float)Math.Cos(rot) * (py.Min() - oy) + oy;
}
Transform = new Rectangle((int)TransformPos.X, (int)TransformPos.Y, (int)TransformPos2.X - (int)TransformPos.X, (int)TransformPos2.Y - (int)TransformPos.Y);
它看起來對精靈的角落基於它的最大值和最小值的自轉,使邊框。 的代碼假設原點是精靈的中間,你將有基於原點
請添加langage標籤 –