我如何计算精灵的“最远”边缘以围绕具有原点的变换精灵创建矩形轮廓?
我想实现这样的东西 http://oi43.tinypic.com/14l39k0.jpg http://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);
它根据精灵的旋转来查找精灵角的最大值和最小值以制作边界框。 代码假设原点是精灵的中间,您必须根据原点更改代码