Skip to content

Commit

Permalink
[libgdx] Physics mass -> massInverse. Timeline is not inverted.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Sweet committed Nov 8, 2023
1 parent 043a8ea commit 3541d00
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2303,7 +2303,7 @@ public void apply (Skeleton skeleton, float lastTime, float time, @Null Array<Ev
}
}

/** Changes a physics constraint's {@link PhysicsConstraint#getMass()}. */
/** Changes a physics constraint's {@link PhysicsConstraint#getMassInverse()}. The timeline values are not inverted. */
static public class PhysicsConstraintMassTimeline extends PhysicsConstraintTimeline {
public PhysicsConstraintMassTimeline (int frameCount, int bezierCount, int physicsConstraintIndex) {
super(frameCount, bezierCount, physicsConstraintIndex, Property.physicsConstraintMass);
Expand All @@ -2313,7 +2313,10 @@ public void apply (Skeleton skeleton, float lastTime, float time, @Null Array<Ev
MixDirection direction) {

PhysicsConstraint constraint = skeleton.physicsConstraints.get(constraintIndex);
if (constraint.active) constraint.mass = getAbsoluteValue(time, alpha, blend, constraint.mass, constraint.data.mass);
if (constraint.active) {
constraint.massInverse = 1
/ getAbsoluteValue(time, alpha, blend, 1 / constraint.massInverse, 1 / constraint.data.massInverse);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
public class PhysicsConstraint implements Updatable {
final PhysicsConstraintData data;
public Bone bone;
float inertia, strength, damping, mass, wind, gravity, mix;
float inertia, strength, damping, massInverse, wind, gravity, mix;

boolean reset = true;
float ux, uy, cx, cy, tx, ty;
Expand All @@ -62,7 +62,7 @@ public PhysicsConstraint (PhysicsConstraintData data, Skeleton skeleton) {
inertia = data.inertia;
strength = data.strength;
damping = data.damping;
mass = data.mass;
massInverse = data.massInverse;
wind = data.wind;
gravity = data.gravity;
mix = data.mix;
Expand All @@ -77,7 +77,7 @@ public PhysicsConstraint (PhysicsConstraint constraint) {
inertia = constraint.inertia;
strength = constraint.strength;
damping = constraint.damping;
mass = constraint.mass;
massInverse = constraint.massInverse;
wind = constraint.wind;
gravity = constraint.gravity;
mix = constraint.mix;
Expand All @@ -103,7 +103,7 @@ public void setToSetupPose () {
inertia = data.inertia;
strength = data.strength;
damping = data.damping;
mass = data.mass;
massInverse = data.massInverse;
wind = data.wind;
gravity = data.gravity;
mix = data.mix;
Expand Down Expand Up @@ -134,7 +134,7 @@ public void update (Physics physics) {
ux = bx;
uy = by;
} else {
float remaining = this.remaining, i = this.inertia, step = data.step;
float remaining = this.remaining, i = inertia, step = data.step;
if (x || y) {
if (x) {
xOffset += (ux - bx) * i;
Expand All @@ -145,15 +145,16 @@ public void update (Physics physics) {
uy = by;
}
if (remaining >= step) {
float m = this.mass * step, e = this.strength, w = wind * 100, g = gravity * -100;
float d = (float)Math.pow(this.damping, 60 * step);
float m = massInverse * step, e = strength, w = wind * 100, g = gravity * -100;
float d = (float)Math.pow(damping, 60 * step);
do {
if (x) {
xVelocity += (w - xOffset * e) * m;
xOffset += xVelocity * step;
xVelocity *= d;
}
if (y) {
System.out.println(massInverse);
yVelocity += (g - yOffset * e) * m;
yOffset += yVelocity * step;
yVelocity *= d;
Expand All @@ -180,8 +181,8 @@ public void update (Physics physics) {
}
remaining = this.remaining;
if (remaining >= step) {
float m = this.mass * step, e = this.strength, w = wind, g = gravity;
float d = (float)Math.pow(this.damping, 60 * step);
float m = massInverse * step, e = strength, w = wind, g = gravity;
float d = (float)Math.pow(damping, 60 * step);
while (true) {
remaining -= step;
if (scaleX) {
Expand Down Expand Up @@ -281,13 +282,12 @@ public void setDamping (float damping) {
this.damping = damping;
}

/** The inverse of the mass. */
public float getMass () {
return mass;
public float getMassInverse () {
return massInverse;
}

public void setMass (float mass) {
this.mass = mass;
public void setMassInverse (float massInverse) {
this.massInverse = massInverse;
}

public float getWind () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
public class PhysicsConstraintData extends ConstraintData {
BoneData bone;
boolean x, y, rotate, scaleX, shearX;
float step, inertia, strength, damping, mass, wind, gravity, mix;
float step, inertia, strength, damping, massInverse, wind, gravity, mix;

public PhysicsConstraintData (String name) {
super(name);
Expand Down Expand Up @@ -122,13 +122,12 @@ public void setDamping (float damping) {
this.damping = damping;
}

/** The inverse of the mass. */
public float getMass () {
return mass;
public float getMassInverse () {
return massInverse;
}

public void setMass (float mass) {
this.mass = mass;
public void setMassInverse (float massInverse) {
this.massInverse = massInverse;
}

public float getWind () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public SkeletonData readSkeletonData (InputStream dataInput) {
data.inertia = input.readFloat();
data.strength = input.readFloat();
data.damping = input.readFloat();
data.mass = input.readFloat();
data.massInverse = input.readFloat();
data.wind = input.readFloat();
data.gravity = input.readFloat();
data.mix = input.readFloat();
Expand Down

0 comments on commit 3541d00

Please sign in to comment.