Search in sources :

Example 6 with Vec2

use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.

the class ParticleIntroApp method initGame.

@Override
protected void initGame() {
    pixelIndex = 0;
    getGameScene().setBackgroundColor(Color.BLACK);
    var texture = texture("logo/fxgl_logo1.png");
    pixels = ImagesKt.toPixels(texture.getImage()).stream().filter(p -> !p.getColor().equals(Color.TRANSPARENT)).map(p -> {
        var r = new Rectangle(1, 1, p.getColor());
        r.setLayoutX(p.getX());
        r.setLayoutY(p.getY());
        r.setScaleX(0);
        r.setScaleY(0);
        return r;
    }).collect(Collectors.toList());
    var knight = texture("logo/javafx_logo1.png");
    pixels2 = ImagesKt.toPixels(knight.getImage()).stream().filter(p -> !p.getColor().equals(Color.TRANSPARENT)).map(p -> {
        var r = new Rectangle(1, 1, p.getColor());
        r.setLayoutX(p.getX() - 340);
        r.setLayoutY(p.getY());
        return r;
    }).collect(Collectors.toList());
    // add the difference in pixels as TRANSPARENT
    int numPixels = pixels.size() - pixels2.size();
    ImagesKt.toPixels(texture.getImage()).stream().filter(p -> p.getColor().equals(Color.TRANSPARENT)).limit(numPixels).map(p -> {
        var r = new Rectangle(1, 1, Color.TRANSPARENT);
        r.setLayoutX(p.getX());
        r.setLayoutY(p.getY());
        // r.setScaleY(3);
        return r;
    }).forEach(pixels2::add);
    pixels.forEach(p -> {
        p.getProperties().put("vel", new Vec2());
        p.getProperties().put("index", pixelIndex++);
        addUINode(p, 300, 120);
    });
    System.out.println(pixels.size() + " " + pixels2.size());
}
Also used : KeyCode(javafx.scene.input.KeyCode) Color(javafx.scene.paint.Color) CubicCurve(javafx.scene.shape.CubicCurve) Rectangle(javafx.scene.shape.Rectangle) Collectors(java.util.stream.Collectors) Duration(javafx.util.Duration) List(java.util.List) Interpolators(com.almasb.fxgl.animation.Interpolators) GameSettings(com.almasb.fxgl.app.GameSettings) Vec2(com.almasb.fxgl.core.math.Vec2) Point2D(javafx.geometry.Point2D) ImagesKt(com.almasb.fxgl.texture.ImagesKt) Comparator(java.util.Comparator) GameApplication(com.almasb.fxgl.app.GameApplication) FXGL(com.almasb.fxgl.dsl.FXGL) Vec2(com.almasb.fxgl.core.math.Vec2) Rectangle(javafx.scene.shape.Rectangle)

Example 7 with Vec2

use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.

the class DefaultWorldPool method getVec2Array.

public final Vec2[] getVec2Array(int argLength) {
    if (!avecs.containsKey(argLength)) {
        Vec2[] ray = new Vec2[argLength];
        for (int i = 0; i < argLength; i++) {
            ray[i] = new Vec2();
        }
        avecs.put(argLength, ray);
    }
    assert avecs.get(argLength).length == argLength : "Array not built with correct length";
    return avecs.get(argLength);
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2)

Example 8 with Vec2

use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.

the class VarsSample method initGameVars.

@Override
protected void initGameVars(Map<String, Object> vars) {
    vars.put("testDouble", -1.5);
    vars.put("testBoolean", true);
    vars.put("vector", new Vec2(1, 1));
    vars.put("score", 0);
    vars.put("lives", 3);
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2)

Example 9 with Vec2

use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.

the class ParticleSystem method solveWall.

@SuppressWarnings("PMD.UnusedFormalParameter")
private void solveWall(TimeStep step) {
    for (int i = 0; i < m_count; i++) {
        if ((m_flagsBuffer.data[i] & ParticleTypeInternal.b2_wallParticle) != 0) {
            final Vec2 r = m_velocityBuffer.data[i];
            r.x = 0.0f;
            r.y = 0.0f;
        }
    }
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2)

Example 10 with Vec2

use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.

the class Island method solveTOI.

