Skip to content

Instantly share code, notes, and snippets.

@Tom-Ski
Created April 23, 2014 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tom-Ski/11199931 to your computer and use it in GitHub Desktop.
Save Tom-Ski/11199931 to your computer and use it in GitHub Desktop.
package me.tomski.blobrun.desktop;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.joints.MouseJoint;
import com.badlogic.gdx.physics.box2d.joints.MouseJointDef;
import com.badlogic.gdx.physics.box2d.joints.WeldJointDef;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;
/**
* Created by Tom on 15/04/14.
*/
public class WeldTest {
public static void main(String[] args) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.width = 1280;
config.height = 720;
config.foregroundFPS = 60;
config.backgroundFPS = 60;
config.fullscreen = false;
config.vSyncEnabled = true;
config.resizable = false;
new LwjglApplication(new WeldTestDemo());
}
static class WeldTestDemo implements ApplicationListener, InputProcessor {
World world;
OrthographicCamera camera;
Box2DDebugRenderer renderer;
MouseJoint mouseJoint = null;
Body hitBody = null;
Body groundBody;
long lastCreate = 0;
Contact createContact = null;
@Override
public void create() {
world = new World(new Vector2(0, -10), false);
camera = new OrthographicCamera(40, 22.5f);
camera.position.set(20, 11.25f, 0);
camera.update();
renderer = new Box2DDebugRenderer();
Body bodyOne = createBody();
Body bodyTwo = createBody();
Body kin = createKinematicBody();
bodyOne.setTransform(10, 10, 0);
bodyTwo.setTransform(10, 13, 0);
kin.setTransform(20,10,0);
kin.setAngularVelocity(0.5f);
WeldJointDef jointDef = new WeldJointDef();
jointDef.initialize(bodyOne, bodyTwo, bodyTwo.getWorldCenter());
jointDef.collideConnected = false;
world.createJoint(jointDef);
PolygonShape groundPoly = new PolygonShape();
groundPoly.setAsBox(50, 1);
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;
groundBodyDef.position.set(0, 0);
groundBody = world.createBody(groundBodyDef);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = groundPoly;
fixtureDef.filter.groupIndex = 0;
groundBody.createFixture(fixtureDef);
groundPoly.dispose();
Gdx.input.setInputProcessor(this);
}
private Body createBody() {
BodyDef def = new BodyDef();
def.type = BodyType.DynamicBody;
PolygonShape shape = new PolygonShape();
shape.setAsBox(1, 1);
Body b = world.createBody(def);
FixtureDef fDef = new FixtureDef();
fDef.shape = shape;
fDef.density = 0.2f;
fDef.friction = 0.2f;
fDef.restitution = 0.5f;
b.createFixture(fDef);
shape.dispose();
return b;
}
private Body createKinematicBody() {
BodyDef def = new BodyDef();
def.type = BodyType.KinematicBody;
PolygonShape shape = new PolygonShape();
shape.setAsBox(3, 3);
Body b = world.createBody(def);
FixtureDef fDef = new FixtureDef();
fDef.shape = shape;
fDef.density = 15f;
fDef.friction = 0.2f;
fDef.restitution = 0.5f;
Fixture f = b.createFixture(fDef);
f.setUserData("k");
shape.dispose();
return b;
}
private void createFallingBody() {
BodyDef def = new BodyDef();
def.type = BodyType.DynamicBody;
CircleShape shape = new CircleShape();
shape.setRadius(1);
Body b = world.createBody(def);
FixtureDef fDef = new FixtureDef();
fDef.shape = shape;
fDef.density = 0.9f;
fDef.friction = 0.2f;
fDef.restitution = 0.5f;
Fixture f = b.createFixture(fDef);
f.setUserData("d");
shape.setRadius( shape.getRadius() * 2.0f);
fDef.shape = shape;
fDef.density = 0.1f;
//fDef.isSensor = false;
Fixture outer = b.createFixture(fDef);
outer.setUserData("o");
shape.dispose();
b.setTransform(20, 50, 0);
}
public void createWeldJointFromContact() {
System.out.println("A: " + createContact.getFixtureA().getUserData() + " B: " + createContact.getFixtureB().getUserData());
Array<Fixture> bFixtures = createContact.getFixtureB().getBody().getFixtureList();
for (int iB = 0; iB < bFixtures.size; iB++) {
System.out.println("\t" + iB + " name: " + bFixtures.get(iB).getUserData());
}
WeldJointDef jointDef = new WeldJointDef();
jointDef.initialize(createContact.getFixtureA().getBody(), createContact.getFixtureB().getBody(), createContact.getFixtureB().getBody().getWorldCenter());
jointDef.collideConnected = true;
world.createJoint(jointDef);
//nudgeFixtures.add(create)
createContact = null;
}
@Override
public void resize(int width, int height) {
}
@Override
public void render() {
Gdx.gl.glClearColor(0.05f, 0.05f, 0.05f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
world.step(1 / 30f, 6, 2);
renderer.render(world, camera.combined);
if (TimeUtils.millis() - lastCreate > 1000) {
createFallingBody();
lastCreate = TimeUtils.millis();
}
if (createContact != null) {
createWeldJointFromContact();
}
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
}
@Override
public boolean keyDown(int keycode) {
return false;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
Vector3 testPoint = new Vector3();
QueryCallback callback = new QueryCallback() {
@Override
public boolean reportFixture(Fixture fixture) {
if (fixture.getBody() == groundBody) return true;
if (fixture.testPoint(testPoint.x, testPoint.y)) {
hitBody = fixture.getBody();
return false;
} else
return true;
}
};
@Override
public boolean touchDown(int x, int y, int pointer, int newParam) {
testPoint.set(x, y, 0);
camera.unproject(testPoint);
hitBody = null;
world.QueryAABB(callback, testPoint.x - 0.1f, testPoint.y - 0.1f, testPoint.x + 0.1f, testPoint.y + 0.1f);
if (hitBody != null) {
MouseJointDef def = new MouseJointDef();
def.bodyA = groundBody;
def.bodyB = hitBody;
def.collideConnected = true;
def.target.set(testPoint.x, testPoint.y);
def.maxForce = 1000.0f * hitBody.getMass();
mouseJoint = (MouseJoint) world.createJoint(def);
hitBody.setAwake(true);
}
return false;
}
Vector2 target = new Vector2();
@Override
public boolean touchDragged(int x, int y, int pointer) {
if (mouseJoint != null) {
camera.unproject(testPoint.set(x, y, 0));
mouseJoint.setTarget(target.set(testPoint.x, testPoint.y));
}
return false;
}
@Override
public boolean touchUp(int x, int y, int pointer, int button) {
if (mouseJoint != null) {
world.destroyJoint(mouseJoint);
mouseJoint = null;
}
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment