Skip to content

Commit

Permalink
Don't evaluate collision if a particle should not be moved
Browse files Browse the repository at this point in the history
  • Loading branch information
steffen-wilke committed Dec 2, 2019
1 parent c114a89 commit c0c9322
Showing 1 changed file with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public abstract class Particle implements ITimeToLive {

private boolean continuousCollision;

private boolean stopOnCollision;

/**
* Constructs a new particle.
*
Expand All @@ -79,6 +81,8 @@ public Particle(final float width, final float height, final Color color, final
this.collisionType = Collision.NONE;
this.opacity = 1;
this.fade = true;
this.setStopOnCollision(true);
this.setContinuousCollision(false);
}

@Override
Expand Down Expand Up @@ -177,6 +181,10 @@ public boolean isContinuousCollisionEnabled() {
return this.continuousCollision;
}

public boolean isStoppingOnCollision() {
return this.stopOnCollision;
}

public abstract void render(final Graphics2D g, final Point2D emitterOrigin);

public Particle setCollisionType(final Collision collisionType) {
Expand All @@ -196,6 +204,10 @@ public Particle setContinuousCollision(boolean ccd) {
return this;
}

public void setStopOnCollision(boolean stopOnCollision) {
this.stopOnCollision = stopOnCollision;
}

public Particle setColor(final Color color) {
this.color = color;
return this;
Expand Down Expand Up @@ -316,10 +328,22 @@ public void update(final Point2D emitterOrigin, final float updateRatio) {
final int alpha = (int) (this.getOpacity() * this.getColorAlpha());
this.color = new Color(this.color.getRed(), this.color.getGreen(), this.color.getBlue(), alpha >= 0 ? alpha : 0);

if (this.getDeltaWidth() != 0) {
this.width += this.getDeltaWidth() * updateRatio;
}

if (this.getDeltaHeight() != 0) {
this.height += this.getDeltaHeight() * updateRatio;
}

// test for ray cast collision
final float targetX = this.x + this.getDx() * updateRatio;
final float targetY = this.y + this.getDy() * updateRatio;

if (targetX == this.x && targetY == this.y) {
return;
}

if (this.checkForCollision(emitterOrigin, targetX, targetY)) {
return;
}
Expand All @@ -339,17 +363,13 @@ public void update(final Point2D emitterOrigin, final float updateRatio) {
if (this.getGravityY() != 0) {
this.deltaY += this.getGravityY() * updateRatio;
}

if (this.getDeltaWidth() != 0) {
this.width += this.getDeltaWidth() * updateRatio;
}

if (this.getDeltaHeight() != 0) {
this.height += this.getDeltaHeight() * updateRatio;
}
}

private boolean checkForCollision(final Point2D emitterOrigin, float targetX, float targetY) {
if (this.isStoppingOnCollision() && this.colliding) {
return true;
}

if (this.isContinuousCollisionEnabled()) {
Point2D start = this.getAbsoluteLocation(emitterOrigin);
double endX = emitterOrigin.getX() + targetX - this.getWidth() / 2.0;
Expand Down

0 comments on commit c0c9322

Please sign in to comment.