void solveTOI(TimeStep subStep, int toiIndexA, int toiIndexB) {
    assert toiIndexA < bodyCount;
    assert toiIndexB < bodyCount;
    // Initialize the body state.
    for (int i = 0; i < bodyCount; ++i) {
        positions[i].c.x = bodies[i].m_sweep.c.x;
        positions[i].c.y = bodies[i].m_sweep.c.y;
        positions[i].a = bodies[i].m_sweep.a;
        velocities[i].v.x = bodies[i].getLinearVelocity().x;
        velocities[i].v.y = bodies[i].getLinearVelocity().y;
        velocities[i].w = bodies[i].getAngularVelocity();
    }
    toiSolverDef.contacts = contacts;
    toiSolverDef.count = contactCount;
    toiSolverDef.step = subStep;
    toiSolverDef.positions = positions;
    toiSolverDef.velocities = velocities;
    toiContactSolver.init(toiSolverDef);
    // Solve position constraints.
    for (int i = 0; i < subStep.positionIterations; ++i) {
        boolean contactsOkay = toiContactSolver.solveTOIPositionConstraints(toiIndexA, toiIndexB);
        if (contactsOkay) {
            break;
        }
    }
    // Leap of faith to new safe state.
    bodies[toiIndexA].m_sweep.c0.x = positions[toiIndexA].c.x;
    bodies[toiIndexA].m_sweep.c0.y = positions[toiIndexA].c.y;
    bodies[toiIndexA].m_sweep.a0 = positions[toiIndexA].a;
    bodies[toiIndexB].m_sweep.c0.set(positions[toiIndexB].c);
    bodies[toiIndexB].m_sweep.a0 = positions[toiIndexB].a;
    // No warm starting is needed for TOI events because warm
    // starting impulses were applied in the discrete solver.
    toiContactSolver.initializeVelocityConstraints();
    // Solve velocity constraints.
    for (int i = 0; i < subStep.velocityIterations; ++i) {
        toiContactSolver.solveVelocityConstraints();
    }
    // Don't store the TOI contact forces for warm starting
    // because they can be quite large.
    float h = subStep.dt;
    // Integrate positions
    for (int i = 0; i < bodyCount; ++i) {
        Vec2 v = velocities[i].v;
        // Check for large velocities
        float tX = v.x * h;
        float tY = v.y * h;
        float translationSquared = tX * tX + tY * tY;
        if (translationSquared > maxTranslationSquared) {
            float ratio = maxTranslation / FXGLMath.sqrtF(translationSquared);
            v.mulLocal(ratio);
        }
        float w = velocities[i].w;
        float rotation = h * w;
        if (rotation * rotation > maxRotationSquared) {
            float ratio = maxRotation / FXGLMath.abs(rotation);
            w *= ratio;
        }
        Vec2 c = positions[i].c;
        // Integrate
        c.x += v.x * h;
        c.y += v.y * h;
        float a = positions[i].a;
        a += h * w;
        positions[i].c.x = c.x;
        positions[i].c.y = c.y;
        positions[i].a = a;
        velocities[i].v.x = v.x;
        velocities[i].v.y = v.y;
        velocities[i].w = w;
        // Sync bodies
        Body body = bodies[i];
        body.m_sweep.c.x = c.x;
        body.m_sweep.c.y = c.y;
        body.m_sweep.a = a;
        body.setLinearVelocityDirectly(v.x, v.y);
        body.setAngularVelocityDirectly(w);
        body.synchronizeTransform();
    }
    report(toiContactSolver.getVelocityConstraints());
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2) Joint(com.almasb.fxgl.physics.box2d.dynamics.joints.Joint)

Aggregations

Vec2 (com.almasb.fxgl.core.math.Vec2)138 Rotation (com.almasb.fxgl.physics.box2d.common.Rotation)36 Point2D (javafx.geometry.Point2D)7 Mat22 (com.almasb.fxgl.physics.box2d.common.Mat22)6 Body (com.almasb.fxgl.physics.box2d.dynamics.Body)6 Rectangle (javafx.scene.shape.Rectangle)6 GameApplication (com.almasb.fxgl.app.GameApplication)5 Vec3 (com.almasb.fxgl.core.math.Vec3)5 AABB (com.almasb.fxgl.physics.box2d.collision.AABB)5 ManifoldPoint (com.almasb.fxgl.physics.box2d.collision.ManifoldPoint)5 VelocityConstraintPoint (com.almasb.fxgl.physics.box2d.dynamics.contacts.ContactVelocityConstraint.VelocityConstraintPoint)5 Rectangle2D (javafx.geometry.Rectangle2D)5 Color (javafx.scene.paint.Color)5 Interpolators (com.almasb.fxgl.animation.Interpolators)4 GameSettings (com.almasb.fxgl.app.GameSettings)4 FXGL (com.almasb.fxgl.dsl.FXGL)4 ImagesKt (com.almasb.fxgl.texture.ImagesKt)4 Comparator (java.util.Comparator)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4