Skip to content

Commit

Permalink
replaced "friction" with "velocityHalfLife"
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-mohr committed Feb 3, 2023
1 parent 3739013 commit 31bbd77
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/main/java/com/particle_life/Physics.java
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,18 @@ private int wrapContainerY(int cy) {
}
}

private double computeFrictionFactor(double halfLife, double dt) {
if (halfLife == 0) return 0.0; // avoid division by zero
if (halfLife == Double.POSITIVE_INFINITY) return 1.0;

return Math.pow(0.5, dt / halfLife);
}

private void updateVelocity(int i) {
Particle p = particles[i];

// apply friction before adding new velocity
p.velocity.mul(Math.pow(settings.friction, settings.dt));
p.velocity.mul(computeFrictionFactor(settings.velocityHalfLife, settings.dt));

int cx0 = (int) Math.floor((p.position.x + 1) / containerSize);
int cy0 = (int) Math.floor((p.position.y + 1) / containerSize);
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/particle_life/PhysicsSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ public class PhysicsSettings {
public double rmax = 0.04;

/**
* velocity will be multiplied with this factor every second
* The time in seconds after which half the velocity of a particle
* should be lost due to friction.
* The actual friction factor <code>f</code> that the velocity is
* multiplied with in every time step is calculated on the basis of
* this value according to the following formula:
* <code>f = Math.pow(0.5, dt / frictionHalfLife)</code>
*/
public double friction = 0.0000001f;
public double velocityHalfLife = 0.043;

/**
* Scaled force by an arbitrary factor.
Expand All @@ -36,7 +41,7 @@ public PhysicsSettings deepCopy() {

p.wrap = wrap;
p.rmax = rmax;
p.friction = friction;
p.velocityHalfLife = velocityHalfLife;
p.force = force;
p.dt = dt;
p.matrix = matrix.deepCopy();
Expand All @@ -50,7 +55,7 @@ public boolean equals(Object o) {

if (s.wrap != wrap) return false;
if (s.rmax != rmax) return false;
if (s.friction != friction) return false;
if (s.velocityHalfLife != velocityHalfLife) return false;
if (s.force != force) return false;
if (s.dt != dt) return false;
if (!s.matrix.equals(matrix)) return false;
Expand Down

0 comments on commit 31bbd77

Please sign in to comment.