Skip to content

Commit

Permalink
Fix Particle opacity calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
nightm4re94 committed Sep 15, 2023
1 parent 6b59039 commit 413bc2e
Showing 1 changed file with 23 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,8 @@ public abstract class Particle implements ITimeToLive {
/**
* Constructs a new particle.
*
* @param width
* the particle width in pixels
* @param height
* the particle height in pixels
* @param width the particle width in pixels
* @param height the particle height in pixels
*/
public Particle(final float width, final float height) {
this.setWidth(width);
Expand All @@ -97,13 +95,12 @@ public long getAliveTime() {
/**
* Gets the current bounding box of the particle, depending on its spawn location.
*
* @param origin
* the spawn location of this particle
* @param origin the spawn location of this particle
* @return The Rectangular particle bounding box.
*/
public Rectangle2D getBoundingBox(final Point2D origin) {
return new Rectangle2D.Double(origin.getX() + this.getX(), origin.getY() + this.getY(),
this.getWidth(), this.getHeight());
this.getWidth(), this.getHeight());
}

public Collision getCollisionType() {
Expand Down Expand Up @@ -159,27 +156,26 @@ public boolean isAntiAliased() {
}

public float getOpacity() {
if (this.isFading() && this.getTimeToLive() > 0) {
return MathUtilities.clamp(
this.getColor().getAlpha() / 255f - (float) this.getAliveTime() / this.getTimeToLive(), 0,
1);
if (isFading() && getTimeToLive() > 0) {
float maxAlpha = getColor().getAlpha() / 255f;
float progress = (float) getAliveTime() / getTimeToLive();
return MathUtilities.clamp(maxAlpha - progress * maxAlpha, 0, 1);
}
return 1;
}

/**
* Gets the location relative to the specified effect location.
*
* @param effectLocation
* the effect position
* @param effectLocation the effect position
* @return the location
*/
public Point2D getRenderLocation(Point2D effectLocation) {
// if we have a camera, we need to render the particle relative to the
// viewport
Point2D newEffectLocation =
Game.screens() != null ? Game.world().camera().getViewportLocation(effectLocation)
: effectLocation;
Game.screens() != null ? Game.world().camera().getViewportLocation(effectLocation)
: effectLocation;
return this.getAbsoluteLocation(newEffectLocation);
}

Expand Down Expand Up @@ -228,11 +224,11 @@ public Particle setCollisionType(final Collision collisionType) {
}

/**
* Enabling this check can be very performance hungry and should be used with caution and only for a small amount of
* particles.
* Enabling this check can be very performance hungry and should be used with caution and only for
* a small amount of particles.
*
* @param ccd
* If set to true, the collision will be checked continuously by a ray-cast approximation.
* @param ccd If set to true, the collision will be checked continuously by a ray-cast
* approximation.
* @return This particle instance.
*/
public Particle setContinuousCollision(boolean ccd) {
Expand Down Expand Up @@ -377,12 +373,11 @@ public boolean timeToLiveReached() {
}

/**
* Updates the effect's position, change in xCurrent, change in yCurrent, remaining lifetime, and color.
* Updates the effect's position, change in xCurrent, change in yCurrent, remaining lifetime, and
* color.
*
* @param emitterOrigin
* The current {@link Emitter} origin
* @param updateRatio
* The update ratio for this particle.
* @param emitterOrigin The current {@link Emitter} origin
* @param updateRatio The update ratio for this particle.
*/
public void update(final Point2D emitterOrigin, final float updateRatio) {
if (this.aliveTick == 0) {
Expand Down Expand Up @@ -427,10 +422,8 @@ public void update(final Point2D emitterOrigin, final float updateRatio) {
/**
* Test for ray cast collisions
*
* @param emitterOrigin
* The current {@link Emitter} origin
* @param updateRatio
* The update ratio for this particle.
* @param emitterOrigin The current {@link Emitter} origin
* @param updateRatio The update ratio for this particle.
* @return True if ray cast collision occurs
*/
protected boolean hasRayCastCollision(final Point2D emitterOrigin, final float updateRatio) {
Expand Down Expand Up @@ -467,12 +460,12 @@ private boolean checkForCollision(final Point2D emitterOrigin, float targetX, fl
double endY = emitterOrigin.getY() + targetY;
Line2D ray = new Line2D.Double(start.getX(), start.getY(), endX, endY);
if (this.getCollisionType() != Collision.NONE && Game.physics() != null && Game.physics()
.collides(ray, this.getCollisionType())) {
.collides(ray, this.getCollisionType())) {
collide();
return true;
}
} else if (this.getCollisionType() != Collision.NONE && Game.physics() != null && Game.physics()
.collides(this.getBoundingBox(emitterOrigin).getBounds2D(), this.getCollisionType())) {
.collides(this.getBoundingBox(emitterOrigin).getBounds2D(), this.getCollisionType())) {
collide();
return true;
}
Expand Down

0 comments on commit 413bc2e

Please sign in to comment.