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