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();
});
}
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;
}
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;
}
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);
}
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;
}
Aggregations