gpt4 book ai didi

c# - Farseer 物理对象不太接触

转载 作者:行者123 更新时间:2023-11-30 17:56:20 25 4
gpt4 key购买 nike

我刚刚开始使用 Farseer Physics,并且创建了一个快速/粗略的基础对象,我可以使用它来轻松创建对象。

所以我设置了一个快速模拟,它看起来像这样:Farseer example of boxes not colliding

在 DebugView 中,它看起来像这样:Farseer example of boxes in debug mode

仔细观察,启用两种模式后,我可以看到橙色框缺少一行和一列像素:Missing pixel on texture

有人知道为什么会这样吗?

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 单位 = 100 像素 的比例下,多边形半径恰好等于一个像素 - 因此对象之间的间隙为一个像素。

可能最简单的解决方法是在每个方向上将矩形尺寸缩小两倍的多边形半径。或者,您可以将纹理缩放得稍微大一些以考虑半径。

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


不要忘记,Farseer 可以处理大小和重量从 0.1 到 10 个单位不等的物体。传统上,您会使用米和千克来表示这些比例(尽管您不必这样做)。

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

关于c# - Farseer 物理对象不太接触,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14286125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com