[英]Farseer Physics objects don't quite touch
我剛剛讓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
填滿整個文件。
Farseer中的多邊形周圍有一層薄薄的“皮膚”。 它們不會完全相互接觸 - 但會被這個皮膚抵消。 這是設計的。
有關更多詳細信息,請參閱Settings.PolygonRadius
。 它基本上可以幫助改善碰撞檢測。
默認情況下,多邊形半徑為0.01
物理單位。 你會注意到,在1 unit = 100 pixels
比例下,多邊形半徑恰好等於一個像素 - 因此你的對象之間有一個像素間隙。
對此最簡單的解決方法可能是使每個方向的矩形尺寸減小兩倍。 或者,您可以將紋理縮放得稍大,以考慮半徑。
(不要關閉半徑本身 - 你會遇到物理故障。)
不要忘記,Farseer可以處理尺寸和重量從0.1到10個單位的物體。 傳統上你會使用米和千克這些尺度(雖然你不必)。
我個人認為像素到單位的轉換有點難看,因為你最終會在項目中散布容易出錯的縮放代碼。 對於實質性的東西,我建議在物理空間中編碼所有內容,然后使用相機矩陣(可以傳遞給SpriteBatch.Begin
)在渲染時縮放所有內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.