我對在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