-
-
Save anonymous/365fed8439f4ca65f91a to your computer and use it in GitHub Desktop.
Full box2d platformer movement test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.testbed; | |
import com.badlogic.gdx.ApplicationAdapter; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.Input; | |
import com.badlogic.gdx.graphics.GL20; | |
import com.badlogic.gdx.graphics.OrthographicCamera; | |
import com.badlogic.gdx.math.MathUtils; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.physics.box2d.*; | |
public class PhysicsTest2 extends ApplicationAdapter { | |
private static final float WIDTH = 960f; | |
private static final float HEIGHT = 640f; | |
private static float ZOOM = 1f; | |
private static final float SCALE = 100f; | |
private static final float INVERSE_SCALE = 1f / SCALE; | |
private static final int VELOCITY_ITERATIONS = 6; | |
private static final int POSITION_ITERATIONS = 4; | |
private float STEP = 1f / 60f; | |
private float accumulator = 0f; | |
private Body testBody; | |
World world; | |
OrthographicCamera cam; | |
Box2DDebugRenderer renderer; | |
@Override | |
public void create() { | |
world = new World(new Vector2(0, -9.81f), true); | |
renderer = new Box2DDebugRenderer(); | |
cam = new OrthographicCamera(WIDTH * ZOOM * INVERSE_SCALE, HEIGHT * ZOOM * INVERSE_SCALE); | |
cam.update(); | |
createWorld(); | |
testBody = createTestBody(); | |
} | |
@Override | |
public void render() { | |
handleCameraInput(); | |
handleTestBodyInputImpulse(); | |
// handleTestBodyInputVelocity(); | |
accumulator += Math.min(Gdx.graphics.getDeltaTime(), STEP); | |
int logicStepCount = 0; | |
while (accumulator >= STEP) { | |
world.step(STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS); | |
accumulator -= STEP; | |
++logicStepCount; | |
} | |
Gdx.graphics.setTitle("FPS: " + Gdx.graphics.getFramesPerSecond() + ", Logic steps: " + logicStepCount + ", Test body vel: " + testBody.getLinearVelocity()); | |
cam.update(); | |
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | |
renderer.render(world, cam.combined); | |
} | |
@Override | |
public void dispose() { | |
super.dispose(); | |
} | |
private void createWorld() { | |
BodyDef bd = new BodyDef(); | |
bd.type = BodyDef.BodyType.StaticBody; | |
FixtureDef fd = new FixtureDef(); | |
PolygonShape shape = new PolygonShape(); | |
shape.setAsBox(500f * INVERSE_SCALE, 20f * INVERSE_SCALE); | |
fd.shape = shape; | |
fd.friction = 0; | |
Body body = world.createBody(bd); | |
body.createFixture(fd); | |
} | |
private Body createTestBody() { | |
BodyDef bd = new BodyDef(); | |
bd.type = BodyDef.BodyType.DynamicBody; | |
bd.position.x = 20f * INVERSE_SCALE; | |
bd.position.y = 50f * INVERSE_SCALE; | |
FixtureDef fd = new FixtureDef(); | |
PolygonShape shape = new PolygonShape(); | |
shape.setAsBox(8f * INVERSE_SCALE, 16f * INVERSE_SCALE); | |
fd.shape = shape; | |
fd.friction = 0; | |
Body body = world.createBody(bd); | |
body.createFixture(fd); | |
return body; | |
} | |
private void handleCameraInput() { | |
if(Gdx.input.isKeyPressed(Input.Keys.PLUS)) { | |
ZOOM = MathUtils.clamp(ZOOM - 0.1f, 0f, 1f); | |
} | |
if(Gdx.input.isKeyPressed(Input.Keys.MINUS)) { | |
ZOOM = MathUtils.clamp(ZOOM + 0.1f, 0f, 1f); | |
} | |
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) { | |
cam.translate(-1f, 0f); | |
} | |
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { | |
cam.translate(1f, 0f); | |
} | |
if(Gdx.input.isKeyPressed(Input.Keys.UP)) { | |
cam.translate(0f, 1f); | |
} | |
if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) { | |
cam.translate(0f, -1f); | |
} | |
} | |
private void handleTestBodyInputImpulse() { | |
float desiredVel = 0f; | |
boolean inputProcessed = false; | |
if(Gdx.input.isKeyPressed(Input.Keys.A)) { | |
desiredVel = -1.5f; | |
inputProcessed = true; | |
} | |
if(Gdx.input.isKeyPressed(Input.Keys.D)) { | |
desiredVel = 1.5f; | |
inputProcessed = true; | |
} | |
if(Gdx.input.isKeyJustPressed(Input.Keys.SPACE) && testBody.getLinearVelocity().y <= 0f) { | |
testBody.applyForceToCenter(0f, 250f, true); | |
inputProcessed = true; | |
} | |
if(!inputProcessed) { | |
desiredVel = 0f; | |
} | |
float velChange = desiredVel - testBody.getLinearVelocity().x; | |
float impulse = testBody.getMass() * velChange; | |
testBody.applyLinearImpulse(impulse, 0, testBody.getWorldCenter().x, testBody.getWorldCenter().y, true); | |
} | |
private void handleTestBodyInputVelocity() { | |
Vector2 linearVelocity = testBody.getLinearVelocity(); | |
float desiredVelocity = 0f; | |
boolean inputProcessed = false; | |
if(Gdx.input.isKeyPressed(Input.Keys.A)) { | |
desiredVelocity = -1.5f; | |
inputProcessed = true; | |
} | |
if(Gdx.input.isKeyPressed(Input.Keys.D)) { | |
desiredVelocity = 1.5f; | |
inputProcessed = true; | |
} | |
if(Gdx.input.isKeyJustPressed(Input.Keys.SPACE) && testBody.getLinearVelocity().y <= 0f) { | |
testBody.applyForceToCenter(0f, 250f, true); | |
inputProcessed = true; | |
} | |
if(!inputProcessed) { | |
desiredVelocity = 0f; | |
} | |
float velChange = desiredVelocity - linearVelocity.x; | |
float force = testBody.getMass() * velChange / STEP; | |
testBody.applyForceToCenter(force, 0, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment