Skip to content

Commit

Permalink
Fixed #139 wrong coordinates.
Browse files Browse the repository at this point in the history
Tested with the DemoPhysicsMouse demo.
  • Loading branch information
metc committed May 31, 2016
1 parent fb0f589 commit 94058a5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
public class DemoControllers extends PortableApplication {

private final static String TAG = DemoControllers.class.getSimpleName();
private static final String TAG = DemoControllers.class.getSimpleName();

public static void main(String[] args) {
new DemoControllers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ch.hevs.gdx2d.desktop.physics.DebugRenderer;
import ch.hevs.gdx2d.lib.GdxGraphics;
import ch.hevs.gdx2d.lib.physics.PhysicsWorld;
import ch.hevs.gdx2d.lib.utils.Logger;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
Expand All @@ -19,30 +20,26 @@
import java.util.Random;

/**
* A demo on how to use the mouse to move objects with box2d
* A demo on how to use the mouse to move objects with {@code Box2D}.
*
* @author Pierre-André Mudry, mui 2013
* @version 1.1
*/
public class DemoPhysicsMouse extends PortableApplication {
protected Body hitBody = null;
/**
* ground body to connect the mouse joint to *
*/
protected Body groundBody;
/**
* our mouse joint *
*/
protected MouseJoint mouseJoint = null;
World world = PhysicsWorld.getInstance();
// Contains all the objects that will be simulated
DebugRenderer debugRenderer;
protected Body groundBody; // Ground body to connect the mouse joint to
protected MouseJoint mouseJoint = null; // Our mouse joint

World world = PhysicsWorld.getInstance();

DebugRenderer debugRenderer; // Contains all the objects that will be simulated
/**
* we instantiate this vector and the callback here so we don't irritate the
* GC
*/
Vector2 testPoint = new Vector2();
Vector2 target = new Vector2();

// Called for AABB lookup
QueryCallback callback = new QueryCallback() {
@Override
Expand All @@ -56,10 +53,6 @@ public boolean reportFixture(Fixture fixture) {
}
};

public DemoPhysicsMouse() {
super();
}

public DemoPhysicsMouse(int w, int h) {
super(w, h);
}
Expand All @@ -72,8 +65,7 @@ public static void main(String[] args) {
public void onInit() {
setTitle("Mouse interactions in box2d, mui 2013");

// we also need an invisible zero size ground body
// to which we can connect the mouse joint
// We also need an invisible zero size ground body to which we can connect the mouse joint
BodyDef bodyDef = new BodyDef();
groundBody = world.createBody(bodyDef);

Expand Down Expand Up @@ -103,49 +95,40 @@ public void onGraphicRender(GdxGraphics g) {

g.drawFPS();
g.drawSchoolLogoUpperRight();

}

@Override
public void onDrag(int x, int y) {
// if a mouse joint exists we simply update
// the target of the joint based on the new
// mouse coordinates
if (mouseJoint != null) {
// If a mouse joint exists we simply update the target of the joint based on the new mouse coordinates
mouseJoint.setTarget(target.set(x, y).scl(PhysicsConstants.PIXEL_TO_METERS));
}
return;
}

@Override
public void onRelease(int x, int y, int button) {
// if a mouse joint exists we simply destroy it
if (mouseJoint != null) {
// If a mouse joint exists we simply destroy it
world.destroyJoint(mouseJoint);
mouseJoint = null;
}
}

@Override
public void onClick(int x, int y, int button) {
// translate the mouse coordinates to world coordinates
// cam.unproject(testPoint.set(x, y, 0));
testPoint.x = x;
testPoint.y = y;

testPoint.scl(PhysicsConstants.PIXEL_TO_METERS);
// Translate the mouse coordinates to world coordinates
testPoint.set(x, y).scl(PhysicsConstants.PIXEL_TO_METERS);

// ask the world which bodies are within the given
// bounding box around the mouse pointer
// Ask the world which bodies are within the given bounding box around the mouse pointer
hitBody = null;

world.QueryAABB(callback, testPoint.x - 5, testPoint.y - 5, testPoint.x + 5, testPoint.y + 5);

// ignore kinematic bodies, they don't work with the mouse joint
// Ignore kinematic bodies, they don't work with the mouse joint
if (hitBody == null || hitBody.getType() == BodyType.KinematicBody)
return;

// if we hit something we create a new mouse joint
// and attach it to the hit body.
// If we hit something we create a new mouse joint and attach it to the hit body
if (hitBody != null) {
MouseJointDef def = new MouseJointDef();
def.bodyA = groundBody;
Expand All @@ -158,7 +141,6 @@ public void onClick(int x, int y, int button) {
mouseJoint = (MouseJoint) world.createJoint(def);
hitBody.setAwake(true);
}
return;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/**
* Input mouse and keyboard processor for gdx2d.
* <p/>
* Automatically quits the application when the {@code ESCAPE} key is pressed.
*
* @see com.badlogic.gdx.InputProcessor
*
Expand Down Expand Up @@ -34,7 +36,7 @@ class GdxInputProcessor extends InputAdapter {

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
app.onRelease(screenX, screenY, button);
app.onRelease(screenX, Gdx.graphics.getHeight() - screenY, button);
return false;
}

Expand Down

0 comments on commit 94058a5

Please sign in to comment.