繁体   English   中英

Farseer Physics对象不太接触

[英]Farseer Physics objects don't quite touch

我刚刚让Farseer Physics工作,我创建了一个快速/粗略的基础对象,我可以用来轻松创建对象。

所以我设置了一个快速模拟,它看起来像这样: 盒子没有碰撞的更精简的例子

在DebugView中,它看起来像这样: 调试模式下框的Farseer示例

仔细检查后,启用两种模式后,我可以看到橙色框中缺少一行和一列像素: 缺少纹理上的像素

任何人都知道为什么会这样吗?

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填满整个文件。

Farseer中的多边形周围有一层薄薄的“皮肤”。 它们不会完全相互接触 - 但会被这个皮肤抵消。 这是设计的。

有关更多详细信息,请参阅Settings.PolygonRadius 它基本上可以帮助改善碰撞检测。

默认情况下,多边形半径为0.01物理单位。 你会注意到,在1 unit = 100 pixels比例下,多边形半径恰好等于一个像素 - 因此你的对象之间有一个像素间隙。

对此最简单的解决方法可能是使每个方向的矩形尺寸减小两倍。 或者,您可以将纹理缩放得稍大,以考虑半径。

(不要关闭半径本身 - 你会遇到物理故障。)


不要忘记,Farseer可以处理尺寸和重量从0.1到10个单位的物体。 传统上你会使用米和千克这些尺度(虽然你不必)。

我个人认为像素到单位的转换有点难看,因为你最终会在项目中散布容易出错的缩放代码。 对于实质性的东西,我建议在物理空间中编码所有内容,然后使用相机矩阵(可以传递给SpriteBatch.Begin )在渲染时缩放所有内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM