Skip to content

Commit

Permalink
fix issue on disturbance friction processing
Browse files Browse the repository at this point in the history
  • Loading branch information
mcgivrer committed May 5, 2024
1 parent a4ac85c commit 7e324b1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
18 changes: 14 additions & 4 deletions src/main/java/my/karma/app/KarmaPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,10 @@ public void resetForces() {
public <T> T getAttributeOrDefault(String key, T defaultValue) {
return (T) attributes.getOrDefault(key, defaultValue);
}

public Collection<? extends Vector2D> getForces() {
return forces;
}
}

/**
Expand Down Expand Up @@ -1671,7 +1675,9 @@ public void update(double d, Map<String, Object> stats) {

public void updateEntity(double d, Entity e) {
// if concerned, apply World disturbances.
applyWorldDisturbance(world, e, d);
if (e.getPhysicType() != PhysicType.STATIC) {
applyWorldDisturbance(world, e, d);
}

// detect collision and apply response
detectCollision(world, e, d);
Expand Down Expand Up @@ -1745,10 +1751,14 @@ private void applyPhysics(World world, Entity entity, double d) {
*/
private void applyWorldDisturbance(World world, Entity entity, double d) {
for (Disturbance dist : world.disturbances) {
dist.updateBox();
if (dist.box.intersects(entity.box) || dist.box.contains(entity.box)) {
// TODO add forces and acceleration to Entity.
entity.forces.addAll(dist.forces);
//entity.velocity = entity.velocity.multiply(dist.getMaterial().friction);
// Apply disturbance forces on the entity.
entity.forces.addAll(dist.getForces());
// Apply friction to the velocity of the GameObject.
if (Optional.ofNullable(dist.getMaterial()).isPresent()) {
entity.velocity = entity.velocity.multiply(dist.getMaterial().friction);
}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/my/karma/app/scenes/PlayScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public void create(KarmaPlatform app) {
.setPosition(0, 0)
.setSize(w.getPlayArea().getWidth(), w.getPlayArea().getHeight() * 0.8)
.addForce(new KarmaPlatform.Vector2D(0.0002, 0.0))
.setMaterial(KarmaPlatform.Material.DEFAULT));
.setMaterial(null));
w.addDisturbance((KarmaPlatform.Disturbance)
new KarmaPlatform.Disturbance("mag")
.setPosition(0, 0)
.setSize(w.getPlayArea().getWidth() * 0.15, w.getPlayArea().getHeight())
.setForegroundColor(new Color(0.7f, 0.6f, 0.0f, 0.5f))
.setBackgroundColor(new Color(0.7f, 0.6f, 0.0f, 0.5f))
.addForce(new KarmaPlatform.Vector2D(-0.002, -0.012))
.setMaterial(KarmaPlatform.Material.DEFAULT)
.setMaterial(null)
);
createPlatforms(app);

Expand All @@ -55,9 +55,9 @@ public void create(KarmaPlatform app) {
.setMaterial(
new KarmaPlatform.Material(
"water",
0.90, 0.6, 0.2))
0.97, 1.0, 0.6))
.setPosition(16, (w.getPlayArea().getHeight() * 0.75))
.setSize(w.getPlayArea().getWidth() - 32, (w.getPlayArea().getHeight() * 0.25) - 32)
.setSize(w.getPlayArea().getWidth() - 64, (w.getPlayArea().getHeight() * 0.25) - 32)
.setForegroundColor(new Color(0.0f, 0.0f, 0.8f, 0.5f))
.setBackgroundColor(new Color(0.0f, 0.0f, 0.6f, 0.5f))
.addBehavior(new WaterSimulationBehavior(0.0125, 0.25, getWorld()));
Expand Down

0 comments on commit 7e324b1

Please sign in to comment.