1

对于那些熟悉 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);
    }
}

还有我的雪碧课

    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 可以处理一大堆东西。那么有人知道为什么我的碰撞不起作用吗?

4

1 回答 1

0

是的,您的定位已关闭,因为从我玩弄引擎中获得的结果来看,它使用米中的屏幕坐标。这意味着您的屏幕只能是 10x16(ish) 米(480x800 像素),因为这是微软的屏幕分辨率上限。因此,当您传入 16000,1500 像素的位置并将其除以 50 时,您实际上将物体放置在 320m 和 30m 处,这对于物体来说是远离屏幕的,但对于 Draw 函数来说是在屏幕上,因为它认为这些值以像素为单位。

同样从我在他们网站上的 HelloWorld 示例中看到的内容,您应该使用 64 除以不是 50。我不确定为什么会这样,但我可以猜到这是因为 64 是 2 的因数,而计算机喜欢因数2 大声笑所以如果你使用 50 你可能会发生一些碰撞。

于 2011-09-17T10:39:53.333 回答