Class Particle

All Implemented Interfaces:
ITimeToLive
Direct Known Subclasses:
ShapeParticle, SpriteParticle, TextParticle

public abstract class Particle extends Object implements ITimeToLive

Base class for particles spawned by a Emitter. A particle has a position, velocity, acceleration, lifetime, color and rendering options.

Subclasses are responsible for implementing [Point2D)][#render(Graphics2D,] which draws the particle relative to the emitter origin.

  • Constructor Details

    • Particle

      protected Particle(float width, float height)
      Constructs a new particle.
      Parameters:
      width - the particle width in pixels
      height - the particle height in pixels
  • Method Details

    • getAliveTime

      public long getAliveTime()
      Description copied from interface: ITimeToLive
      Gets the time this instance is alive.
      Specified by:
      getAliveTime in interface ITimeToLive
      Returns:
      Returns how long this instance is alive.
    • getBoundingBox

      public Rectangle2D getBoundingBox(Point2D origin)
      Gets the current bounding box of the particle, depending on its spawn location.
      Parameters:
      origin - the spawn location of this particle
      Returns:
      The Rectangular particle bounding box.
    • getCollisionType

      public Collision getCollisionType()
      Gets the collision behavior of this particle.
      Returns:
      the collision behavior
    • getColor

      public Color getColor()
      Gets the current color of the particle.
      Returns:
      the color
    • getDeltaHeight

      public float getDeltaHeight()
      Gets the per-update height delta applied to the particle.
      Returns:
      the height delta
    • getDeltaWidth

      public float getDeltaWidth()
      Gets the per-update width delta applied to the particle.
      Returns:
      the width delta
    • getVelocityX

      public float getVelocityX()
      Gets the horizontal velocity of the particle.
      Returns:
      the horizontal velocity
    • getVelocityY

      public float getVelocityY()
      Gets the vertical velocity of the particle.
      Returns:
      the vertical velocity
    • getAccelerationX

      public float getAccelerationX()
      Gets the horizontal acceleration of the particle.
      Returns:
      the horizontal acceleration
    • getAccelerationY

      public float getAccelerationY()
      Gets the vertical acceleration of the particle.
      Returns:
      the vertical acceleration
    • getAngle

      public float getAngle()
      Gets the current rotation angle of the particle, in degrees.
      Returns:
      the current angle
    • getDeltaAngle

      public float getDeltaAngle()
      Gets the per-update angle delta applied to the particle, in degrees.
      Returns:
      the angle delta
    • getHeight

      public float getHeight()
      Gets the current height of the particle, in pixels.
      Returns:
      the particle height
    • getOutlineThickness

      public float getOutlineThickness()
      Gets the outline thickness used when rendering the particle as an outline.
      Returns:
      the outline thickness
    • isOutlineOnly

      public boolean isOutlineOnly()
      Returns whether the particle is rendered as an outline only.
      Returns:
      true if outline-only rendering is enabled
    • isAntiAliased

      public boolean isAntiAliased()
      Returns whether the particle is rendered with anti-aliasing.
      Returns:
      true if anti-aliasing is enabled
    • getOpacity

      public float getOpacity()
      Computes the current opacity of the particle. If fading is enabled and the particle has a finite time-to-live, the opacity decreases linearly from the color's initial alpha towards zero as the particle ages.
      Returns:
      the opacity in the range [0, 1]
    • getRenderLocation

      public Point2D getRenderLocation(Point2D effectLocation)
      Gets the location relative to the specified effect location.
      Parameters:
      effectLocation - the effect position
      Returns:
      the location
    • getCustomRenderType

      public RenderType getCustomRenderType()
      Gets the custom render type that overrides the emitter's render type, if usesCustomRenderType() is true.
      Returns:
      the custom render type
    • getTimeToLive

      public int getTimeToLive()
      Description copied from interface: ITimeToLive
      Gets the total time to live of this instance.
      Specified by:
      getTimeToLive in interface ITimeToLive
      Returns:
      The total time to live.
    • getWidth

      public float getWidth()
      Gets the current width of the particle, in pixels.
      Returns:
      the particle width
    • getX

      public float getX()
      Gets the X position of the particle relative to its emitter origin.
      Returns:
      the X position
    • getY

      public float getY()
      Gets the Y position of the particle relative to its emitter origin.
      Returns:
      the Y position
    • isFading

      public boolean isFading()
      Returns whether the particle fades out over its lifetime.
      Returns:
      true if fading is enabled
    • isFadingOnCollision

      public boolean isFadingOnCollision()
      Returns whether the particle fades upon collision.
      Returns:
      true if fade-on-collision is enabled
    • isContinuousCollisionEnabled

      public boolean isContinuousCollisionEnabled()
      Returns whether continuous (ray-cast) collision detection is enabled for this particle.
      Returns:
      true if continuous collision detection is enabled
    • isStoppingOnCollision

      public boolean isStoppingOnCollision()
      Returns whether the particle stops moving once a collision occurs.
      Returns:
      true if the particle stops on collision
    • render

      public abstract void render(Graphics2D g, Point2D emitterOrigin)
      Renders this particle to the given graphics context.
      Parameters:
      g - the graphics context to draw to
      emitterOrigin - the world location of the owning emitter
    • setCollisionType

      public Particle setCollisionType(Collision collisionType)
      Sets the collision behavior of this particle.
      Parameters:
      collisionType - the new collision behavior
      Returns:
      this particle instance for chaining
    • setContinuousCollision

      public Particle setContinuousCollision(boolean ccd)
      Enabling this check can be very performance hungry and should be used with caution and only for a small amount of particles.
      Parameters:
      ccd - If set to true, the collision will be checked continuously by a ray-cast approximation.
      Returns:
      This particle instance.
    • setStopOnCollision

      public Particle setStopOnCollision(boolean stopOnCollision)
      Sets whether the particle stops moving when it collides.
      Parameters:
      stopOnCollision - true to stop the particle on collision
      Returns:
      this particle instance for chaining
    • setColor

      public Particle setColor(Color color)
      Sets the color of the particle. null values are ignored.
      Parameters:
      color - the new color
      Returns:
      this particle instance for chaining
    • setDeltaHeight

      public Particle setDeltaHeight(float deltaHeight)
      Sets the per-update height delta applied to the particle.
      Parameters:
      deltaHeight - the height delta
      Returns:
      this particle instance for chaining
    • setAccelerationX

      public Particle setAccelerationX(float accelerationX)
      Sets the horizontal acceleration of the particle.
      Parameters:
      accelerationX - the horizontal acceleration
      Returns:
      this particle instance for chaining
    • setAccelerationY

      public Particle setAccelerationY(float accelerationY)
      Sets the vertical acceleration of the particle.
      Parameters:
      accelerationY - the vertical acceleration
      Returns:
      this particle instance for chaining
    • setAngle

      public Particle setAngle(float angle)
      Sets the current rotation angle of the particle, in degrees.
      Parameters:
      angle - the angle to set
      Returns:
      this particle instance for chaining
    • setDeltaAngle

      public Particle setDeltaAngle(float deltaAngle)
      Sets the per-update angle delta applied to the particle, in degrees.
      Parameters:
      deltaAngle - the angle delta
      Returns:
      this particle instance for chaining
    • setDeltaWidth

      public Particle setDeltaWidth(float deltaWidth)
      Sets the per-update width delta applied to the particle.
      Parameters:
      deltaWidth - the width delta
      Returns:
      this particle instance for chaining
    • setVelocityX

      public Particle setVelocityX(float velocityX)
      Sets the horizontal velocity of the particle.
      Parameters:
      velocityX - the horizontal velocity
      Returns:
      this particle instance for chaining
    • setVelocityY

      public Particle setVelocityY(float velocityY)
      Sets the vertical velocity of the particle.
      Parameters:
      velocityY - the vertical velocity
      Returns:
      this particle instance for chaining
    • setFade

      public Particle setFade(boolean fade)
      Sets whether the particle fades out over its lifetime.
      Parameters:
      fade - true to enable fading
      Returns:
      this particle instance for chaining
    • setFadeOnCollision

      public Particle setFadeOnCollision(boolean fadeOnCollision)
      Sets whether the particle fades upon collision.
      Parameters:
      fadeOnCollision - true to enable fade-on-collision
      Returns:
      this particle instance for chaining
    • setHeight

      public Particle setHeight(float height)
      Sets the height of the particle, in pixels.
      Parameters:
      height - the height to set
      Returns:
      this particle instance for chaining
    • setOutlineThickness

      public Particle setOutlineThickness(float outlineThickness)
      Sets the outline thickness used when rendering the particle as an outline.
      Parameters:
      outlineThickness - the outline thickness
      Returns:
      this particle instance for chaining
    • setOutlineOnly

      public Particle setOutlineOnly(boolean outlineOnly)
      Sets whether the particle is rendered as an outline only.
      Parameters:
      outlineOnly - true to enable outline-only rendering
      Returns:
      this particle instance for chaining
    • setAntiAliasing

      public Particle setAntiAliasing(boolean antiAliasing)
      Sets whether the particle is rendered with anti-aliasing.
      Parameters:
      antiAliasing - true to enable anti-aliasing
      Returns:
      this particle instance for chaining
    • setCustomRenderType

      public Particle setCustomRenderType(RenderType renderType)
      Sets a custom render type that overrides the emitter's render type for this particle.
      Parameters:
      renderType - the render type to use
      Returns:
      this particle instance for chaining
    • setWidth

      public Particle setWidth(float width)
      Sets the width of the particle, in pixels.
      Parameters:
      width - the width to set
      Returns:
      this particle instance for chaining
    • setX

      public Particle setX(float x)
      Sets the X position of the particle relative to its emitter origin.
      Parameters:
      x - the X position
      Returns:
      this particle instance for chaining
    • setY

      public Particle setY(float y)
      Sets the Y position of the particle relative to its emitter origin.
      Parameters:
      y - the Y position
      Returns:
      this particle instance for chaining
    • setTimeToLive

      public Particle setTimeToLive(int ttl)
      Sets the time-to-live (TTL) of the particle, in milliseconds.
      Parameters:
      ttl - the TTL to set
      Returns:
      this particle instance for chaining
    • init

      public Particle init(EmitterAttributes data)
      Initializes this particle's mutable state from the supplied EmitterAttributes.
      Parameters:
      data - the emitter data providing initial parameter values
      Returns:
      this particle instance for chaining
    • timeToLiveReached

      public boolean timeToLiveReached()
      Description copied from interface: ITimeToLive
      Determines whether this instance has exceeded its time to live.
      Specified by:
      timeToLiveReached in interface ITimeToLive
      Returns:
      True if the time to live was reached; otherwise false.
    • update

      public void update(Point2D emitterOrigin, float updateRatio)
      Updates the effect's position, change in xCurrent, change in yCurrent, remaining lifetime, and color.
      Parameters:
      emitterOrigin - The current Emitter origin
      updateRatio - The update ratio for this particle.
    • hasRayCastCollision

      protected boolean hasRayCastCollision(Point2D emitterOrigin, float updateRatio)
      Test for ray cast collisions
      Parameters:
      emitterOrigin - The current Emitter origin
      updateRatio - The update ratio for this particle.
      Returns:
      True if ray cast collision occurs
    • getAbsoluteLocation

      public Point2D getAbsoluteLocation(Point2D effectLocation)
      Computes the absolute world location of this particle given an emitter origin.
      Parameters:
      effectLocation - the emitter origin
      Returns:
      the absolute world location of the particle
    • getAbsoluteX

      protected float getAbsoluteX(Point2D emitterOrigin)
      Computes the absolute world X coordinate of this particle, accounting for the particle's width.
      Parameters:
      emitterOrigin - the emitter origin
      Returns:
      the absolute X coordinate
    • getAbsoluteY

      protected float getAbsoluteY(Point2D emitterOrigin)
      Computes the absolute world Y coordinate of this particle, accounting for the particle's height.
      Parameters:
      emitterOrigin - the emitter origin
      Returns:
      the absolute Y coordinate
    • usesCustomRenderType

      public boolean usesCustomRenderType()
      Returns whether this particle has a custom render type that overrides the emitter's render type.
      Returns:
      true if a custom render type is set