首先,让我说我刚刚开始探索Box2D和LibGDX的世界。 所以,如果我正在做什么/问什么是非常新秀...好吧 - 现在你知道为什么了!
我正在尝试创建一个Box2d World,它具有落在地面上的坠落体(动态)(静态)
我注意到的奇怪的“问题”是当身体坠落时,它会失去线速度。
玩家的起点越高 - 他到达地面时的线速度越低。
这是我正在使用的代码。
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
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.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
public class TestGame implements ApplicationListener {
private OrthographicCamera camera;
private int GAME_WIDTH = 1024 ;
private int GAME_HEIGHT = 768 ;
Box2DDebugRenderer renderer;
World world ;
BodyDef bdefPlayer;
FixtureDef fdefPlayer;
PolygonShape shapePlayer;
Body fallingObject;
int fallingObjectHeight = 64;
int fallingObjectWidth = 64;
BodyDef bdefGround;
FixtureDef fdefGround;
PolygonShape shapeGround;
int numberOfGroundUnitsToCreate = 100;
int groundUnitHeight = 64;
int groundUnitWidth = 64;
Body groundUnitsList[] = new Body[numberOfGroundUnitsToCreate];
boolean isDebugMode = true;
@Override
public void create() {
Texture.setEnforcePotImages(false);
// Create World
world = new World(new Vector2(0.0f, -9.8f), false);
// Create debug render
renderer = new Box2DDebugRenderer();
// Initiate camera
camera = new OrthographicCamera(GAME_WIDTH, GAME_HEIGHT);
camera.setToOrtho(false);
camera.position.set(GAME_WIDTH/2, GAME_HEIGHT/2, 0);
// Create Player
createPlayer();
// Create Ground
createGround();
}
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// Setup camera to follow Player
camera.position.x = fallingObject.getPosition().x + 50;
camera.update();
// Update and render the world
world.step(1/12f, 6, 2);
if(isDebugMode) {
renderer.render(world, camera.combined);
}
System.out.println(
"Linear Velocity: " + fallingObject.getLinearVelocity() +
" Linear Damping: " + fallingObject.getLinearDamping() +
" Angular Velocity: " + fallingObject.getAngularVelocity() +
" Angular Damping: " + fallingObject.getAngularDamping() +
" World Gravity: " + world.getGravity() +
" Density: " + fallingObject.getMass()
);
handleInput();
}
private void createPlayer() {
bdefPlayer = new BodyDef();
fdefPlayer = new FixtureDef();
shapePlayer = new PolygonShape();
// create player
bdefPlayer.type = BodyType.DynamicBody;
bdefPlayer.linearVelocity.x = 20.0f;
bdefPlayer.linearDamping = 0.00f;
bdefPlayer.angularDamping = 0.0f;
bdefPlayer.angularVelocity = 0.0f;
bdefPlayer.position.set(50, 250);
fallingObject = world.createBody(bdefPlayer);
shapePlayer.setAsBox(fallingObjectHeight/2 , fallingObjectWidth/2);
fdefPlayer.shape = shapePlayer;
fallingObject.createFixture(fdefPlayer);
// create foot sensor
shapePlayer.setAsBox(fallingObjectWidth/2 , 10);
fdefPlayer.shape = shapePlayer;
fdefPlayer.isSensor = true;
fallingObject.createFixture(fdefPlayer);
}
private void createGround() {
bdefGround = new BodyDef();
fdefGround = new FixtureDef();
shapeGround = new PolygonShape();
for (int i=0; i<numberOfGroundUnitsToCreate; i++) {
bdefGround.type = BodyType.StaticBody;
bdefGround.position.set((float) ((groundUnitWidth + 0.5) *i ) , 0);
shapeGround.setAsBox(groundUnitWidth/2 , groundUnitHeight/2);
fdefGround.shape = shapeGround;
fdefGround.friction = 0.0f;
Body grassTileBodyTemp = world.createBody(bdefGround);
grassTileBodyTemp.createFixture(fdefGround);
groundUnitsList[i]= grassTileBodyTemp;
}
}
public void handleInput() {
if(Gdx.input.isKeyPressed(Keys.SPACE))
fallingObject.applyForceToCenter(0, 20.0f, true);
if(Gdx.input.isKeyPressed(Keys.RIGHT))
fallingObject.applyForceToCenter(20.0f, 0, true);
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
}
对于我可能导致此问题的错误而言,我最终会陷入困境。