【发布时间】:2013-07-04 15:20:54
【问题描述】:
我如何计算精灵的“最远”边缘来创建一个 具有原点的变形精灵周围的矩形轮廓?
我想实现这样的目标 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);
它会根据精灵的旋转来寻找精灵角落的最大值和最小值来制作边界框。 代码假设原点是精灵的中间,你必须根据原点更改代码
【问题讨论】:
-
请添加语言标签