Search in sources :

Example 6 with Tuple2f

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

the class DebugDraw method getWorldToScreen.

/**
 * Takes the world coordinates and returns the screen coordinates.
 *
 * @param worldX
 * @param worldY
 */
public Tuple2f getWorldToScreen(float worldX, float worldY) {
    Tuple2f argScreen = new v2(worldX, worldY);
    viewportTransform.getWorldToScreen(argScreen, argScreen);
    return argScreen;
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) spacegraph.util.math.v2(spacegraph.util.math.v2)

Example 7 with Tuple2f

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

the class DebugDraw method getScreenToWorld.

/**
 * takes the screen coordinates and returns the world coordinates.
 *
 * @param screenX
 * @param screenY
 */
public Tuple2f getScreenToWorld(float screenX, float screenY) {
    Tuple2f screen = new v2(screenX, screenY);
    viewportTransform.getScreenToWorld(screen, screen);
    return screen;
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) spacegraph.util.math.v2(spacegraph.util.math.v2)

Example 8 with Tuple2f

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

the class Fracture method smash.

/**
 * Rozbije objekt. Upravi objekt world tak, ze vymaze triesteny objekt
 * a nahradi ho fragmentami na zaklade nastaveneho materialu a clenskych
 * premennych.
 *
 * @param dt casova dlzka framu
 */
public void smash(Smasher smasher, float dt) {
    Shape s = f1.shape;
    if (s == null)
        return;
    if (contact == null) {
        // riesi sa staticky prvok, ktory ma priliz maly obsah
        b1.setType(BodyType.DYNAMIC);
        return;
    }
    Dynamics2D w = b1.W;
    Polygon p = f1.polygon;
    if (p == null) {
        switch(s.m_type) {
            case POLYGON:
                PolygonShape ps = (PolygonShape) s;
                Tuple2f[] vertices = ps.vertex;
                int n = ps.vertices;
                p = new Polygon(n);
                for (int i = 0; i < n; ++i) {
                    p.add(vertices[n - i - 1]);
                }
                break;
            case CIRCLE:
                CircleShape cs = (CircleShape) s;
                p = new Polygon(CIRCLEVERTICES);
                float radius = cs.radius;
                double u = Math.PI * 2 / CIRCLEVERTICES;
                // upravim radius tak, aby bola zachovana velkost obsahu
                radius = (float) Math.sqrt(u / Math.sin(u)) * radius;
                Tuple2f center = cs.center;
                for (int i = 0; i < CIRCLEVERTICES; ++i) {
                    // uhol
                    double j = u * i;
                    float sin = (float) Math.sin(j);
                    float cos = (float) Math.cos(j);
                    Tuple2f v = new v2(sin, cos).scaled(radius).added(center);
                    p.add(v);
                }
                break;
            default:
                throw new RuntimeException("Dany typ tvaru nepodporuje stiepenie");
        }
    }
    // sila v zavislosti na pevnosti telesa
    float mConst = f1.material.m_rigidity / normalImpulse;
    // true, ak f2 je v objekte contact ako m_fixtureA
    boolean fixA = f1 == contact.aFixture;
    float oldAngularVelocity = fixA ? contact.m_angularVelocity_bodyA : contact.m_angularVelocity_bodyB;
    Tuple2f oldLinearVelocity = fixA ? contact.m_linearVelocity_bodyA : contact.m_linearVelocity_bodyB;
    b1.setAngularVelocity((b1.velAngular - oldAngularVelocity) * mConst + oldAngularVelocity);
    b1.setLinearVelocity(b1.vel.sub(oldLinearVelocity).scaled(mConst).added(oldLinearVelocity));
    if (!w.isFractured(f2) && b2.type == BodyType.DYNAMIC && !b2.m_fractureTransformUpdate) {
        // ak sa druhy objekt nerozbija, tak sa jej nahodia povodne hodnoty (TREBA MODIFIKOVAT POHYB OBJEKTU, KTORY SPOSOBUJE ROZPAD)
        oldAngularVelocity = !fixA ? contact.m_angularVelocity_bodyA : contact.m_angularVelocity_bodyB;
        oldLinearVelocity = !fixA ? contact.m_linearVelocity_bodyA : contact.m_linearVelocity_bodyB;
        b2.setAngularVelocity((b2.velAngular - oldAngularVelocity) * mConst + oldAngularVelocity);
        b2.setLinearVelocity(b2.vel.sub(oldLinearVelocity).scaled(mConst).added(oldLinearVelocity));
        b2.setTransform(b2.transformPrev.pos.add(b2.vel.scale(dt)), b2.transformPrev.angle());
        // osetruje jbox2d od posuvania telesa pri rieseni kolizie
        b2.m_fractureTransformUpdate = true;
    }
    Tuple2f localPoint = Transform.mulTrans(b1, point);
    Tuple2f b1Vec = b1.getLinearVelocityFromWorldPoint(point);
    Tuple2f b2Vec = b2.getLinearVelocityFromWorldPoint(point);
    Tuple2f localVelocity = b2Vec.subbed(b1Vec);
    localVelocity.scaled(dt);
    // rodeli to
    Polygon[] fragment = m.split(smasher, p, localPoint, localVelocity, normalImpulse);
    if (fragment.length <= 1) {
        // nerozbilo to na ziadne fragmenty
        return;
    }
    // definuje tela fragmentov - tie maju vsetky rovnaku definiciu (preberaju parametre z povodneho objektu)
    BodyDef bodyDef = new BodyDef();
    // pozicia
    bodyDef.position.set(b1.pos);
    // otocenie
    bodyDef.angle = b1.angle();
    bodyDef.fixedRotation = b1.isFixedRotation();
    bodyDef.angularDamping = b1.m_angularDamping;
    bodyDef.allowSleep = b1.isSleepingAllowed();
    FixtureDef fd = new FixtureDef();
    // trenie
    fd.friction = f1.friction;
    // odrazivost
    fd.restitution = f1.restitution;
    fd.isSensor = f1.isSensor;
    fd.density = f1.density;
    // odstrani fragmentacne predmety/cele teleso
    List<Fixture> fixtures = new FasterList<>();
    if (f1.polygon != null) {
        for (Fixture f = b1.fixtures; f != null; f = f.next) {
            if (f.polygon == f1.polygon) {
                fixtures.add(f);
            }
        }
    } else {
        fixtures.add(f1);
    }
    for (Fixture f : fixtures) {
        b1.removeFixture(f);
    }
    if (b1.fixtureCount == 0) {
        w.removeBody(b1);
    }
    // prida fragmenty do simulacie
    MyList<Body2D> newbodies = new MyList<>();
    for (Polygon pg : fragment) {
        // vytvori tela, prida fixtury, poriesi konvexnu dekompoziciu
        if (pg.isCorrect()) {
            if (pg instanceof Fragment) {
                Polygon[] convex = pg.convexDecomposition();
                bodyDef.type = BodyType.DYNAMIC;
                for (Polygon pgx : convex) {
                    Body2D f_body = w.addBody(bodyDef);
                    pgx.flip();
                    PolygonShape ps = new PolygonShape();
                    ps.set(pgx.getArray(), pgx.size());
                    fd.shape = ps;
                    fd.polygon = null;
                    fd.material = f1.material;
                    // .m_fragments; //rekurzivne stiepenie
                    f_body.addFixture(fd);
                    f_body.setAngularVelocity(b1.velAngular);
                    f_body.setLinearVelocity(b1.getLinearVelocityFromLocalPoint(f_body.getLocalCenter()));
                    newbodies.add(f_body);
                }
            } else {
                fd.material = // .m_fragments; //rekurzivne stiepenie
                f1.material;
                bodyDef.type = b1.getType();
                Body2D f_body = w.addBody(bodyDef);
                PolygonFixture pf = new PolygonFixture(pg);
                f_body.addFixture(pf, fd);
                f_body.setLinearVelocity(b1.getLinearVelocityFromLocalPoint(f_body.getLocalCenter()));
                f_body.setAngularVelocity(b1.velAngular);
                newbodies.add(f_body);
            }
        }
    }
    // zavola sa funkcia z fraction listeneru (pokial je nadefinovany)
    FractureListener fl = w.getContactManager().m_fractureListener;
    if (fl != null) {
        fl.action(m, normalImpulse, newbodies);
    }
}
Also used : PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) Shape(spacegraph.space2d.phys.collision.shapes.Shape) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) Tuple2f(spacegraph.util.math.Tuple2f) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) FasterList(jcog.list.FasterList) MyList(spacegraph.space2d.phys.fracture.util.MyList) spacegraph.util.math.v2(spacegraph.util.math.v2)

