Java Stage和Box2d相机未对齐
在我的第一个游戏中,我使用Scened2d+Box2d独立渲染我的身体,而不扩展Java Stage和Box2d相机未对齐,java,libgdx,box2d,Java,Libgdx,Box2d,在我的第一个游戏中,我使用Scened2d+Box2d独立渲染我的身体,而不扩展Actor。它是有效的,但它是混乱的,不推荐使用 现在在我的第二个游戏中,我想通过使用物理stagestage在Scened2d和Box2d之间建立一座桥梁 我发现这段代码基本上满足了我的需要。但是,即使在名为GameStage的我的stage类的Draw()方法中使用了mDebugRenderer.render(world,camera.combined),stage和box2d相机也不会对齐 我已经看过了,但是运
Actor
。它是有效的,但它是混乱的,不推荐使用
现在在我的第二个游戏中,我想通过使用物理stagestage
在Scened2d和Box2d之间建立一座桥梁
我发现这段代码基本上满足了我的需要。但是,即使在名为GameStage
的我的stage
类的Draw()
方法中使用了mDebugRenderer.render(world,camera.combined)
,stage和box2d相机也不会对齐
我已经看过了,但是运气不好。
我知道这可能很简单,但却伤了我的头
结果如下:
以下是完整的“工作”源代码:
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
public class Box2D_Platformer extends Game {
public World mBoxWorld;
public Box2DDebugRenderer mDebugRenderer;
public OrthographicCamera mCam;
public AssetManager mManager;
public sprite_actor mBall;
public GameStage stage;
public SpriteBatch batch;
public static final float PIXEL_TO_METER_RATIO_DEFAULT = 32f;
static final float one_over_60 = 1f / 60f;
@Override
public void create() {
init();
batch = new SpriteBatch();
stage = new GameStage();
Gdx.input.setInputProcessor(stage);
stage.setupStage(mCam, mBoxWorld, mDebugRenderer);
stage.addActor(mBall);
}
@Override
public void render() {
stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
render_physics();
stage.draw();
}
@Override
public void dispose() {
stage.dispose();
mManager.dispose();
}
private void init() {
mManager = new AssetManager();
mCam = new OrthographicCamera(1024, 768);
mCam.update();
init_physics();
init_ball();
}
private void init_physics() {
mBoxWorld = new World(new Vector2(0, -200), true);
mDebugRenderer = new Box2DDebugRenderer();
create_static_object_floor();
}
private void init_ball() {
TextureAtlas atlasSprites;
mManager.load("data/atlas.txt",TextureAtlas.class);
mManager.finishLoading();
atlasSprites = mManager.get("data/atlas.txt");
TextureRegion t = atlasSprites.findRegion("bar");
//
mBall = new sprite_actor(mBoxWorld, new Sprite(t));
}
private Body create_dynamic_object_ball() {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(100 / PIXEL_TO_METER_RATIO_DEFAULT,5000 / PIXEL_TO_METER_RATIO_DEFAULT );
Body body = mBoxWorld.createBody(bodyDef);
// Create a circle shape and set its radius to 6
CircleShape circle = new CircleShape();
circle.setRadius(1f*PIXEL_TO_METER_RATIO_DEFAULT);
// Create a fixture definition to apply our shape to
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = circle;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 1f; // Make it bounce a little bit
// Create our fixture and attach it to the body
Fixture fixture = body.createFixture(fixtureDef);
circle.dispose();
return body;
}
private Body create_static_object_floor() {
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vector2(0, 10));
Body groundBody = mBoxWorld.createBody(groundBodyDef);
PolygonShape groundBox = new PolygonShape();
groundBox.setAsBox(mCam.viewportWidth, 10.0f);
groundBody.createFixture(groundBox, 0.0f);
groundBox.dispose();
return groundBody;
}
private void render_physics() {mDebugRenderer.render(mBoxWorld, mCam.combined);}
private float to_world_x(float size) {return size /PIXEL_TO_METER_RATIO_DEFAULT;}
private float to_world_y(float size) {return size /PIXEL_TO_METER_RATIO_DEFAULT;}
private class sprite_actor extends Actor {
private Body mBody = null;
private Sprite mSprite;
public sprite_actor(World world, Sprite sprite) {
this.mSprite = sprite;
create_body();
}
@Override
public void act(float delta) {
super.act(delta);
}
@Override
public void draw(Batch batch, float parentAlpha) {
// TODO Auto-generated method stub
setRotation(MathUtils.radiansToDegrees * mBody.getAngle());
setPosition(to_world_x(mBody.getPosition().x),to_world_y(mBody.getPosition().y));
batch.draw(mSprite, getX(), getY());
System.out.println("("+to_world_x(mBody.getPosition().x)+","+to_world_y(mBody.getPosition().y)+")");
}
private void create_body() {
mBody = create_dynamic_object_ball();
}
}
private class GameStage extends Stage {
private World world;
private OrthographicCamera camera;
public void setupStage(OrthographicCamera cam, World box_world,Box2DDebugRenderer renderer) {
this.world = box_world;
this.camera = cam;
}
@Override
public void act(float step) {
world.step(one_over_60, 6, 2);
super.act(step);
}
@Override
public void draw() {
mDebugRenderer.render(world, camera.combined);
super.draw();
}
}
}
使用两个不同的摄像机。物理调试摄影机应使用视口大小/每米像素。Sprite相机具有标准的视口大小。谢谢@noone,通过设置
batch.setProjectionMatrix(mCam.combined),它才能够正常工作代码>在我的Actor
类的Draw
方法中。哪一个更值得推荐,两个摄像头还是上面的那个?