先知身体的高度和宽度

本文关键字:高度 先知 | 更新日期: 2023-09-27 17:54:43

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

 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;
 }

怎么了?我怎样才能得到另一个身体的宽度和高度?

先知身体的高度和宽度

试试这个!=}

我没有测试过,但我认为它工作得很好。

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;
    }
}