Skip to content

Instantly share code, notes, and snippets.

@Happsson
Created July 4, 2014 16:32
Show Gist options
  • Save Happsson/ddf868e9899cf3dc3ce6 to your computer and use it in GitHub Desktop.
Save Happsson/ddf868e9899cf3dc3ce6 to your computer and use it in GitHub Desktop.
Rep
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
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;
import com.badlogic.gdx.physics.box2d.joints.DistanceJointDef;
public class Test4000 extends ApplicationAdapter {
private OrthographicCamera camera;
private World world;
private Box2DDebugRenderer debugRenderer;
private Body[] b;
@Override
public void render() {
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
world.step(0.02f, 8, 3);
debugRenderer.render(world, camera.combined);
}
@Override
public void create() {
camera = new OrthographicCamera(Gdx.graphics.getWidth()/70, Gdx.graphics.getHeight()/70);
debugRenderer = new Box2DDebugRenderer();
world = new World(new Vector2(0,-9.81f), true);
BodyDef bdef = new BodyDef(); //body definition
PolygonShape shape = new PolygonShape();
FixtureDef fdef = new FixtureDef();
DistanceJointDef djf1 = new DistanceJointDef();
fdef.shape = shape;
fdef.density = 1;
b = new Body[50];
for(int i = 0; i<b.length; i++){
float y = i*0.1f;
bdef.position.set(0, y);
if(i == 0){
bdef.type = BodyType.StaticBody;
}else{
bdef.type = BodyType.DynamicBody;
}
shape.setAsBox(0.0005f, 0.0005f);
b[i] = world.createBody(bdef);
b[i].createFixture(fdef);
if(i != 0){
djf1.bodyA = b[i];
djf1.bodyB = b[i-1];
djf1.length = .1f;
world.createJoint(djf1);
}
}
Gdx.input.setInputProcessor(new InputMultiplexer(new InputAdapter() {
@Override
public boolean keyDown(int keycode) {
b[b.length-1].applyForceToCenter(1000,0, true);
return true;
}
}));
}
public void resize(int width, int height) {}
public void hide() {}
public void pause() {}
public void resume() {}
public void dispose() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment