Search in sources :

Example 16 with PolygonShape

use of spacegraph.space2d.phys.collision.shapes.PolygonShape in project narchy by automenta.

the class ParticlesTest method init.

@Override
public void init(Dynamics2D m_world) {
    // first one
    Body2D ground = m_world.bodies().iterator().next();
    {
        PolygonShape shape = new PolygonShape();
        v2[] vertices = new v2[] { new v2(-40, -10), new v2(40, -10), new v2(40, 0), new v2(-40, 0) };
        shape.set(vertices, 4);
        ground.addFixture(shape, 0.0f);
    }
    {
        PolygonShape shape = new PolygonShape();
        v2[] vertices = { new v2(-40, -1), new v2(-20, -1), new v2(-20, 20), new v2(-40, 30) };
        shape.set(vertices, 4);
        ground.addFixture(shape, 0.0f);
    }
    {
        PolygonShape shape = new PolygonShape();
        v2[] vertices = { new v2(20, -1), new v2(40, -1), new v2(40, 30), new v2(20, 20) };
        shape.set(vertices, 4);
        ground.addFixture(shape, 0.0f);
    }
    m_world.setParticleRadius(0.35f);
    m_world.setParticleDamping(0.2f);
    {
        CircleShape shape = new CircleShape();
        shape.center.set(0, 30);
        shape.radius = 20;
        ParticleGroupDef pd = new ParticleGroupDef();
        pd.flags = ParticleType.b2_waterParticle;
        pd.shape = shape;
        m_world.addParticles(pd);
    }
    {
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        Body2D body = m_world.addBody(bd);
        CircleShape shape = new CircleShape();
        shape.center.set(0, 80);
        shape.radius = 5;
        body.addFixture(shape, 0.5f);
    }
}
Also used : ParticleGroupDef(spacegraph.space2d.phys.particle.ParticleGroupDef) PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) spacegraph.util.math.v2(spacegraph.util.math.v2) BodyDef(spacegraph.space2d.phys.dynamics.BodyDef) Body2D(spacegraph.space2d.phys.dynamics.Body2D)

Example 17 with PolygonShape

use of spacegraph.space2d.phys.collision.shapes.PolygonShape in project narchy by automenta.

the class Rover method newTorso.

@Override
protected Body newTorso() {
    PolygonShape shape = new PolygonShape();
    Vec2[] vertices = { new Vec2(3.0f, 0.0f), new Vec2(-1.0f, +2.0f), new Vec2(-1.0f, -2.0f) };
    shape.set(vertices, vertices.length);
    // shape.m_centroid.set(bodyDef.position);
    BodyDef bd = new BodyDef();
    bd.linearDamping = (linearDamping);
    bd.angularDamping = (angularDamping);
    bd.type = BodyType.DYNAMIC;
    bd.position.set(0, 0);
    Body torso = getWorld().createBody(bd);
    Fixture f = torso.createFixture(shape, mass);
    f.setRestitution(restitution);
    f.setFriction(friction);
    // for (int i = -pixels / 2; i <= pixels / 2; i++) {
    for (int i = 0; i < retinaPixels; i++) {
        final int ii = i;
        final float angle = /*MathUtils.PI / 2f*/
        aStep * i;
        final boolean eats = ((angle < mouthArc / 2f) || (angle > (Math.PI * 2f) - mouthArc / 2f));
        // System.out.println(i + " " + angle + " " + eats);
        VisionRay v = new VisionRay(this, torso, /*eats ?*/
        mouthPoint, /*: new Vec2(0,0)*/
        angle, aStep, retinaRaysPerPixel, L, distanceResolution) {

            @Override
            public void onTouch(Body touched, float di) {
                if (touched == null)
                    return;
                if (touched.getUserData() instanceof Sim.Edible) {
                    if (eats) {
                        if (di <= biteDistanceThreshold)
                            eat(touched);
                    /*} else if (di <= tasteDistanceThreshold) {
                                //taste(touched, di );
                            }*/
                    }
                }
            }
        };
        v.sparkColor = new Color3f(0.5f, 0.4f, 0.4f);
        v.normalColor = new Color3f(0.4f, 0.4f, 0.4f);
        ((JoglAbstractDraw) draw).addLayer(v);
        senses.add(v);
    }
    return torso;
}
Also used : PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) VisionRay(nars.rover.obj.VisionRay) Color3f(spacegraph.space2d.phys.common.Color3f) Vec2(spacegraph.space2d.phys.common.Vec2) Fixture(spacegraph.space2d.phys.dynamics.Fixture) BodyDef(spacegraph.space2d.phys.dynamics.BodyDef) Body(spacegraph.space2d.phys.dynamics.Body) JoglAbstractDraw(nars.rover.physics.gl.JoglAbstractDraw)

Example 18 with PolygonShape

use of spacegraph.space2d.phys.collision.shapes.PolygonShape in project narchy by automenta.

the class BoxEntity method getBoxShape.

/**
 * Gets a box shape with a given width and height
 */
private static FixtureDef getBoxShape(float width, float height, float density) {
    PolygonShape box = new PolygonShape();
    box.setAsBox(width, height);
    FixtureDef fixture = new FixtureDef();
    fixture.shape = box;
    // fixture.filter.categoryBits = CollisionFilters.ENTITY;
    // fixture.filter.maskBits = CollisionFilters.EVERYTHING;
    fixture.density = density;
    return fixture;
}
Also used : PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) FixtureDef(spacegraph.space2d.phys.dynamics.FixtureDef)

Example 19 with PolygonShape

use of spacegraph.space2d.phys.collision.shapes.PolygonShape in project narchy by automenta.

the class Collision method collidePolygons.

/**
 * Compute the collision manifold between two polygons.
 *
 * @param manifold
 * @param polygon1
 * @param xf1
 * @param polygon2
 * @param xf2
 */
public final void collidePolygons(Manifold manifold, final PolygonShape polyA, final Transform xfA, final PolygonShape polyB, final Transform xfB) {
    // Find edge normal of max separation on A - return if separating axis is found
    // Find edge normal of max separation on B - return if separation axis is found
    // Choose reference edge as min(minA, minB)
    // Find incident edge
    // Clip
    // The normal points from 1 to 2
    manifold.pointCount = 0;
    float totalRadius = polyA.radius + polyB.radius;
    findMaxSeparation(results1, polyA, xfA, polyB, xfB);
    if (results1.separation > totalRadius) {
        return;
    }
    findMaxSeparation(results2, polyB, xfB, polyA, xfA);
    if (results2.separation > totalRadius) {
        return;
    }
    // reference polygon
    final PolygonShape poly1;
    // incident polygon
    final PolygonShape poly2;
    Transform xf1, xf2;
    // reference edge
    int edge1;
    boolean flip;
    final float k_tol = 0.1f * Settings.linearSlop;
    if (results2.separation > results1.separation + k_tol) {
        poly1 = polyB;
        poly2 = polyA;
        xf1 = xfB;
        xf2 = xfA;
        edge1 = results2.edgeIndex;
        manifold.type = ManifoldType.FACE_B;
        flip = true;
    } else {
        poly1 = polyA;
        poly2 = polyB;
        xf1 = xfA;
        xf2 = xfB;
        edge1 = results1.edgeIndex;
        manifold.type = ManifoldType.FACE_A;
        flip = false;
    }
    final Rot xf1q = xf1;
    findIncidentEdge(incidentEdge, poly1, xf1, edge1, poly2, xf2);
    int count1 = poly1.vertices;
    final Tuple2f[] vertices1 = poly1.vertex;
    final int iv1 = edge1;
    final int iv2 = edge1 + 1 < count1 ? edge1 + 1 : 0;
    v11.set(vertices1[iv1]);
    v12.set(vertices1[iv2]);
    localTangent.x = v12.x - v11.x;
    localTangent.y = v12.y - v11.y;
    localTangent.normalize();
    // Vec2 localNormal = Vec2.cross(dv, 1.0f);
    localNormal.x = 1f * localTangent.y;
    localNormal.y = -1f * localTangent.x;
    // Vec2 planePoint = 0.5f * (v11+ v12);
    planePoint.x = (v11.x + v12.x) * .5f;
    planePoint.y = (v11.y + v12.y) * .5f;
    // Rot.mulToOutUnsafe(xf1.q, localTangent, tangent);
    tangent.x = xf1q.c * localTangent.x - xf1q.s * localTangent.y;
    tangent.y = xf1q.s * localTangent.x + xf1q.c * localTangent.y;
    // Vec2.crossToOutUnsafe(tangent, 1f, normal);
    final float normalx = 1f * tangent.y;
    final float normaly = -1f * tangent.x;
    Transform.mulToOut(xf1, v11, v11);
    Transform.mulToOut(xf1, v12, v12);
    // v11 = Mul(xf1, v11);
    // v12 = Mul(xf1, v12);
    // Face offset
    // float frontOffset = Vec2.dot(normal, v11);
    float frontOffset = normalx * v11.x + normaly * v11.y;
    // Side offsets, extended by polytope skin thickness.
    // float sideOffset1 = -Vec2.dot(tangent, v11) + totalRadius;
    // float sideOffset2 = Vec2.dot(tangent, v12) + totalRadius;
    float sideOffset1 = -(tangent.x * v11.x + tangent.y * v11.y) + totalRadius;
    float sideOffset2 = tangent.x * v12.x + tangent.y * v12.y + totalRadius;
    // Clip incident edge against extruded edge1 side edges.
    // ClipVertex clipPoints1[2];
    // ClipVertex clipPoints2[2];
    int np;
    // Clip to box side 1
    // np = ClipSegmentToLine(clipPoints1, incidentEdge, -sideNormal, sideOffset1);
    tangent.negated();
    np = clipSegmentToLine(clipPoints1, incidentEdge, tangent, sideOffset1, iv1);
    tangent.negated();
    if (np < 2) {
        return;
    }
    // Clip to negative box side 1
    np = clipSegmentToLine(clipPoints2, clipPoints1, tangent, sideOffset2, iv2);
    if (np < 2) {
        return;
    }
    // Now clipPoints2 contains the clipped points.
    manifold.localNormal.set(localNormal);
    manifold.localPoint.set(planePoint);
    int pointCount = 0;
    for (int i = 0; i < Settings.maxManifoldPoints; ++i) {
        // float separation = Vec2.dot(normal, clipPoints2[i].v) - frontOffset;
        float separation = normalx * clipPoints2[i].v.x + normaly * clipPoints2[i].v.y - frontOffset;
        if (separation <= totalRadius) {
            ManifoldPoint cp = manifold.points[pointCount];
            // cp.m_localPoint = MulT(xf2, clipPoints2[i].v);
            Tuple2f out = cp.localPoint;
            final float px = clipPoints2[i].v.x - xf2.pos.x;
            final float py = clipPoints2[i].v.y - xf2.pos.y;
            out.x = (xf2.c * px + xf2.s * py);
            out.y = (-xf2.s * px + xf2.c * py);
            cp.id.set(clipPoints2[i].id);
            if (flip) {
                // Swap features
                cp.id.flip();
            }
            ++pointCount;
        }
    }
    manifold.pointCount = pointCount;
}
Also used : PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) Tuple2f(spacegraph.util.math.Tuple2f) Rot(spacegraph.space2d.phys.common.Rot) Transform(spacegraph.space2d.phys.common.Transform)

Example 20 with PolygonShape

use of spacegraph.space2d.phys.collision.shapes.PolygonShape in project narchy by automenta.

the class RoverWorld method addBlock.

public Body addBlock(float x, float y, float w, float h, float a, float mass) {
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(w, h);
    BodyDef bd = new BodyDef();
    if (mass != 0) {
        bd.linearDamping = (0.95f);
        bd.angularDamping = (0.8f);
        bd.type = BodyType.DYNAMIC;
    } else {
        bd.type = BodyType.STATIC;
    }
    bd.position.set(x, y);
    Body body = p.getWorld().createBody(bd);
    Fixture fd = body.createFixture(shape, mass);
    fd.setRestitution(1f);
    return body;
}
Also used : PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) Fixture(spacegraph.space2d.phys.dynamics.Fixture) BodyDef(spacegraph.space2d.phys.dynamics.BodyDef) Body(spacegraph.space2d.phys.dynamics.Body)

Aggregations

PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)21 spacegraph.util.math.v2 (spacegraph.util.math.v2)8 CircleShape (spacegraph.space2d.phys.collision.shapes.CircleShape)7 Vec2 (spacegraph.space2d.phys.common.Vec2)6 BodyDef (spacegraph.space2d.phys.dynamics.BodyDef)6 Body (spacegraph.space2d.phys.dynamics.Body)4 Fixture (spacegraph.space2d.phys.dynamics.Fixture)4 RevoluteJointDef (spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef)4 Tuple2f (spacegraph.util.math.Tuple2f)4 EdgeShape (spacegraph.space2d.phys.collision.shapes.EdgeShape)3 Shape (spacegraph.space2d.phys.collision.shapes.Shape)3 FixtureDef (spacegraph.space2d.phys.dynamics.FixtureDef)3 Physics2dBody (ptrman.difficultyEnvironment.physics.Physics2dBody)2 Body2D (spacegraph.space2d.phys.dynamics.Body2D)2 DistanceJointDef (spacegraph.space2d.phys.dynamics.joints.DistanceJointDef)2 RevoluteJoint (spacegraph.space2d.phys.dynamics.joints.RevoluteJoint)2 PolygonFixture (spacegraph.space2d.phys.fracture.PolygonFixture)2 MyList (spacegraph.space2d.phys.fracture.util.MyList)2 ParticleGroupDef (spacegraph.space2d.phys.particle.ParticleGroupDef)2 Random (java.util.Random)1