1

farseer に詳しい方に、衝突検出について質問があります。Joran Omark による XNAtutorial の優れたビデオ チュートリアルをたくさん読みました。残念ながら、彼は古いバージョンの farseer (2006 年版) を使用していたので、私のものを機能させるには少し調整する必要がありました。これで、衝突検出を除いてすべてが機能します。私はそれらを機能させることができないようです。私はスクリーンマネージャーのアプローチを採用することに決めたので、素敵な個別のクラスを作成しました。

私の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 が大量のものを処理できるようになりました。では、なぜ私の衝突が機能しないのか、誰にもわかりませんか?

4

1 に答える 1

0

ええ、あなたはポジショニングがオフになっています。なぜなら、私がエンジンで遊んで得たものから、それはメートルの画面座標を使用しているからです。つまり、Microsoftの画面解像度には10x16(ish)メートル(480x800ピクセル)しかありません。したがって、16000,1500ピクセルの位置を通過し、それを50で割ると、実際にはボディを320mと30mに配置します。これは、ボディの画面からはかなり離れていますが、描画機能の画面では、これらの値を考慮しているためです。ピクセル単位です。

また、彼らのサイトにあるHelloWorldの例で見たものから、64を使用して50ではなく除算する必要があります。それが正確である理由はわかりませんが、64は2の因数であり、コンピューターは因数が大好きだからだと推測できます。 2 lolなので、50を使用すると、衝突が発生する可能性があります。

于 2011-09-17T10:39:53.333 に答える