我读了很多关于“像素米”、视口等的内容。 我想,我在某种程度上已经明白了。 现在已经创建了一个 scene2d 来测试。 但我认为地箱的转换有问题。 香蕉以正常速度飞到地面(接地箱)。然而,接触地面后,香蕉会再次被扔掉(向上)。重力和其他一切都设置正确。对于另一个没有转换 P2M 的项目,它可以工作,但速度非常慢。 如果有人能够准确地解释(可能用代码)如何最好地将 box2D 与 szene2d 结合使用,那就太好了。 我只是无法再进一步了。网上的解释总是很难实施。
P2M.PPM=100; 1 米 = 100 像素(然后会看到 Viewport Box2d 尺寸宽度 10,高度 8)
public BaseScreen()
{
assMan.loadAll();
assMan.manager.finishLoading();
mainStage = new Stage(viewport=new ExtendViewport(1000,800));
uiStage = new Stage(viewport=new ExtendViewport(1000,800));
在基本屏幕中调整大小:
// methods required by Screen interface
public void resize(int width, int height) {
viewport.update(width, height);
}
关卡屏幕:
public void initialize()
{
batch = new SpriteBatch();
TextureAtlas atlas;
atlas = new TextureAtlas(Gdx.files.internal("sprites.txt"));
TextureAtlas.AtlasRegion region = atlas.findRegion("banana");
bananas = atlas.createSprite("banana");
test = atlas.createSprite("banana");
//camera1 = new OrthographicCamera(); // viewport1 = new ExtendViewport(10, 8, 相机1);
Box2D.init();
world = new World(new Vector2(0, -20), true);
physicsBodies = new PhysicsShapeCache("physics.xml");
debugRenderer = new Box2DDebugRenderer();
createGround();
generateFruit();
createCollisionListener();
/* BaseActor space = new BaseActor(0,0, mainStage);
space.loadTexture("badlogic.jpg");
space.setSize(1000,800);
BaseActor.setWorldBounds(space);*/
}
private void generateFruit() {
float width = bananas.getWidth() * SCALE;
float height = bananas.getHeight() * SCALE;
bananas.setSize(width, height);
bananas.setOrigin(0, 0);
name="banana";
frucht=createBody(name,200 / P2M.PPM,800 /P2M.PPM,0);
}
private Body createBody(String name, float x, float y, float rotation) {
Body body = physicsBodies.createBody(name, world, SCALE, SCALE);
body.setUserData("BANANE");
body.setTransform(x, y, rotation);
return body;
}
private void drawSprite(String name1, float x, float y, float degrees) {
Sprite sprite = bananas;//sprites.get(name);
sprite.setPosition(x, y);
sprite.setRotation(degrees);
sprite.draw(batch);
}
private void createGround() {
if (ground != null) world.destroyBody(ground);
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.friction = 1;
PolygonShape shape = new PolygonShape();
shape.setAsBox(1000 , 0.9f );//0.5f 1000 / P2M.PPM +20
fixtureDef.shape = shape;
ground = world.createBody(bodyDef);
ground.createFixture(fixtureDef);
ground.setTransform(0 , 0 , 0);
ground.setUserData("BODEN");
shape.dispose();
}
public void update(float dt)
{
stepWorld();
int numContacts = world.getContactCount();
if (numContacts > 0) {
Gdx.app.log("contact", "start of contact list");
for (Contact contact : world.getContactList()) {
Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();
/*Gdx.app.log("contact", "between " + fixtureA.getBody().getUserData()
+ " and " + fixtureB.getBody().getUserData());*/
String a= fixtureA.getBody().getUserData().toString();
String b= fixtureB.getBody().getUserData().toString();
Gdx.app.log("contact", "between " + a
+ " and " + b);
//Kollidiert Banane mit Boden?******************************************************
if(a.equals("BANANE") && b.equals("BODEN")){
Gdx.app.log("TEST", "TEST ERFOLGREICH");
}
//Alles andere Auswerten...
//**********************************************************************************
//Mit statischen body kollidiert?***************************************************
if(fixtureA.getBody().getType() == BodyDef.BodyType.StaticBody){
Gdx.app.log("Contact", "A=STATISCH");
}
if(fixtureB.getBody().getType() == BodyDef.BodyType.StaticBody){
Gdx.app.log("Contact", "B=STATISCH");
}
//**********************************************************************************
}
Gdx.app.log("contact", "end of contact list");
}
batch.begin();
// test.draw(batch);
Body body = frucht;
String name1 = "banana";
Vector2 position = body.getPosition();
float degrees = (float) Math.toDegrees(body.getAngle());
drawSprite(name1, position.x * P2M.PPM, position.y * P2M.PPM, degrees);
Vector2 position1 = ground.getPosition();
batch.end();
// uncomment to show the polygons
// debugRenderer.render(world,camera1.combined);
}
private void stepWorld() {
float delta = Gdx.graphics.getDeltaTime();
accumulator += Math.min(delta, 0.25f);
if (accumulator >= STEP_TIME) {
accumulator -= STEP_TIME;
world.step(STEP_TIME, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
}
}
物体相互弹跳是正常现象。这可以通过使用
FixtureDef
创建主体时设置恢复来控制。但我看到您正在使用某种方式从文件physics.xml
加载数据,所以我认为您可以控制创建此文件的程序中的恢复。要禁用弹跳,您需要将该值设置为 0。