对于那些熟悉farseer的人,我有一个关于碰撞检测的问题。我已经阅读了很多内容,并观看了Joran Omark撰写的关于XNAtutorial的优秀视频教程。不幸的是他使用了旧版本的farseer(2006版本),所以我不得不稍微调整一下以让我的工作。现在一切正常,除了我的碰撞检测。我似乎无法让那些工作。我决定采用screenmanager方法,因此创建了很好的单独类。
我的GameplayScreen看起来像这样
public class GamePlayScreen : GameScreen
{
Texture2D stewieTexture;
Texture2D floorTexture;
Sprite stewie;
Sprite floor;
World world;
public GamePlayScreen()
{
world = new World(new Vector2(0, 9.81F));
EnabledGestures = GestureType.Flick;
}
public override void LoadContent()
{
stewieTexture = ScreenManager.Game.Content.Load<Texture2D>("playerStewie");
floorTexture = ScreenManager.Game.Content.Load<Texture2D>("Floor");
this.stewie = new Sprite();
this.floor = new Sprite();
this.stewie.LoadGraphicsContent(ScreenManager.SpriteBatch, this.stewieTexture, world);
this.floor.LoadGraphicsContent(ScreenManager.SpriteBatch, this.floorTexture, world);
// very ugly unfortunately for now like this.. We need Textures for our sprite that's why
this.stewie.Initialize(world, BodyType.Dynamic ,new Vector2(16000,1500));
this.floor.Initialize(world, BodyType.Static, new Vector2(1500, 16000));
}
public override void HandleInput(InputState input)
{
foreach (GestureSample gesture in input.Gestures)
{
if (gesture.GestureType == GestureType.Flick)
{
stewie.PhysicsBody.ApplyForce(Vector2.Divide(gesture.Delta, 0.5f));
}
}
}
public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
{
world.Step((float)gameTime.ElapsedGameTime.TotalSeconds);
this.stewie.Update(gameTime, world);
this.floor.Update(gameTime, world);
base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
}
public override void Draw(GameTime gameTime)
{
// TODO: Add your drawing code here
ScreenManager.SpriteBatch.Begin();
this.stewie.Draw(gameTime);
this.floor.Draw(gameTime);
ScreenManager.SpriteBatch.End();
base.Draw(gameTime);
}
}
我的Sprite类
public class Sprite
{
private Animation graphicsImage;
private Body physicsBody;
//Fixture test
//private Fixture fixtureBody;
private Vector2 _screenCenter;
private float pixelsPerMeter = 50; //needed to convert pixels per meter , because the gesture.position is in meters
public Body PhysicsBody { get { return physicsBody; } }
public Sprite()
{
this.graphicsImage = new Animation();
}
/// <summary>
/// Initialize the sprite sets up position , creates rectangle from bodyfactory
/// </summary>
/// <param name="world"> takes a reference to the worldobject</param>
public void Initialize(World world, BodyType bodyType,Vector2 position)
{
this.graphicsImage.Initialize();
this.physicsBody = BodyFactory.CreateRectangle(world, this.graphicsImage.Size.X / pixelsPerMeter, this.graphicsImage.Size.Y / pixelsPerMeter, 1f);
this.graphicsImage.Position = position / pixelsPerMeter;
this.physicsBody.Position = this.graphicsImage.Position;
this.physicsBody.BodyType = bodyType;
// this.fixtureBody = FixtureFactory.AttachRectangle(this.physicsBody.Position.X / pixelsPerMeter, this.physicsBody.Position.Y / pixelsPerMeter, 3f, new Vector2(100, 100), this.physicsBody);
//this.physicsBody.Restitution = 0.7f;
Debug.WriteLine("Initialize Sprite");
Debug.WriteLine(this.graphicsImage.Position);
}
/// <summary>
/// LoadGraphicsContent for the sprite call this function in the LoadContent ( initialize also works )
/// </summary>
/// <param name="spriteBatch">takes a reference to a spritebatch</param>
/// <param name="texture">takes a reference to texture2D</param>
public void LoadGraphicsContent(SpriteBatch spriteBatch, Texture2D texture, World world)
{
this.graphicsImage.LoadGraphicsContent(spriteBatch, texture);
}
/// <summary>
/// Update sprite for graphicsImage position , and rotation needs to be called in the main Update function of the game
/// </summary>
/// <param name="gameTime">reference to gameTime</param>
/// <param name="world">reference to worldobject</param>
public void Update(GameTime gameTime, World world)
{
this.graphicsImage.Update(gameTime);
this.graphicsImage.Position = this.physicsBody.Position;
this.graphicsImage.Rotation = this.physicsBody.Rotation;
}
/// <summary>
/// Draw sprite method needs to be called in the main Draw function of the game
/// </summary>
/// <param name="gameTime"></param>
public void Draw(GameTime gameTime)
{
this.graphicsImage.Draw(gameTime);
}
}
}
正如我已经说过的那样,我的碰撞是不起作用的。我查看了使用Farseer 3.3的当前样本(就像我一样)。 例如,这里是http://farseerphysics.codeplex.com/releases/view/64108,然后是HelloWorld示例。 我已经阅读了有关GeomFactory的内容,这些内容正在以前的版本中用于碰撞。然而,随着新版本的Farseer的改变,bodyfactory可以处理大量的东西。 那么有谁知道为什么我的碰撞不起作用?
答案 0 :(得分:0)
是的,你的定位是关闭的,因为我从使用引擎获得的内容中使用了Meters中的屏幕坐标。这意味着您的屏幕只能是10x16(ish)米(480x800像素),因为这就是微软的屏幕分辨率上限。因此,当你传入16000,1500像素的位置并将其除以50时,你实际上是将身体放在320米和30米处,这对于身体而言在屏幕之外,但是在屏幕上用于绘制功能,因为它认为这些值以像素为单位。
同样从我在他们网站上的HelloWorld示例中看到的,您应该使用64来除以不是50.我不确定为什么这是完全但我猜它是因为64是因子2和计算机喜欢2 lol的因素,所以如果你使用50,你可能会发生一些碰撞事件。