Search in sources :

Example 71 with Tuple2f

use of spacegraph.util.math.Tuple2f in project narchy by automenta.

the class SingletonVD method sort.

/**
 * Zotriedi vrcholy v polygone podla uhlu
 *
 * @param indexPolygon
 */
private void sort(int indexPolygon) {
    int size = vCount[indexPolygon];
    if (size != 0) {
        polygon = voronoi[indexPolygon];
        Tuple2f focus = ar[indexPolygon];
        for (int i = 0; i != size; ++i) {
            comparer[i] = angle(points[polygon[i]], focus);
        }
        quicksortPoly(0, size - 1);
    }
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f)

Example 72 with Tuple2f

use of spacegraph.util.math.Tuple2f in project narchy by automenta.

the class Box2DTests method drawJoint.

private void drawJoint(Joint joint) {
    g.setColor(Color.GREEN);
    Tuple2f v1 = new Vec2();
    Tuple2f v2 = new Vec2();
    switch(joint.getType()) {
        case DISTANCE:
            DistanceJoint dj = (DistanceJoint) joint;
            v1 = joint.getBodyA().getWorldPoint(dj.getLocalAnchorA());
            v2 = joint.getBodyB().getWorldPoint(dj.getLocalAnchorB());
            break;
        case MOUSE:
            MouseJoint localMj = (MouseJoint) joint;
            localMj.getAnchorA(v1);
            localMj.getAnchorB(v2);
            break;
    }
    Point p1 = getPoint(v1);
    Point p2 = getPoint(v2);
    g.drawLine(p1.x, p1.y, p2.x, p2.y);
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) DistanceJoint(spacegraph.space2d.phys.dynamics.joints.DistanceJoint) Vec2(spacegraph.space2d.phys.common.Vec2) MouseJoint(spacegraph.space2d.phys.dynamics.joints.MouseJoint)

Example 73 with Tuple2f

use of spacegraph.util.math.Tuple2f 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 74 with Tuple2f

use of spacegraph.util.math.Tuple2f in project narchy by automenta.

the class Box2DTests method drawParticles.

private void drawParticles() {
    Tuple2f[] vec = w.getParticlePositionBuffer();
    if (vec == null) {
        return;
    }
    g.setColor(Color.MAGENTA);
    float radius = w.getParticleRadius();
    int size = w.getParticleCount();
    for (int i = 0; i < size; i++) {
        Tuple2f vx = vec[i];
        Point pp = getPoint(vx);
        float r = radius * zoom;
        if (r < 0.5f) {
            // ak je zoom priliz maly, tak by kvapalinu nezobrazilo
            g.drawLine(pp.x, pp.y, pp.x, pp.y);
        } else {
            // int radInt = Math.round(r * 2);
            g.fillOval(pp.x - (int) r, pp.y - (int) r, (int) (r * 2), (int) (r * 2));
        }
    }
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) DistanceJoint(spacegraph.space2d.phys.dynamics.joints.DistanceJoint) MouseJoint(spacegraph.space2d.phys.dynamics.joints.MouseJoint) Joint(spacegraph.space2d.phys.dynamics.joints.Joint)

Example 75 with Tuple2f

use of spacegraph.util.math.Tuple2f in project narchy by automenta.

the class Box2DTests method drawBody.

