use of com.almasb.fxgl.core.math.Vec3 in project FXGL by AlmasB.
the class PrismaticJoint method solveVelocityConstraints.
@Override
public void solveVelocityConstraints(final SolverData data) {
Vec2 vA = data.velocities[m_indexA].v;
float wA = data.velocities[m_indexA].w;
Vec2 vB = data.velocities[m_indexB].v;
float wB = data.velocities[m_indexB].w;
float mA = m_invMassA, mB = m_invMassB;
float iA = m_invIA, iB = m_invIB;
final Vec2 temp = pool.popVec2();
// Solve linear motor constraint.
if (m_enableMotor && m_limitState != LimitState.EQUAL) {
temp.set(vB).subLocal(vA);
float Cdot = Vec2.dot(m_axis, temp) + m_a2 * wB - m_a1 * wA;
float impulse = m_motorMass * (m_motorSpeed - Cdot);
float oldImpulse = m_motorImpulse;
float maxImpulse = data.step.dt * m_maxMotorForce;
m_motorImpulse = FXGLMath.clamp(m_motorImpulse + impulse, -maxImpulse, maxImpulse);
impulse = m_motorImpulse - oldImpulse;
final Vec2 P = pool.popVec2();
P.set(m_axis).mulLocal(impulse);
float LA = impulse * m_a1;
float LB = impulse * m_a2;
vA.x -= mA * P.x;
vA.y -= mA * P.y;
wA -= iA * LA;
vB.x += mB * P.x;
vB.y += mB * P.y;
wB += iB * LB;
pool.pushVec2(1);
}
final Vec2 Cdot1 = pool.popVec2();
temp.set(vB).subLocal(vA);
Cdot1.x = Vec2.dot(m_perp, temp) + m_s2 * wB - m_s1 * wA;
Cdot1.y = wB - wA;
if (m_enableLimit && m_limitState != LimitState.INACTIVE) {
// Solve prismatic and limit constraint in block form.
float Cdot2;
temp.set(vB).subLocal(vA);
Cdot2 = Vec2.dot(m_axis, temp) + m_a2 * wB - m_a1 * wA;
final Vec3 Cdot = pool.popVec3();
Cdot.set(Cdot1.x, Cdot1.y, Cdot2);
final Vec3 f1 = pool.popVec3();
final Vec3 df = pool.popVec3();
f1.set(m_impulse);
m_K.solve33ToOut(Cdot.negateLocal(), df);
// Cdot.negateLocal(); not used anymore
m_impulse.addLocal(df);
if (m_limitState == LimitState.AT_LOWER) {
m_impulse.z = Math.max(m_impulse.z, 0.0f);
} else if (m_limitState == LimitState.AT_UPPER) {
m_impulse.z = Math.min(m_impulse.z, 0.0f);
}
// f2(1:2) = invK(1:2,1:2) * (-Cdot(1:2) - K(1:2,3) * (f2(3) - f1(3))) +
// f1(1:2)
final Vec2 b = pool.popVec2();
final Vec2 f2r = pool.popVec2();
temp.set(m_K.ez.x, m_K.ez.y).mulLocal(m_impulse.z - f1.z);
b.set(Cdot1).negateLocal().subLocal(temp);
m_K.solve22ToOut(b, f2r);
f2r.addLocal(f1.x, f1.y);
m_impulse.x = f2r.x;
m_impulse.y = f2r.y;
df.set(m_impulse).subLocal(f1);
final Vec2 P = pool.popVec2();
temp.set(m_axis).mulLocal(df.z);
P.set(m_perp).mulLocal(df.x).addLocal(temp);
float LA = df.x * m_s1 + df.y + df.z * m_a1;
float LB = df.x * m_s2 + df.y + df.z * m_a2;
vA.x -= mA * P.x;
vA.y -= mA * P.y;
wA -= iA * LA;
vB.x += mB * P.x;
vB.y += mB * P.y;
wB += iB * LB;
pool.pushVec2(3);
pool.pushVec3(3);
} else {
// Limit is inactive, just solve the prismatic constraint in block form.
final Vec2 df = pool.popVec2();
m_K.solve22ToOut(Cdot1.negateLocal(), df);
Cdot1.negateLocal();
m_impulse.x += df.x;
m_impulse.y += df.y;
final Vec2 P = pool.popVec2();
P.set(m_perp).mulLocal(df.x);
float LA = df.x * m_s1 + df.y;
float LB = df.x * m_s2 + df.y;
vA.x -= mA * P.x;
vA.y -= mA * P.y;
wA -= iA * LA;
vB.x += mB * P.x;
vB.y += mB * P.y;
wB += iB * LB;
pool.pushVec2(2);
}
// data.velocities[m_indexA].v.set(vA);
data.velocities[m_indexA].w = wA;
// data.velocities[m_indexB].v.set(vB);
data.velocities[m_indexB].w = wB;
pool.pushVec2(2);
}
use of com.almasb.fxgl.core.math.Vec3 in project FXGL by AlmasB.
the class WeldJoint method solveVelocityConstraints.
@Override
public void solveVelocityConstraints(final SolverData data) {
Vec2 vA = data.velocities[m_indexA].v;
float wA = data.velocities[m_indexA].w;
Vec2 vB = data.velocities[m_indexB].v;
float wB = data.velocities[m_indexB].w;
float mA = m_invMassA, mB = m_invMassB;
float iA = m_invIA, iB = m_invIB;
final Vec2 Cdot1 = pool.popVec2();
final Vec2 P = pool.popVec2();
final Vec2 temp = pool.popVec2();
if (m_frequencyHz > 0.0f) {
float Cdot2 = wB - wA;
float impulse2 = -m_mass.ez.z * (Cdot2 + m_bias + m_gamma * m_impulse.z);
m_impulse.z += impulse2;
wA -= iA * impulse2;
wB += iB * impulse2;
Vec2.crossToOutUnsafe(wB, m_rB, Cdot1);
Vec2.crossToOutUnsafe(wA, m_rA, temp);
Cdot1.addLocal(vB).subLocal(vA).subLocal(temp);
final Vec2 impulse1 = P;
Mat33.mul22ToOutUnsafe(m_mass, Cdot1, impulse1);
impulse1.negateLocal();
m_impulse.x += impulse1.x;
m_impulse.y += impulse1.y;
vA.x -= mA * P.x;
vA.y -= mA * P.y;
wA -= iA * Vec2.cross(m_rA, P);
vB.x += mB * P.x;
vB.y += mB * P.y;
wB += iB * Vec2.cross(m_rB, P);
} else {
Vec2.crossToOutUnsafe(wA, m_rA, temp);
Vec2.crossToOutUnsafe(wB, m_rB, Cdot1);
Cdot1.addLocal(vB).subLocal(vA).subLocal(temp);
float Cdot2 = wB - wA;
final Vec3 Cdot = pool.popVec3();
Cdot.set(Cdot1.x, Cdot1.y, Cdot2);
final Vec3 impulse = pool.popVec3();
Mat33.mulToOutUnsafe(m_mass, Cdot, impulse);
impulse.negateLocal();
m_impulse.addLocal(impulse);
P.set(impulse.x, impulse.y);
vA.x -= mA * P.x;
vA.y -= mA * P.y;
wA -= iA * (Vec2.cross(m_rA, P) + impulse.z);
vB.x += mB * P.x;
vB.y += mB * P.y;
wB += iB * (Vec2.cross(m_rB, P) + impulse.z);
pool.pushVec3(2);
}
// data.velocities[m_indexA].v.set(vA);
data.velocities[m_indexA].w = wA;
// data.velocities[m_indexB].v.set(vB);
data.velocities[m_indexB].w = wB;
pool.pushVec2(3);
}
use of com.almasb.fxgl.core.math.Vec3 in project FXGL by AlmasB.
the class WeldJoint method solvePositionConstraints.
@Override
public boolean solvePositionConstraints(final SolverData data) {
Vec2 cA = data.positions[m_indexA].c;
float aA = data.positions[m_indexA].a;
Vec2 cB = data.positions[m_indexB].c;
float aB = data.positions[m_indexB].a;
final Rotation qA = pool.popRot();
final Rotation qB = pool.popRot();
final Vec2 temp = pool.popVec2();
final Vec2 rA = pool.popVec2();
final Vec2 rB = pool.popVec2();
qA.set(aA);
qB.set(aB);
float mA = m_invMassA, mB = m_invMassB;
float iA = m_invIA, iB = m_invIB;
Rotation.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), rA);
Rotation.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), rB);
float positionError, angularError;
final Mat33 K = pool.popMat33();
final Vec2 C1 = pool.popVec2();
final Vec2 P = pool.popVec2();
K.ex.x = mA + mB + rA.y * rA.y * iA + rB.y * rB.y * iB;
K.ey.x = -rA.y * rA.x * iA - rB.y * rB.x * iB;
K.ez.x = -rA.y * iA - rB.y * iB;
K.ex.y = K.ey.x;
K.ey.y = mA + mB + rA.x * rA.x * iA + rB.x * rB.x * iB;
K.ez.y = rA.x * iA + rB.x * iB;
K.ex.z = K.ez.x;
K.ey.z = K.ez.y;
K.ez.z = iA + iB;
if (m_frequencyHz > 0.0f) {
C1.set(cB).addLocal(rB).subLocal(cA).subLocal(rA);
positionError = C1.length();
angularError = 0.0f;
K.solve22ToOut(C1, P);
P.negateLocal();
cA.x -= mA * P.x;
cA.y -= mA * P.y;
aA -= iA * Vec2.cross(rA, P);
cB.x += mB * P.x;
cB.y += mB * P.y;
aB += iB * Vec2.cross(rB, P);
} else {
C1.set(cB).addLocal(rB).subLocal(cA).subLocal(rA);
float C2 = aB - aA - m_referenceAngle;
positionError = C1.length();
angularError = FXGLMath.abs(C2);
final Vec3 C = pool.popVec3();
final Vec3 impulse = pool.popVec3();
C.set(C1.x, C1.y, C2);
K.solve33ToOut(C, impulse);
impulse.negateLocal();
P.set(impulse.x, impulse.y);
cA.x -= mA * P.x;
cA.y -= mA * P.y;
aA -= iA * (Vec2.cross(rA, P) + impulse.z);
cB.x += mB * P.x;
cB.y += mB * P.y;
aB += iB * (Vec2.cross(rB, P) + impulse.z);
pool.pushVec3(2);
}
// data.positions[m_indexA].c.set(cA);
data.positions[m_indexA].a = aA;
// data.positions[m_indexB].c.set(cB);
data.positions[m_indexB].a = aB;
pool.pushVec2(5);
pool.pushRot(2);
pool.pushMat33(1);
return positionError <= JBoxSettings.linearSlop && angularError <= JBoxSettings.angularSlop;
}
use of com.almasb.fxgl.core.math.Vec3 in project FXGL by AlmasB.
the class PrismaticJoint method solvePositionConstraints.
@Override
public boolean solvePositionConstraints(final SolverData data) {
final Rotation qA = pool.popRot();
final Rotation qB = pool.popRot();
final Vec2 rA = pool.popVec2();
final Vec2 rB = pool.popVec2();
final Vec2 d = pool.popVec2();
final Vec2 axis = pool.popVec2();
final Vec2 perp = pool.popVec2();
final Vec2 temp = pool.popVec2();
final Vec2 C1 = pool.popVec2();
final Vec3 impulse = pool.popVec3();
Vec2 cA = data.positions[m_indexA].c;
float aA = data.positions[m_indexA].a;
Vec2 cB = data.positions[m_indexB].c;
float aB = data.positions[m_indexB].a;
qA.set(aA);
qB.set(aB);
float mA = m_invMassA, mB = m_invMassB;
float iA = m_invIA, iB = m_invIB;
// Compute fresh Jacobians
Rotation.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), rA);
Rotation.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), rB);
d.set(cB).addLocal(rB).subLocal(cA).subLocal(rA);
Rotation.mulToOutUnsafe(qA, m_localXAxisA, axis);
float a1 = Vec2.cross(temp.set(d).addLocal(rA), axis);
float a2 = Vec2.cross(rB, axis);
Rotation.mulToOutUnsafe(qA, m_localYAxisA, perp);
float s1 = Vec2.cross(temp.set(d).addLocal(rA), perp);
float s2 = Vec2.cross(rB, perp);
C1.x = Vec2.dot(perp, d);
C1.y = aB - aA - m_referenceAngle;
float linearError = FXGLMath.abs(C1.x);
float angularError = FXGLMath.abs(C1.y);
boolean active = false;
float C2 = 0.0f;
if (m_enableLimit) {
float translation = Vec2.dot(axis, d);
if (FXGLMath.abs(m_upperTranslation - m_lowerTranslation) < 2.0f * JBoxSettings.linearSlop) {
// Prevent large angular corrections
C2 = FXGLMath.clamp(translation, -JBoxSettings.maxLinearCorrection, JBoxSettings.maxLinearCorrection);
linearError = Math.max(linearError, FXGLMath.abs(translation));
active = true;
} else if (translation <= m_lowerTranslation) {
// Prevent large linear corrections and allow some slop.
C2 = FXGLMath.clamp(translation - m_lowerTranslation + JBoxSettings.linearSlop, -JBoxSettings.maxLinearCorrection, 0.0f);
linearError = Math.max(linearError, m_lowerTranslation - translation);
active = true;
} else if (translation >= m_upperTranslation) {
// Prevent large linear corrections and allow some slop.
C2 = FXGLMath.clamp(translation - m_upperTranslation - JBoxSettings.linearSlop, 0.0f, JBoxSettings.maxLinearCorrection);
linearError = Math.max(linearError, translation - m_upperTranslation);
active = true;
}
}
if (active) {
float k11 = mA + mB + iA * s1 * s1 + iB * s2 * s2;
float k12 = iA * s1 + iB * s2;
float k13 = iA * s1 * a1 + iB * s2 * a2;
float k22 = iA + iB;
if (k22 == 0.0f) {
// For fixed rotation
k22 = 1.0f;
}
float k23 = iA * a1 + iB * a2;
float k33 = mA + mB + iA * a1 * a1 + iB * a2 * a2;
final Mat33 K = pool.popMat33();
K.ex.set(k11, k12, k13);
K.ey.set(k12, k22, k23);
K.ez.set(k13, k23, k33);
final Vec3 C = pool.popVec3();
C.x = C1.x;
C.y = C1.y;
C.z = C2;
K.solve33ToOut(C.negateLocal(), impulse);
pool.pushVec3(1);
pool.pushMat33(1);
} else {
float k11 = mA + mB + iA * s1 * s1 + iB * s2 * s2;
float k12 = iA * s1 + iB * s2;
float k22 = iA + iB;
if (k22 == 0.0f) {
k22 = 1.0f;
}
final Mat22 K = pool.popMat22();
K.ex.set(k11, k12);
K.ey.set(k12, k22);
// temp is impulse1
K.solveToOut(C1.negateLocal(), temp);
C1.negateLocal();
impulse.x = temp.x;
impulse.y = temp.y;
impulse.z = 0.0f;
pool.pushMat22(1);
}
float Px = impulse.x * perp.x + impulse.z * axis.x;
float Py = impulse.x * perp.y + impulse.z * axis.y;
float LA = impulse.x * s1 + impulse.y + impulse.z * a1;
float LB = impulse.x * s2 + impulse.y + impulse.z * a2;
cA.x -= mA * Px;
cA.y -= mA * Py;
aA -= iA * LA;
cB.x += mB * Px;
cB.y += mB * Py;
aB += iB * LB;
// data.positions[m_indexA].c.set(cA);
data.positions[m_indexA].a = aA;
// data.positions[m_indexB].c.set(cB);
data.positions[m_indexB].a = aB;
pool.pushVec2(7);
pool.pushVec3(1);
pool.pushRot(2);
return linearError <= JBoxSettings.linearSlop && angularError <= JBoxSettings.angularSlop;
}
use of com.almasb.fxgl.core.math.Vec3 in project FXGL by AlmasB.
the class RevoluteJoint method solveVelocityConstraints.
@Override
public void solveVelocityConstraints(final SolverData data) {
Vec2 vA = data.velocities[m_indexA].v;
float wA = data.velocities[m_indexA].w;
Vec2 vB = data.velocities[m_indexB].v;
float wB = data.velocities[m_indexB].w;
float mA = m_invMassA, mB = m_invMassB;
float iA = m_invIA, iB = m_invIB;
boolean fixedRotation = iA + iB == 0.0f;
// Solve motor constraint.
if (m_enableMotor && m_limitState != LimitState.EQUAL && !fixedRotation) {
float Cdot = wB - wA - m_motorSpeed;
float impulse = -m_motorMass * Cdot;
float oldImpulse = m_motorImpulse;
float maxImpulse = data.step.dt * m_maxMotorTorque;
m_motorImpulse = FXGLMath.clamp(m_motorImpulse + impulse, -maxImpulse, maxImpulse);
impulse = m_motorImpulse - oldImpulse;
wA -= iA * impulse;
wB += iB * impulse;
}
final Vec2 temp = pool.popVec2();
// Solve limit constraint.
if (m_enableLimit && m_limitState != LimitState.INACTIVE && !fixedRotation) {
final Vec2 Cdot1 = pool.popVec2();
final Vec3 Cdot = pool.popVec3();
// Solve point-to-point constraint
Vec2.crossToOutUnsafe(wA, m_rA, temp);
Vec2.crossToOutUnsafe(wB, m_rB, Cdot1);
Cdot1.addLocal(vB).subLocal(vA).subLocal(temp);
float Cdot2 = wB - wA;
Cdot.set(Cdot1.x, Cdot1.y, Cdot2);
Vec3 impulse = pool.popVec3();
m_mass.solve33ToOut(Cdot, impulse);
impulse.negateLocal();
if (m_limitState == LimitState.EQUAL) {
m_impulse.addLocal(impulse);
} else if (m_limitState == LimitState.AT_LOWER) {
float newImpulse = m_impulse.z + impulse.z;
if (newImpulse < 0.0f) {
final Vec2 rhs = pool.popVec2();
rhs.set(m_mass.ez.x, m_mass.ez.y).mulLocal(m_impulse.z).subLocal(Cdot1);
m_mass.solve22ToOut(rhs, temp);
impulse.x = temp.x;
impulse.y = temp.y;
impulse.z = -m_impulse.z;
m_impulse.x += temp.x;
m_impulse.y += temp.y;
m_impulse.z = 0.0f;
pool.pushVec2(1);
} else {
m_impulse.addLocal(impulse);
}
} else if (m_limitState == LimitState.AT_UPPER) {
float newImpulse = m_impulse.z + impulse.z;
if (newImpulse > 0.0f) {
final Vec2 rhs = pool.popVec2();
rhs.set(m_mass.ez.x, m_mass.ez.y).mulLocal(m_impulse.z).subLocal(Cdot1);
m_mass.solve22ToOut(rhs, temp);
impulse.x = temp.x;
impulse.y = temp.y;
impulse.z = -m_impulse.z;
m_impulse.x += temp.x;
m_impulse.y += temp.y;
m_impulse.z = 0.0f;
pool.pushVec2(1);
} else {
m_impulse.addLocal(impulse);
}
}
final Vec2 P = pool.popVec2();
P.set(impulse.x, impulse.y);
vA.x -= mA * P.x;
vA.y -= mA * P.y;
wA -= iA * (Vec2.cross(m_rA, P) + impulse.z);
vB.x += mB * P.x;
vB.y += mB * P.y;
wB += iB * (Vec2.cross(m_rB, P) + impulse.z);
pool.pushVec2(2);
pool.pushVec3(2);
} else {
// Solve point-to-point constraint
Vec2 Cdot = pool.popVec2();
Vec2 impulse = pool.popVec2();
Vec2.crossToOutUnsafe(wA, m_rA, temp);
Vec2.crossToOutUnsafe(wB, m_rB, Cdot);
Cdot.addLocal(vB).subLocal(vA).subLocal(temp);
// just leave negated
m_mass.solve22ToOut(Cdot.negateLocal(), impulse);
m_impulse.x += impulse.x;
m_impulse.y += impulse.y;
vA.x -= mA * impulse.x;
vA.y -= mA * impulse.y;
wA -= iA * Vec2.cross(m_rA, impulse);
vB.x += mB * impulse.x;
vB.y += mB * impulse.y;
wB += iB * Vec2.cross(m_rB, impulse);
pool.pushVec2(2);
}
// data.velocities[m_indexA].v.set(vA);
data.velocities[m_indexA].w = wA;
// data.velocities[m_indexB].v.set(vB);
data.velocities[m_indexB].w = wB;
pool.pushVec2(1);
}