我对在Box2D身体上绘制雪碧产生了沉重的问题。Libgdx Box2D - 在身体上绘制雪碧 - 沉重的问题
我正在创造一个平台游戏,我之前在一个身体上绘制了一个精灵,但后来才意识到我的重力真的很飘。谷歌搜索后,我发现我应该使用Box2D时使用米,我改变了我的代码使用像素到米的转换比为25.
从那时起,我无法让所有的东西都工作,虽然我的精灵只是不会吸引我的身体。
摄像头:
float width = Gdx.graphics.getWidth() * PIXELS_TO_METERS;
float height = Gdx.graphics.getHeight() * PIXELS_TO_METERS;
camera = new OrthographicCamera(width/2, height/2);
camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0);
camera.update();
这里是我的身体代码:
idleRegion = new TextureRegion(xeonTexture, 20, 13, 50, 65);
xeonSprite = new Sprite(idleRegion);
//Physics
bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(100 * PIXELS_TO_METERS, 100 * PIXELS_TO_METERS);
bodyDef.fixedRotation = true;
body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox((xeonSprite.getWidth()/2) * PIXELS_TO_METERS, (xeonSprite.getHeight()/2) * PIXELS_TO_METERS);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
fixtureDef.friction = 1f;
fixtureDef.restitution = 0f;
fixtureDef.isSensor = false;
physicsFixture = body.createFixture(fixtureDef);
这是我如何设置我的精灵位置:
final float widthD2 = (xeonSprite.getWidth()/2);
final float heightD2 = (xeonSprite.getHeight()/2);
final float angle = this.getBodyAngle();
xeonSprite.setOrigin(widthD2, heightD2);
xeonSprite.setPosition(body.getPosition().x - xeonSprite.getWidth()/2, body.getPosition().y - xeonSprite.getHeight()/2);
xeonSprite.setRotation((float) Math.toRadians(angle));
我也试过如下:
xeonSprite.setPosition(body.getPosition().x - xeonSprite.getWidth()/2 * METERS_TO_PIXELS, body.getPosition().y - xeonSprite.getHeight()/2 * METERS_TO_PIXELS);
这里是我画我的雪碧:
penguinBatch.begin();
xeon.getPenguinSprite(stateTime, Gdx.graphics.getDeltaTime()).draw(penguinBatch);
penguinBatch.end();
不要使用Pixel来测量对话,只需设置好相机即可。例如,您是否希望在屏幕上看到x方向100米,相机的宽度应该为100.相机将像素值为您在引擎盖下进行对话。更好的解决方案是使用'Viewport',阅读wiki获取更多信息。 – Springrbua 2014-10-30 15:43:51
也添加了一些微不足道的方式来缩小/大量数量,看看,如果由于错误的缩放或中心没有设置,精灵是“某处/巨大/微观”等。 – cfrick 2014-10-30 18:13:39