Skip to content

Instantly share code, notes, and snippets.

@Tom-Ski
Created April 15, 2014 06:25
Show Gist options
  • Save Tom-Ski/10707021 to your computer and use it in GitHub Desktop.
Save Tom-Ski/10707021 to your computer and use it in GitHub Desktop.
Weld Joint Test
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;
/**
* 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;
@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();
bodyOne.setTransform(10, 10, 0);
bodyTwo.setTransform(10, 13, 0);
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;
}
@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);
}
@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