Example 9 with Tuple2f

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

the class Fragment method swap.

// /**
// * Zotriedi vrcholy polygonu do konvexneho polygonu, ako idu za sebou.
// * Triedi podla uhlu, aky zviera usecka tvoriaca bodmi focus a lubovolny
// * vrchol polygonu. Polygony su vacsinou velmi male, cca 8 bodov, preto
// * je vyuzivany selected sort ako najrychlejsi algoritmus na data takehoto
// * typu.
// *
// * @param focus Vlozi vnutorny bod, podla ktoreho zotriedi polygon - podla
// *              uhlu spojnice daneho bodu a parametra.
// */
// void sort(Tuple2f focus) {
// this.focus = focus;
// resort();
// }
/**
 * Vymeni 2 vrcholy polygonu
 *
 * @param i
 * @param j
 */
private void swap(int i, int j) {
    Tuple2f[] a = this.array;
    Tuple2f item = a[i];
    a[i] = a[j];
    a[j] = item;
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f)

Example 10 with Tuple2f

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

the class Polygon method centroid.

/**
 * @return Vrati tazisko polygonu.
 */
public Tuple2f centroid() {
    // centroid
    Tuple2f C = new v2();
    double m = 0;
    // pomocne vektor pre medzivypocet
    Tuple2f g = new v2();
    for (int i = 0, j = 1; i != count; i = j, j++) {
        Tuple2f b1 = get(i);
        Tuple2f b2 = get(j == count ? 0 : j);
        float s = Tuple2f.cross(b1, b2);
        m += s;
        g.set(b1);
        g.added(b2);
        g.scaled(s);
        C.added(g);
    }
    C.scaled((float) (1 / (3 * m)));
    return C;
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) spacegraph.util.math.v2(spacegraph.util.math.v2)

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