Search in sources :

Example 1 with MouseJointDef

use of spacegraph.space2d.phys.dynamics.joints.MouseJointDef in project narchy by automenta.

the class Box2DTests method initMouse.

private void initMouse() {
    addMouseWheelListener((MouseWheelEvent e) -> {
        if (e.getWheelRotation() < 0) {
            zoom *= 1.25f * -e.getWheelRotation();
        } else {
            zoom /= 1.25f * e.getWheelRotation();
        }
        zoom = Math.min(zoom, 100);
        zoom = Math.max(zoom, 0.1f);
        repaint();
    });
    addMouseMotionListener(new MouseMotionListener() {

        @Override
        public void mouseDragged(MouseEvent e) {
            Point p = e.getPoint();
            mousePosition = getPoint(p);
            if (clickedPoint != null) {
                p.x -= clickedPoint.x;
                p.y -= clickedPoint.y;
                center.x = startCenter.x - p.x / zoom;
                center.y = startCenter.y + p.y / zoom;
            } else {
                if (mj != null) {
                    mj.setTarget(mousePosition);
                }
            }
            if (!running) {
                repaint();
            }
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            Point p = e.getPoint();
            mousePosition = getPoint(p);
            if (!running) {
                repaint();
            }
        }
    });
    addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            Point p = new Point(x, y);
            switch(e.getButton()) {
                case 3:
                    startCenter.set(center);
                    clickedPoint = p;
                    break;
                case 1:
                    Tuple2f v = getPoint(p);
                    /*synchronized(Tests.this)*/
                    {
                        w.bodies(b -> {
                            for (Fixture f = b.fixtures(); f != null; f = f.next) {
                                if (f.testPoint(v)) {
                                    MouseJointDef def = new MouseJointDef();
                                    def.bodyA = ground;
                                    def.bodyB = b;
                                    def.collideConnected = true;
                                    def.target.set(v);
                                    def.maxForce = 500f * b.getMass();
                                    def.dampingRatio = 0;
                                    mjdef = def;
                                    return;
                                }
                            }
                        });
                    }
                    break;
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // synchronized (Tests.this) {
            switch(e.getButton()) {
                case 3:
                    clickedPoint = null;
                    break;
                case 1:
                    if (mj != null) {
                        destroyMj = true;
                    }
                    break;
            }
        // }
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    });
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) PolygonFixture(spacegraph.space2d.phys.fracture.PolygonFixture) MouseJointDef(spacegraph.space2d.phys.dynamics.joints.MouseJointDef)

Example 2 with MouseJointDef

use of spacegraph.space2d.phys.dynamics.joints.MouseJointDef in project narchy by automenta.

the class TestQueryCallback method spawnMouseJoint.

private void spawnMouseJoint(Vec2 p) {
    if (mouseJoint != null) {
        return;
    }
    queryAABB.lowerBound.set(p.x - .001f, p.y - .001f);
    queryAABB.upperBound.set(p.x + .001f, p.y + .001f);
    callback.point.set(p);
    callback.fixture = null;
    world.queryAABB(callback, queryAABB);
    if (callback.fixture != null) {
        Body body = callback.fixture.getBody();
        MouseJointDef def = new MouseJointDef();
        def.bodyA = groundBody;
        def.bodyB = body;
        def.collideConnected = true;
        def.target.set(p);
        def.maxForce = 1000f * body.getMass();
        mouseJoint = (MouseJoint) world.createJoint(def);
        body.setAwake(true);
    }
}
Also used : MouseJointDef(spacegraph.space2d.phys.dynamics.joints.MouseJointDef)

Aggregations

MouseJointDef (spacegraph.space2d.phys.dynamics.joints.MouseJointDef)2 PolygonFixture (spacegraph.space2d.phys.fracture.PolygonFixture)1 Tuple2f (spacegraph.util.math.Tuple2f)1