use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.
the class PulleyJoint 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 uA = pool.popVec2();
final Vec2 uB = pool.popVec2();
final Vec2 temp = pool.popVec2();
final Vec2 PA = pool.popVec2();
final Vec2 PB = pool.popVec2();
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);
Rotation.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), rA);
Rotation.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), rB);
uA.set(cA).addLocal(rA).subLocal(m_groundAnchorA);
uB.set(cB).addLocal(rB).subLocal(m_groundAnchorB);
float lengthA = uA.length();
float lengthB = uB.length();
if (lengthA > 10.0f * JBoxSettings.linearSlop) {
uA.mulLocal(1.0f / lengthA);
} else {
uA.setZero();
}
if (lengthB > 10.0f * JBoxSettings.linearSlop) {
uB.mulLocal(1.0f / lengthB);
} else {
uB.setZero();
}
// Compute effective mass.
float ruA = Vec2.cross(rA, uA);
float ruB = Vec2.cross(rB, uB);
float mA = m_invMassA + m_invIA * ruA * ruA;
float mB = m_invMassB + m_invIB * ruB * ruB;
float mass = mA + m_ratio * m_ratio * mB;
if (mass > 0.0f) {
mass = 1.0f / mass;
}
float C = m_constant - lengthA - m_ratio * lengthB;
float linearError = FXGLMath.abs(C);
float impulse = -mass * C;
PA.set(uA).mulLocal(-impulse);
PB.set(uB).mulLocal(-m_ratio * impulse);
cA.x += m_invMassA * PA.x;
cA.y += m_invMassA * PA.y;
aA += m_invIA * Vec2.cross(rA, PA);
cB.x += m_invMassB * PB.x;
cB.y += m_invMassB * PB.y;
aB += m_invIB * Vec2.cross(rB, PB);
// 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.pushRot(2);
pool.pushVec2(7);
return linearError < JBoxSettings.linearSlop;
}
use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.
the class PulleyJoint method initVelocityConstraints.
@Override
public void initVelocityConstraints(final SolverData data) {
m_indexA = m_bodyA.m_islandIndex;
m_indexB = m_bodyB.m_islandIndex;
m_localCenterA.set(m_bodyA.m_sweep.localCenter);
m_localCenterB.set(m_bodyB.m_sweep.localCenter);
m_invMassA = m_bodyA.m_invMass;
m_invMassB = m_bodyB.m_invMass;
m_invIA = m_bodyA.m_invI;
m_invIB = m_bodyB.m_invI;
Vec2 cA = data.positions[m_indexA].c;
float aA = data.positions[m_indexA].a;
Vec2 vA = data.velocities[m_indexA].v;
float wA = data.velocities[m_indexA].w;
Vec2 cB = data.positions[m_indexB].c;
float aB = data.positions[m_indexB].a;
Vec2 vB = data.velocities[m_indexB].v;
float wB = data.velocities[m_indexB].w;
final Rotation qA = pool.popRot();
final Rotation qB = pool.popRot();
final Vec2 temp = pool.popVec2();
qA.set(aA);
qB.set(aB);
// Compute the effective masses.
Rotation.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), m_rA);
Rotation.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), m_rB);
m_uA.set(cA).addLocal(m_rA).subLocal(m_groundAnchorA);
m_uB.set(cB).addLocal(m_rB).subLocal(m_groundAnchorB);
float lengthA = m_uA.length();
float lengthB = m_uB.length();
if (lengthA > 10f * JBoxSettings.linearSlop) {
m_uA.mulLocal(1.0f / lengthA);
} else {
m_uA.setZero();
}
if (lengthB > 10f * JBoxSettings.linearSlop) {
m_uB.mulLocal(1.0f / lengthB);
} else {
m_uB.setZero();
}
// Compute effective mass.
float ruA = Vec2.cross(m_rA, m_uA);
float ruB = Vec2.cross(m_rB, m_uB);
float mA = m_invMassA + m_invIA * ruA * ruA;
float mB = m_invMassB + m_invIB * ruB * ruB;
m_mass = mA + m_ratio * m_ratio * mB;
if (m_mass > 0.0f) {
m_mass = 1.0f / m_mass;
}
if (data.step.warmStarting) {
// Scale impulses to support variable time steps.
m_impulse *= data.step.dtRatio;
// Warm starting.
final Vec2 PA = pool.popVec2();
final Vec2 PB = pool.popVec2();
PA.set(m_uA).mulLocal(-m_impulse);
PB.set(m_uB).mulLocal(-m_ratio * m_impulse);
vA.x += m_invMassA * PA.x;
vA.y += m_invMassA * PA.y;
wA += m_invIA * Vec2.cross(m_rA, PA);
vB.x += m_invMassB * PB.x;
vB.y += m_invMassB * PB.y;
wB += m_invIB * Vec2.cross(m_rB, PB);
pool.pushVec2(2);
} else {
m_impulse = 0.0f;
}
// 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);
pool.pushRot(2);
}
use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.
the class PulleyJoint method getLength1.
public float getLength1() {
final Vec2 p = pool.popVec2();
m_bodyA.getWorldPointToOut(m_localAnchorA, p);
p.subLocal(m_groundAnchorA);
float len = p.length();
pool.pushVec2(1);
return len;
}
use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.
the class PulleyJoint 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;
final Vec2 vpA = pool.popVec2();
final Vec2 vpB = pool.popVec2();
final Vec2 PA = pool.popVec2();
final Vec2 PB = pool.popVec2();
Vec2.crossToOutUnsafe(wA, m_rA, vpA);
vpA.addLocal(vA);
Vec2.crossToOutUnsafe(wB, m_rB, vpB);
vpB.addLocal(vB);
float Cdot = -Vec2.dot(m_uA, vpA) - m_ratio * Vec2.dot(m_uB, vpB);
float impulse = -m_mass * Cdot;
m_impulse += impulse;
PA.set(m_uA).mulLocal(-impulse);
PB.set(m_uB).mulLocal(-m_ratio * impulse);
vA.x += m_invMassA * PA.x;
vA.y += m_invMassA * PA.y;
wA += m_invIA * Vec2.cross(m_rA, PA);
vB.x += m_invMassB * PB.x;
vB.y += m_invMassB * PB.y;
wB += m_invIB * Vec2.cross(m_rB, PB);
// 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(4);
}
use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.
the class RevoluteJoint method initVelocityConstraints.
@Override
public void initVelocityConstraints(final SolverData data) {
m_indexA = m_bodyA.m_islandIndex;
m_indexB = m_bodyB.m_islandIndex;
m_localCenterA.set(m_bodyA.m_sweep.localCenter);
m_localCenterB.set(m_bodyB.m_sweep.localCenter);
m_invMassA = m_bodyA.m_invMass;
m_invMassB = m_bodyB.m_invMass;
m_invIA = m_bodyA.m_invI;
m_invIB = m_bodyB.m_invI;
// Vec2 cA = data.positions[m_indexA].c;
float aA = data.positions[m_indexA].a;
Vec2 vA = data.velocities[m_indexA].v;
float wA = data.velocities[m_indexA].w;
// Vec2 cB = data.positions[m_indexB].c;
float aB = data.positions[m_indexB].a;
Vec2 vB = data.velocities[m_indexB].v;
float wB = data.velocities[m_indexB].w;
final Rotation qA = pool.popRot();
final Rotation qB = pool.popRot();
final Vec2 temp = pool.popVec2();
qA.set(aA);
qB.set(aB);
// Compute the effective masses.
Rotation.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), m_rA);
Rotation.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), m_rB);
// J = [-I -r1_skew I r2_skew]
// [ 0 -1 0 1]
// r_skew = [-ry; rx]
// Matlab
// K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
// [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
// [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]
float mA = m_invMassA, mB = m_invMassB;
float iA = m_invIA, iB = m_invIB;
boolean fixedRotation = iA + iB == 0.0f;
m_mass.ex.x = mA + mB + m_rA.y * m_rA.y * iA + m_rB.y * m_rB.y * iB;
m_mass.ey.x = -m_rA.y * m_rA.x * iA - m_rB.y * m_rB.x * iB;
m_mass.ez.x = -m_rA.y * iA - m_rB.y * iB;
m_mass.ex.y = m_mass.ey.x;
m_mass.ey.y = mA + mB + m_rA.x * m_rA.x * iA + m_rB.x * m_rB.x * iB;
m_mass.ez.y = m_rA.x * iA + m_rB.x * iB;
m_mass.ex.z = m_mass.ez.x;
m_mass.ey.z = m_mass.ez.y;
m_mass.ez.z = iA + iB;
m_motorMass = iA + iB;
if (m_motorMass > 0.0f) {
m_motorMass = 1.0f / m_motorMass;
}
if (!m_enableMotor || fixedRotation) {
m_motorImpulse = 0.0f;
}
if (m_enableLimit && !fixedRotation) {
float jointAngle = aB - aA - m_referenceAngle;
if (FXGLMath.abs(m_upperAngle - m_lowerAngle) < 2.0f * JBoxSettings.angularSlop) {
m_limitState = LimitState.EQUAL;
} else if (jointAngle <= m_lowerAngle) {
if (m_limitState != LimitState.AT_LOWER) {
m_impulse.z = 0.0f;
}
m_limitState = LimitState.AT_LOWER;
} else if (jointAngle >= m_upperAngle) {
if (m_limitState != LimitState.AT_UPPER) {
m_impulse.z = 0.0f;
}
m_limitState = LimitState.AT_UPPER;
} else {
m_limitState = LimitState.INACTIVE;
m_impulse.z = 0.0f;
}
} else {
m_limitState = LimitState.INACTIVE;
}
if (data.step.warmStarting) {
final Vec2 P = pool.popVec2();
// Scale impulses to support a variable time step.
m_impulse.x *= data.step.dtRatio;
m_impulse.y *= data.step.dtRatio;
m_motorImpulse *= data.step.dtRatio;
P.x = m_impulse.x;
P.y = m_impulse.y;
vA.x -= mA * P.x;
vA.y -= mA * P.y;
wA -= iA * (Vec2.cross(m_rA, P) + m_motorImpulse + m_impulse.z);
vB.x += mB * P.x;
vB.y += mB * P.y;
wB += iB * (Vec2.cross(m_rB, P) + m_motorImpulse + m_impulse.z);
pool.pushVec2(1);
} else {
m_impulse.setZero();
m_motorImpulse = 0.0f;
}
// 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);
pool.pushRot(2);
}
Aggregations