我刚刚 Farseer Physics 正在工作,我已经创建了一个快速/粗略的基础对象,我可以用来轻松创建对象。
所以我设置了一个快速模拟,它看起来像这样:
在DebugView中,它看起来像这样:仔细检查后,启用两种模式后,我可以看到橙色框中缺少一行和一列像素:
任何人都知道为什么会这样?
class BasePhys
{
public float unitToPixel;
public float pixelToUnit;
public Vector2 size;
public Body body;
public Texture2D texture;
public BasePhys(World world, int x, int y)
{
this.unitToPixel = 100.0f;
this.pixelToUnit = 1 / unitToPixel;
TextureManager.GetTextureByName("base", ref this.texture);
this.size = new Vector2(this.texture.Width, this.texture.Height);
this.body = BodyFactory.CreateRectangle(world, this.size.X * this.pixelToUnit, this.size.Y * this.pixelToUnit, 1);
this.body.BodyType = BodyType.Dynamic;
this.body.Position = new Vector2(x * this.pixelToUnit, y * this.pixelToUnit);
}
public void Draw(SpriteBatch spriteBatch)
{
Vector2 scale = new Vector2(this.size.X / (float)this.texture.Width, this.size.Y / (float)this.texture.Height);
Vector2 position = this.body.Position * unitToPixel;
spriteBatch.Draw(this.texture, position, null, Color.White, this.body.Rotation, new Vector2(this.texture.Width / 2.0f, this.texture.Height / 2.0f), scale, SpriteEffects.None, 0);
}
}
有谁知道为什么会这样?它并没有在所有的Farseer演示中都这样做。我检查了纹理,它没有隐藏pixels
,橙色pixels
填充整个文件。
答案 0 :(得分:1)
Farseer中的多边形周围有一层薄薄的“皮肤”。它们不会完全相互接触 - 但会被这个皮肤抵消。这是设计的。
有关详细信息,请参阅Settings.PolygonRadius
。它基本上可以帮助改善碰撞检测。
默认情况下,多边形半径为0.01
物理单位。你会注意到,在1 unit = 100 pixels
的范围内,多边形半径恰好等于一个像素 - 因此你的对象之间有一个像素间隙。
对此最简单的解决方法可能是使每个方向的矩形尺寸减小两倍。或者,您可以将纹理缩放得稍大,以便考虑半径。
(不要关闭半径本身 - 你会遇到物理故障。)
不要忘记,Farseer可以处理大小和重量从0.1到10个单位的物体。传统上,你会使用米和千克这些尺度(虽然你没有)。
就个人而言,我发现像素到单位的转换有点难看,因为你最终会在整个项目中散布容易出错的缩放代码。对于实质性的东西,我建议在物理空间中编码所有内容,然后使用相机矩阵(可以传递给SpriteBatch.Begin
)在渲染时缩放所有内容。