Search in sources :

Example 71 with Vec2

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

the class ParticleMorphApp method initInput.

@Override
protected void initInput() {
    onKeyDown(KeyCode.F, () -> {
        delayIndex = 0.0;
        pixels.stream().sorted(Comparator.comparingDouble(p -> p.getLayoutY())).forEach(p -> {
            // p.setBlendMode(BlendMode.ADD);
            animationBuilder().duration(Duration.seconds(0.2)).interpolator(Interpolators.EXPONENTIAL.EASE_OUT()).onFinished(() -> {
                animationBuilder().delay(Duration.seconds(random(0.1, 0.6))).onFinished(() -> {
                    animationBuilder().delay(Duration.seconds(random(delayIndex, delayIndex + 0.1))).duration(Duration.seconds(1.5)).interpolator(Interpolators.EXPONENTIAL.EASE_IN()).translate(p).alongPath(new CubicCurve(p.getTranslateX(), p.getTranslateY(), random(200, 300), random(-200, 20), random(600, 700), random(500, 700), 650, 150)).buildAndPlay();
                    delayIndex += 0.0001;
                }).duration(Duration.seconds(0.75)).interpolator(Interpolators.BOUNCE.EASE_OUT()).animate(new AnimatedValue<>(0.0, 1.0)).onProgress(progress -> {
                    var x = p.getTranslateX();
                    var y = p.getTranslateY();
                    var noiseValue = FXGLMath.noise2D(x * 0.002 * progress, y * 0.002 * t);
                    var angle = FXGLMath.toDegrees((noiseValue + 1) * Math.PI * 1.5);
                    angle %= 360.0;
                    var v = Vec2.fromAngle(angle).normalizeLocal().mulLocal(FXGLMath.random(1.0, 25));
                    Vec2 velocity = (Vec2) p.getProperties().get("vel");
                    var vx = velocity.x * 0.8f + v.x * 0.2f;
                    var vy = velocity.y * 0.8f + v.y * 0.2f;
                    velocity.x = vx;
                    velocity.y = vy;
                    p.setTranslateX(x + velocity.x);
                    p.setTranslateY(y + velocity.y);
                }).buildAndPlay();
            }).scale(p).from(new Point2D(1, 1)).to(new Point2D(3, 3)).buildAndPlay();
        });
    });
    onKeyDown(KeyCode.G, () -> {
        morph();
    });
}
Also used : KeyCode(javafx.scene.input.KeyCode) Color(javafx.scene.paint.Color) Rectangle2D(javafx.geometry.Rectangle2D) CubicCurve(javafx.scene.shape.CubicCurve) FXGLMath(com.almasb.fxgl.core.math.FXGLMath) Rectangle(javafx.scene.shape.Rectangle) Collectors(java.util.stream.Collectors) Duration(javafx.util.Duration) List(java.util.List) Interpolators(com.almasb.fxgl.animation.Interpolators) AnimatedValue(com.almasb.fxgl.animation.AnimatedValue) 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) CubicCurve(javafx.scene.shape.CubicCurve) Point2D(javafx.geometry.Point2D) Vec2(com.almasb.fxgl.core.math.Vec2)

Example 72 with Vec2

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

the class Mat33 method solve22.

/**
 * Solve A * x = b, where b is a column vector. This is more efficient than computing the inverse
 * in one-shot cases.
 *
 * @param b
 * @return
 */
public final Vec2 solve22(Vec2 b) {
    Vec2 x = new Vec2();
    solve22ToOut(b, x);
    return x;
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2)

Example 73 with Vec2

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

the class SAT method isColliding.

/**
 * Note: NOT thread-safe but GC-friendly.
 *
 * @param box1 hit box 1
 * @param box2 hit box 2
 * @param angle1 angle of hit box 1
 * @param angle2 angle of hit box 2
 * @return true if two hit boxes with respective angles are colliding
 */
public static boolean isColliding(HitBox box1, HitBox box2, double angle1, double angle2) {
    populateAxes(angle1);
    populateAxes(angle2);
    corners(box1, angle1, corners1);
    corners(box2, angle2, corners2);
    boolean result = true;
    for (Vec2 axis : axes) {
        float e1Min = getMin(corners1, axis);
        float e1Max = getMax(corners1, axis);
        float e2Min = getMin(corners2, axis);
        float e2Max = getMax(corners2, axis);
        if (e1Max < e2Min || e2Max < e1Min) {
            result = false;
            break;
        }
    }
    cleanArrays();
    return result;
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2)

Example 74 with Vec2

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

the class SAT method corners.

private static void corners(HitBox box, double angle, Array<Vec2> array) {
    Vec2 center = center(box);
    Vec2 topLeft = newVec(box.getMinXWorld(), box.getMinYWorld());
    Vec2 topRight = newVec(box.getMaxXWorld(), box.getMinYWorld());
    Vec2 botRight = newVec(box.getMaxXWorld(), box.getMaxYWorld());
    Vec2 botLeft = newVec(box.getMinXWorld(), box.getMaxYWorld());
    array.addAll(topLeft, topRight, botRight, botLeft);
    for (Vec2 v : array) {
        double cos = cos(angle);
        double sin = sin(angle);
        v.subLocal(center);
        v.set((float) (v.x * cos - v.y * sin), (float) (v.x * sin + v.y * cos));
        v.addLocal(center);
    }
    freeVec(center);
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2)

Example 75 with Vec2

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

the class PolygonShape method validate.

/**
 * Validate convexity. This is a very time consuming operation.
 *
 * @return
 */
public boolean validate() {
    for (int i = 0; i < vertexCount; ++i) {
        int i1 = i;
        int i2 = i < vertexCount - 1 ? i1 + 1 : 0;
        Vec2 p = m_vertices[i1];
        Vec2 e = pool1.set(m_vertices[i2]).subLocal(p);
        for (int j = 0; j < vertexCount; ++j) {
            if (j == i1 || j == i2) {
                continue;
            }
            Vec2 v = pool2.set(m_vertices[j]).subLocal(p);
            float c = Vec2.cross(e, v);
            if (c < 0.0f) {
                return false;
            }
        }
    }
    return true;
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2)

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