private void drawBody(Body2D body) {
    if (body.getType() == BodyType.DYNAMIC) {
        g.setColor(Color.LIGHT_GRAY);
    } else {
        g.setColor(Color.GRAY);
    }
    Tuple2f v = new Vec2();
    MyList<PolygonFixture> generalPolygons = new MyList<>();
    for (Fixture f = body.fixtures; f != null; f = f.next) {
        PolygonFixture pg = f.polygon;
        if (pg != null) {
            if (!generalPolygons.contains(pg)) {
                generalPolygons.add(pg);
            }
        } else {
            Shape shape = f.shape();
            switch(shape.m_type) {
                case POLYGON:
                    PolygonShape poly = (PolygonShape) shape;
                    for (int i = 0; i < poly.vertices; ++i) {
                        body.getWorldPointToOut(poly.vertex[i], v);
                        Point p = getPoint(v);
                        x[i] = p.x;
                        y[i] = p.y;
                    }
                    g.fillPolygon(x, y, poly.vertices);
                    break;
                case CIRCLE:
                    CircleShape circle = (CircleShape) shape;
                    float r = circle.radius;
                    body.getWorldPointToOut(circle.center, v);
                    Point p = getPoint(v);
                    int wr = (int) (r * zoom);
                    g.fillOval(p.x - wr, p.y - wr, wr * 2, wr * 2);
                    break;
                case EDGE:
                    EdgeShape edge = (EdgeShape) shape;
                    Tuple2f v1 = edge.m_vertex1;
                    Tuple2f v2 = edge.m_vertex2;
                    Point p1 = getPoint(v1);
                    Point p2 = getPoint(v2);
                    g.drawLine(p1.x, p1.y, p2.x, p2.y);
                    break;
            }
        }
    }
    if (generalPolygons.size() != 0) {
        PolygonFixture[] polygonArray = generalPolygons.toArray(new PolygonFixture[generalPolygons.size()]);
        for (PolygonFixture poly : polygonArray) {
            int n = poly.size();
            int[] x = new int[n];
            int[] y = new int[n];
            for (int i = 0; i < n; ++i) {
                body.getWorldPointToOut(poly.get(i), v);
                Point p = getPoint(v);
                x[i] = p.x;
                y[i] = p.y;
            }
            g.fillPolygon(x, y, n);
        }
    }
}
Also used : EdgeShape(spacegraph.space2d.phys.collision.shapes.EdgeShape) PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) Shape(spacegraph.space2d.phys.collision.shapes.Shape) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) EdgeShape(spacegraph.space2d.phys.collision.shapes.EdgeShape) PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) DistanceJoint(spacegraph.space2d.phys.dynamics.joints.DistanceJoint) MouseJoint(spacegraph.space2d.phys.dynamics.joints.MouseJoint) Joint(spacegraph.space2d.phys.dynamics.joints.Joint) Tuple2f(spacegraph.util.math.Tuple2f) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) Vec2(spacegraph.space2d.phys.common.Vec2) MyList(spacegraph.space2d.phys.fracture.util.MyList) PolygonFixture(spacegraph.space2d.phys.fracture.PolygonFixture) PolygonFixture(spacegraph.space2d.phys.fracture.PolygonFixture)

Aggregations

Tuple2f (spacegraph.util.math.Tuple2f)154 spacegraph.util.math.v2 (spacegraph.util.math.v2)32 Rot (spacegraph.space2d.phys.common.Rot)23 AABB (spacegraph.space2d.phys.collision.AABB)7 Vec2 (spacegraph.space2d.phys.common.Vec2)6 Body2D (spacegraph.space2d.phys.dynamics.Body2D)6 ManifoldPoint (spacegraph.space2d.phys.collision.ManifoldPoint)5 VelocityConstraintPoint (spacegraph.space2d.phys.dynamics.contacts.ContactVelocityConstraint.VelocityConstraintPoint)5 PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)4 Joint (spacegraph.space2d.phys.dynamics.joints.Joint)4 PolygonFixture (spacegraph.space2d.phys.fracture.PolygonFixture)4 MyList (spacegraph.space2d.phys.fracture.util.MyList)4 FasterList (jcog.list.FasterList)3 CircleShape (spacegraph.space2d.phys.collision.shapes.CircleShape)3 Shape (spacegraph.space2d.phys.collision.shapes.Shape)3 Transform (spacegraph.space2d.phys.common.Transform)3 DistanceJoint (spacegraph.space2d.phys.dynamics.joints.DistanceJoint)3 MouseJoint (spacegraph.space2d.phys.dynamics.joints.MouseJoint)3 Fragment (spacegraph.space2d.phys.fracture.Fragment)3 Polygon (spacegraph.space2d.phys.fracture.Polygon)3