[libgdx] Physics mass -> massInverse. Timeline is not inverted.

This commit is contained in:
Nathan Sweet 2023-11-08 14:15:49 -04:00
parent 043a8ea9ae
commit 3541d005a7
4 changed files with 25 additions and 23 deletions

View File

@ -2303,7 +2303,7 @@ public class Animation {
} }
} }
/** 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 { static public class PhysicsConstraintMassTimeline extends PhysicsConstraintTimeline {
public PhysicsConstraintMassTimeline (int frameCount, int bezierCount, int physicsConstraintIndex) { public PhysicsConstraintMassTimeline (int frameCount, int bezierCount, int physicsConstraintIndex) {
super(frameCount, bezierCount, physicsConstraintIndex, Property.physicsConstraintMass); super(frameCount, bezierCount, physicsConstraintIndex, Property.physicsConstraintMass);
@ -2313,7 +2313,10 @@ public class Animation {
MixDirection direction) { MixDirection direction) {
PhysicsConstraint constraint = skeleton.physicsConstraints.get(constraintIndex); 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);
}
} }
} }

View File

@ -39,7 +39,7 @@ import com.esotericsoftware.spine.Skeleton.Physics;
public class PhysicsConstraint implements Updatable { public class PhysicsConstraint implements Updatable {
final PhysicsConstraintData data; final PhysicsConstraintData data;
public Bone bone; public Bone bone;
float inertia, strength, damping, mass, wind, gravity, mix; float inertia, strength, damping, massInverse, wind, gravity, mix;
boolean reset = true; boolean reset = true;
float ux, uy, cx, cy, tx, ty; float ux, uy, cx, cy, tx, ty;
@ -62,7 +62,7 @@ public class PhysicsConstraint implements Updatable {
inertia = data.inertia; inertia = data.inertia;
strength = data.strength; strength = data.strength;
damping = data.damping; damping = data.damping;
mass = data.mass; massInverse = data.massInverse;
wind = data.wind; wind = data.wind;
gravity = data.gravity; gravity = data.gravity;
mix = data.mix; mix = data.mix;
@ -77,7 +77,7 @@ public class PhysicsConstraint implements Updatable {
inertia = constraint.inertia; inertia = constraint.inertia;
strength = constraint.strength; strength = constraint.strength;
damping = constraint.damping; damping = constraint.damping;
mass = constraint.mass; massInverse = constraint.massInverse;
wind = constraint.wind; wind = constraint.wind;
gravity = constraint.gravity; gravity = constraint.gravity;
mix = constraint.mix; mix = constraint.mix;
@ -103,7 +103,7 @@ public class PhysicsConstraint implements Updatable {
inertia = data.inertia; inertia = data.inertia;
strength = data.strength; strength = data.strength;
damping = data.damping; damping = data.damping;
mass = data.mass; massInverse = data.massInverse;
wind = data.wind; wind = data.wind;
gravity = data.gravity; gravity = data.gravity;
mix = data.mix; mix = data.mix;
@ -134,7 +134,7 @@ public class PhysicsConstraint implements Updatable {
ux = bx; ux = bx;
uy = by; uy = by;
} else { } 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 || y) {
if (x) { if (x) {
xOffset += (ux - bx) * i; xOffset += (ux - bx) * i;
@ -145,8 +145,8 @@ public class PhysicsConstraint implements Updatable {
uy = by; uy = by;
} }
if (remaining >= step) { if (remaining >= step) {
float m = this.mass * step, e = this.strength, w = wind * 100, g = gravity * -100; float m = massInverse * step, e = strength, w = wind * 100, g = gravity * -100;
float d = (float)Math.pow(this.damping, 60 * step); float d = (float)Math.pow(damping, 60 * step);
do { do {
if (x) { if (x) {
xVelocity += (w - xOffset * e) * m; xVelocity += (w - xOffset * e) * m;
@ -154,6 +154,7 @@ public class PhysicsConstraint implements Updatable {
xVelocity *= d; xVelocity *= d;
} }
if (y) { if (y) {
System.out.println(massInverse);
yVelocity += (g - yOffset * e) * m; yVelocity += (g - yOffset * e) * m;
yOffset += yVelocity * step; yOffset += yVelocity * step;
yVelocity *= d; yVelocity *= d;
@ -180,8 +181,8 @@ public class PhysicsConstraint implements Updatable {
} }
remaining = this.remaining; remaining = this.remaining;
if (remaining >= step) { if (remaining >= step) {
float m = this.mass * step, e = this.strength, w = wind, g = gravity; float m = massInverse * step, e = strength, w = wind, g = gravity;
float d = (float)Math.pow(this.damping, 60 * step); float d = (float)Math.pow(damping, 60 * step);
while (true) { while (true) {
remaining -= step; remaining -= step;
if (scaleX) { if (scaleX) {
@ -281,13 +282,12 @@ public class PhysicsConstraint implements Updatable {
this.damping = damping; this.damping = damping;
} }
/** The inverse of the mass. */ public float getMassInverse () {
public float getMass () { return massInverse;
return mass;
} }
public void setMass (float mass) { public void setMassInverse (float massInverse) {
this.mass = mass; this.massInverse = massInverse;
} }
public float getWind () { public float getWind () {

View File

@ -35,7 +35,7 @@ package com.esotericsoftware.spine;
public class PhysicsConstraintData extends ConstraintData { public class PhysicsConstraintData extends ConstraintData {
BoneData bone; BoneData bone;
boolean x, y, rotate, scaleX, shearX; 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) { public PhysicsConstraintData (String name) {
super(name); super(name);
@ -122,13 +122,12 @@ public class PhysicsConstraintData extends ConstraintData {
this.damping = damping; this.damping = damping;
} }
/** The inverse of the mass. */ public float getMassInverse () {
public float getMass () { return massInverse;
return mass;
} }
public void setMass (float mass) { public void setMassInverse (float massInverse) {
this.mass = mass; this.massInverse = massInverse;
} }
public float getWind () { public float getWind () {

View File

@ -320,7 +320,7 @@ public class SkeletonBinary extends SkeletonLoader {
data.inertia = input.readFloat(); data.inertia = input.readFloat();
data.strength = input.readFloat(); data.strength = input.readFloat();
data.damping = input.readFloat(); data.damping = input.readFloat();
data.mass = input.readFloat(); data.massInverse = input.readFloat();
data.wind = input.readFloat(); data.wind = input.readFloat();
data.gravity = input.readFloat(); data.gravity = input.readFloat();
data.mix = input.readFloat(); data.mix = input.readFloat();