gpt4 book ai didi

java - Libgdx - PPM 世界转换

转载 作者:行者123 更新时间:2023-12-02 12:02:12 26 4
gpt4 key购买 nike

我的 Sprite 移动得太慢了。基本上我想用更少的力来移动我的玩家。目前这个:

getBody().applyForceToCenter(new Vector2(-10000000f,0f), true);

是使其移动一点点所需的力。

我知道我无法移动它的原因是因为我没有缩放 Sprite (64x64),它的重量超过 400 公斤。正确的比例应该是多少?

这是我的游戏屏幕。

public class GameScreen implements Screen {
//Reference to our Game, used to set Screens
private Logang game;

//basic playscreen variables
private OrthographicCamera gamecam;
private Viewport gamePort;
//Box2d variables
private World world;
private Box2DDebugRenderer b2dr;

boolean drawn = true;
private Player p;
private int pX = 100, pY = 300;

public GameScreen(Logang game) {

this.game = game;
//create cam used to follow mario through cam world
gamecam = new OrthographicCamera();
gamePort = new ScalingViewport(Scaling.stretch, Logang.GWIDTH, Logang.GHEIGHT, gamecam);
gamePort.apply();
gamecam.position.set(gamecam.viewportWidth / 2, gamecam.viewportHeight / 2, 0);
gamecam.update();
Box2D.init();
//create our Box2D world, setting no gravity in X, -10 gravity in Y, and allow bodies to sleep
world = new World(new Vector2(0, Logang.GRAVITY), true);
//allows for debug lines of our box2d world.
b2dr = new Box2DDebugRenderer();

//create a FitViewport to maintain virtual aspect ratio despite screen size

p = new Player(new Sprite(new Texture("hud_p3.png")), world, pX, pY, 1);

//initially set our gamcam to be centered correctly at the start of of map

line();
}


@Override
public void show() {

}

public void update(float dt) {
//handle user input first
p.update(dt);
//update our gamecam with correct coordinates after changes
}


@Override
public void render(float delta) {
//separate our update logic from render
update(delta);

//Clear the game screen with Black
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

world.step(1f / 60f, 6, 2);

gamecam.position.set(p.getSprite().getX(),Logang.GHEIGHT / 2, 0); // x and y could be changed by Keyboard input for example

gamecam.update();

game.getBatch().setProjectionMatrix(gamecam.combined);

//renderer our Box2DDebugLines
b2dr.render(world, gamecam.combined);

System.out.println("Player x: " + p.getSprite().getX() + " Camera X: " + gamecam.position.x + " Body X: " + p.getBody().getPosition().x);
//System.out.println("Player y: " + p.getSprite().getY() + " Camera Y: " + gamecam.position.y + " Body Y: " + p.getBody().getPosition().y);


game.getBatch().begin();


if (p.getBody() != null)
p.render(game.getBatch());

EntityManager.renderTerra(game.getBatch(), delta);


game.getBatch().end();

}

public void line() {
Texture tmp = new Texture("hud_p3.png");
tmp.setWrap(Texture.TextureWrap.MirroredRepeat, Texture.TextureWrap.MirroredRepeat);
for (int i = 0; i < 50; i++) {
EntityManager.add(new Ground(new Sprite(tmp), world, (int)(i * Logang.TILE), 1, 2));
}
// EntityManager.changeSize(((Logang.TILE) * 5),Logang.TILE);
}

@Override
public void resize(int width, int height) {
//updated our game viewport
gamePort.update(width, height);
gamecam.position.set(gamecam.viewportWidth / 2, gamecam.viewportHeight / 2, 0);
}

public World getWorld() {
return world;
}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {
world.dispose();
b2dr.dispose();
}

这是我的实体类

private World world;
private Sprite sprite;
private Body body;
private int tipo;

public Entity(Sprite sprite, World world, int x, int y, int tipo) {
this.sprite = sprite;
this.world = world;
getSprite().setPosition(x, y);
sprite.setSize(Logang.TILE, Logang.TILE);
sprite.setOriginCenter();
define(tipo);
this.tipo = tipo;
}

public void update(float dt){
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
getBody().applyForceToCenter(new Vector2(-10000000f,0f), true);
}
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
getBody().applyForceToCenter(new Vector2(10000000f,0f), true);
}
if(Gdx.input.isKeyPressed(Input.Keys.SPACE)){
//getBody().applyLinearImpulse(0f,-Logang.GRAVITY,
getBody().getPosition().x, getBody().getPosition().y, true);
}
}

public void define(int tipo) {
BodyDef bdef = new BodyDef();
bdef.position.set((getSprite().getX() + getSprite().getWidth() / 2),
(getSprite().getY() + getSprite().getHeight() / 2));
switch (tipo) {
case 1: {
bdef.type = BodyDef.BodyType.DynamicBody;
break;
}
case 2: {
bdef.type = BodyDef.BodyType.StaticBody;
break;
}
case 3: {
bdef.type = BodyDef.BodyType.DynamicBody;
break;
}

}

body = world.createBody(bdef);

FixtureDef fdef = new FixtureDef();
fdef.density=0.001f; // (weight: range 0.01 to 1 is good)
PolygonShape shape = new PolygonShape();
shape.setAsBox(getSprite().getWidth() / 2, getSprite().getHeight() / 2);

fdef.shape = shape;
body.createFixture(fdef);
body.setUserData(this);

shape.dispose();
}

public void render(SpriteBatch batch) {
if (tipo != 2) {
float posX = getBody().getPosition().x;
float posY = getBody().getPosition().y;

getSprite().setPosition(posX - getSprite().getWidth() / 2, posY -
getSprite().getHeight() / 2);

}
getSprite().draw(batch);
}

public Sprite getSprite() {
return sprite;
}

public void setSprite(Sprite sprite) {
this.sprite = sprite;
}

public Body getBody() {
return body;
}

public void setBody(Body body) {
this.body = body;
}

这是游戏中的变量

public static final int GWIDTH = 800;
public static final int GHEIGHT = (GWIDTH/16)*9;
public static final float PPM = 100f;
public static final float GRAVITY = -10f;
public static final float TILE = 64;

你能帮我解决一下吗?我已经尝试划分 body 和游戏机位置仍然没有效果

最佳答案

正确的比例应该是多少?

正确的比例应该是现实生活中的比例,其中 LibGDX (Box2D) 中的 1 个单位代表现实生活中的 1 米。我总是建议人们使用这个比例并正确缩放相机。

但请注意,如果您使用非常大的物体并将相机一直向后变焦,则物体看起来会缓慢下落。这显然是因为您的相机包含更大的空间。如果该元素变小,它不仅会缓慢下落,而且无法与世界正确交互。

让相机适应您的世界,而不是让您的世界适应您的相机。

More detailed answer I gave

关于java - Libgdx - PPM 世界转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47164432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com