更改FixtureDef属性Java Libgdx [英] Changing FixtureDef properties Java Libgdx

查看:88
本文介绍了更改FixtureDef属性Java Libgdx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在游戏中使用Factory Pattern创建Box2D实体.这是创建主体的代码:

I use a Factory Pattern for creating Box2D bodies in my game. Here is the code that creates the bodies:

public static HashMap<String, Object> createAndGet(Vector2 position, Vector2 dimensions,
        BodyType type, boolean isCircle){
    HashMap<String, Object> bodyObjectsHash = new HashMap<String, Object>();

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = type;
    bodyDef.position.set(new Vector2(position.x, position.y));

    Body body = EntityManager.createBody(bodyDef, dimensions);

    FixtureDef fixtureDef = new FixtureDef();
    Fixture fixture;
    if(isCircle){
        CircleShape circle = new CircleShape();
        circle.setRadius(dimensions.x);
        fixtureDef.shape = circle;
        fixture = body.createFixture(fixtureDef);
        circle.dispose();
    }else{
        PolygonShape rectangle = new PolygonShape();
        rectangle.setAsBox(dimensions.x, dimensions.y);
        fixtureDef.shape = rectangle;
        fixture = body.createFixture(fixtureDef);
        rectangle.dispose();
    }


    bodyObjectsHash.put(BodyReferences.BODY, body);
    bodyObjectsHash.put(BodyReferences.BODY_DEF, bodyDef);
    bodyObjectsHash.put(BodyReferences.FIXTURE, fixture);
    bodyObjectsHash.put(BodyReferences.FIXTURE_DEF, fixtureDef);


    return bodyObjectsHash;


}

现在,您将看到这将返回一个HashMap.键是字符串,并返回一个对象.这样可以完成以下代码:

Now, you will see this returns a HashMap. The key is strings and returns an object. This is so code like this can be accomplished:

public void attachNewSprite(String internalPath){
    entitySprite = new Sprite(new Texture(Gdx.files.internal(internalPath)));
    ((Body)bodyObjects.get(BodyReferences.BODY)).setUserData(entitySprite);
}

返回的HashMap存储在bodyObjects

The HashMap returned is stored in bodyObjects

但是,您会注意到,在用于创建实体的静态方法中,未设置诸如密度和摩擦之类的属性.这是因为我认为可以通过返回对象来编辑它们.这是我假设要编辑FixtureDef的方式:

However you will notice, in the static method for creating bodies, properties such as density and friction are not set. This is because I thought by returning the objects I may edit them. Here is how I assumed I would edit the FixtureDef:

public void addFixtureDefProperties(float density, float friction, float restitution){
    ((FixtureDef)(bodyObjects.get(BodyReferences.FIXTURE_DEF))).density = density;
    ((FixtureDef)(bodyObjects.get(BodyReferences.FIXTURE_DEF))).friction = friction;
    ((FixtureDef)(bodyObjects.get(BodyReferences.FIXTURE_DEF))).restitution = restitution;

    /*This line will be discussed */
    ((Body)(bodyObjects.get(BodyReferences.BODY))).getFixtureList().removeRange(0, ((Body)(bodyObjects.get(BodyReferences.BODY))).getFixtureList().size - 1);
    ((Body)(bodyObjects.get(BodyReferences.BODY))).createFixture(((FixtureDef)(bodyObjects.get(BodyReferences.FIXTURE_DEF))));
} 

因此,我实际上是设置了新属性,从主体中删除了fixturedef,然后添加了新属性.这样做时,我得到一个fatal error.我删除了删除fixturedef的行,并得到相同的fatal error.这是致命错误:

SO I essentially set the new properties, remove the fixturedef from the body then add the new one. I get a fatal error when doing this. I removed the line that removes the fixturedef's and get the same fatal error. Here is that fatal error:

有人可以帮助我找到一种编辑这些属性的方法,这样我就不必用更多参数填充我的静态createBody方法了.谢谢!

Can someone please help me find a way to edit these properties so I don't have to flood my static createBody method with even more parameters. Thank you!

推荐答案

您可以使用夹具的设置方法(形状除外)更改夹具的属性.您可以使用getter方法获取所有属性.仔细查看Fixture API.因此,您不需要保存fixtureDef对象. BodyDef和Body具有相似的关联.

You can change the properties of a fixture using its setter-methods, except its Shape. You can get all properties with getter-methods. Have a closer look on the Fixture API. Thus you do not need to save the fixtureDef objects. BodyDef and Body are related similarly.

这篇关于更改FixtureDef属性Java Libgdx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