Farseer身体的高度和宽度?

时间:2014-03-29 16:26:24

标签: c# xna farseer

我有一个名为PersonA的身体。如果PersonA与另一个身体发生碰撞,则应保存该身体的宽度和高度。我试过这样的但是我总是收到错误信息" Width"和"身高"不存在。

 bool PersonA_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
 {
    //How tall and broad is the body that collided with PersonA 
    float Width = fixtureB.Body.Width;
    float Height = fixtureB.Body.Height;

    return true;
 }

有什么问题?如何获得另一个身体的宽度和高度?

1 个答案:

答案 0 :(得分:0)

试试这个! =}

我没有测试过密钥,但我觉得它运行正常。

class GameBody
{
    public Body body;
    public Vector2 position;
    public int width;
    public int height;

    public GameBody(int width, int height, int x, int y, BodyType bodyType)
    {
        this.width = width;
        this.height = height;

        body = BodyFactory.CreateRectangle(Game1.world, ConvertUnits.ToSimUnits((float)width), ConvertUnits.ToSimUnits((float)height), 1f);
        body.BodyType = bodyType;
        body.FixedRotation = false;
        body.SleepingAllowed = true;
        body.LinearDamping = 1f;
        body.AngularDamping = 2f;
        setPosition(x, y);

        body.UserData = this;

        body.OnCollision += gameBody_OnCollision;
    }

    public void setPosition(float x, float y)
    {
        position.X = x;
        position.Y = y;
        body.SetTransform(new Vector2(ConvertUnits.ToSimUnits(x), ConvertUnits.ToSimUnits(y)), 0);
    }

    bool gameBody_OnCollision(Fixture me, Fixture him, Contact contact)
    {
        float Width = (him.Body.UserData as GameBody).width;
        float Height = (him.Body.UserData as GameBody).height;

        return true;
    }

}