Development:Particle engine: Difference between revisions

From The Mana World
Crush (talk | contribs)
No edit summary
 
Crush (talk | contribs)
No edit summary
Line 10: Line 10:


A particle engine would allow us to create a large variety of beautiful special effects with minimal effort that would outclass those of most other sprite based rpgs. The areas where particle effects could be used include but are not limited to:
A particle engine would allow us to create a large variety of beautiful special effects with minimal effort that would outclass those of most other sprite based rpgs. The areas where particle effects could be used include but are not limited to:
* Enviromental effects like waterfalls, fountains or smoke comming out of chimneys
* Environmental effects like waterfalls, fountains or smoke comming out of chimneys
* Visualisation of mystical phenomenons like teleporters or other points of interest
* Visualisation of mystical phenomenons like teleporters or other points of interest
* Graphic effects for spells
* Graphic effects for spells
* Visualisation of status effects
* Visualisation of status effects


==Implementation proposal==
==Implementation==
My idea would be to create two new classes named "ParticleEmitter" and "Particle". Both have a position in three dimensional space (in this case the two dimensional game world is threatet as a flat plane in a three dimensional space). The "PaticleEmitter"s purpose is to create new "Particle"s. I would suggest to implement the "ParticleEmitter" as a new class and the "Particle"s as a derived class from "Sprite" so that they can be added to the maps sprite list to be drawn together with the other sprites by the "map" class. But the logic of both should be called by the "engine" class because the particles do not interfere with the game mechanics.
The basic element of the particle engine is the '''ParticleEngine''' class. It maintains a tree of particle effects. Each particle has a position in three dimensional space (in this case the two dimensional game world is threatet as a flat plane in a three dimensional space). Based on the type of particle it might or might not have:
* Movement based on physical properties
* A visual representation (a single image or an animation)
* A list of emitters that create additional particles.


==Particle Emitter==
The Particles are derived class from the "Sprite" class so that they can be added to the maps sprite list to be drawn together with the other sprites by the "map" class.  
The creation of particle emitters is either triggered when a map is loaded that contains particle emitters for esthetical purpose or by events like spellcasting. An emitter can have a large number of properties. Most of these are basically the initial properties of the created particles. I would suggest to predefine the properties of the emitter types in external xml files. These are the properties of the emitter:


;layer: When the [[Action Layers]] concept is realized it would be good to define the map layer on which the particles are drawn.
=== Types of particles ===
;posX, posY, posZ: The coordinates in 3 dimensional space where the newly created particles are spawned.
==== Static particles ====
;particleAppearance: Either the filename of an xml file that contains the animation definition for animated particles or the file name of a png file for not animated particles.
Static particles are spawned on a specific map position and usually stay there. Unually used for invisible emitters that create animated effects on the map.
;particleLifetime: Lifetime of the particles.
==== Independent particles ====
;particleAmmount: Number of particles that are created every game tick.
Particles that have a movement vector and move around without a specific target. Usually spawned by other particles.
;angelHorizontal, angelVertical, power: Initiall vector of the created sprites.
==== targetingPoint particles ====
;accelerationAngelHorizontal, accelerationAngelVertical, accelerationPower: Acceleration vector of the particles. The particles vector is multiplied by this vector after every game tick. Useful for creating particles that obey gravity, slow down or accelerate after they are created.
Particles that move to a specific map position.
;emitterLifetime: The time in game ticks the emitter exists (useful for event triggered special effects)
==== targetingBeing particles ====
Particles that follow or move to a specific being (like players or monsters).


All properties have the attribute "value" and 4 optional attributes "diffusion", "delta", "frequency" and "amplitude" that define the change of the value. When "frequency" or "amplitude" is defined, the other one must be defined, too.
=== Ways to create an effect ===
;diffusion: is the range in which the value fluctuates randomly.
To create an effect we need 3 informations:
;delta: is how much the value changes with every game tick. This allows to create emitters whichs properties change over time.
;amplitude: Defines the amplitude of a sine wave that is added to the "value". Allows to set values that change over time, but not linear like the delta property but in a sine rythm.
;frequency: Defines the frequency of a sine wave that is added to the "value" (unit: ticks per oscilation).


'''''TODO:''' member var description, method description, where and when they are called, description of data structure in which they are stored, implementation details.''*
*Animation file
*Location of the effect (a being or a map location)
*Destination of created particles (empty for independent particles, a being or a map location)


In contrast to an implementation proposal i made earlier i decided to abstain from an option to create particle emitters that explicitely follow a being. Instead i would like to provide an interface for beings to create emitters and move them to their current position themself.
hat means that there are 6 different ways to create an effect.


==Particle==
== The effect definition file ==
These are the member variables of the particle class:
The effect definition files are xml documents describing one or more single particles or particle emitters. They can theoretically be loaded for all 6 ways to create effects.


;Animation animation: Animation class that controls the animation of the particle (NULL for unanimated particles).
=== <effect> ===
;Image* image: Pointer to the image class for unanimated particles (NULL for animated particles).
Is the root element of the XML document.
;float posX, posY, posZ: The coordinates in 3 dimensional space of the particle.
;float vectorX, vectorY, vectorZ: The movement vector of the sprites. For faster calculation already splitted up in its components.
;float accelX, accelY, accelZ: Acceleration vector of the sprites. For faster calculation already splitted up in its components.
;int lifetime: A counter for the remaining lifetime of the particle. It is decremented during every update. When it reaches 0 the dead flag is raised.
;bool dead: a flag that signalizes the engine that the particle should be removed.


'''''TODO:''' method description, where and when they are called, description of data structure in which they are stored, implementation details.''
=== <particle> ===
Every effect is based on at least one particle. Often but not always these root particles function only as emitters that spawn new particles and have no appearance of their own. They have the following three required properties:
;type: physical properties of the particle
;positionX: (initial) position on map relative to the position where the effect is created (pixel-based)
;positionY: (initial) position on map relative to the position where the effect is created (pixel-based)
;positionZ: (initial) position on map relative to the position where the effect is created (pixel-based)


==Necessary extensions of the map format==
The following properties are optional:
Mappers should be able to define special effects that are placed statically on their maps.
:;lifetime: Lifetime in game-ticks. (default: unlimited)
:;image: path to the image that should be drawn at the position of the particle (default: no image)
:;behavior: Either ''following'' or ''independent''. Determines if the effect should head toward the target if one has been defined or not. (default: ''independent'')
:;acceleration: acceleration towards the target if one is defined in pixels/game-tick² (default: 0.0f)
:;momentum: Momentum of the particle if a target is defined. Before adding the acceleration the old vectors are multiplied by this. A momentum of 0 would make the acceleration a constant speed, a momentum of 1 a perfect newtonian object. (default: 0.0f)
:;die-distance: Distance in pixels to the target that causes the destruction of the particle when reached if a target is defined. (Default: 0.0f = not destroyed on contact)


'''''TODO:'''Explain another reason why Bjørn should add a feature to Tiled and the .tmx format that allows to place user defined special objects on the map''
In addition they can have any number of emitters
 
=== <emitter> ===
Emitters are always child-elements of a particle and create new particles themself which can also include emitters and so on. Their only (and required) property is the type of particle they create. They also include a number of <property> child elements that define the initial properties of the created particles. The property elements have two or three properties. Either ''name'' and ''value'' or ''name'', ''min'' and ''max''.
 
They can also have one or more <emitter> childtags. When these exist all child-particles will be equipped with these emitters.
 
 
'''required:'''
:;positionX: (initial) position on map relative to the parent particle (pixel-based)
:;positionY: (initial) position on map relative to the parent particle (pixel-based)
:;positionZ: (initial) position on map relative to the parent particle (pixel-based)
 
'''optional:'''
:;image: path to the image that should be drawn at the position of the particles (default: no image)
:;behavior: Either ''targeting'' or ''independent''. Determines if the created particles should head toward the target if one has been defined or not. (default: ''independent'')
:;horizontal-angle: clockwise angle of initial vector on horizontal plane in degree. 0° means straight right. (default: 0°)
:;vertical-angle: angle of initial vector in degree (default: 0°)
:;power: Initial speed of the particles in pixels/ game-tick (default: 0.0f)
:;gravity: Downward acceleration of particles in pixels/game-tick² (default: 0.01f)
:;randomnes: Random changes in the X, Y and Z vector. The unit is maximum pixels/game-tick/1000.
:;lifetime: lifetime in game ticks (default: unlimited)
 
'''optional for ''targeting'' particles:'''
:;acceleration: acceleration of particles towards the target in pixels/game-tick² (default: 0.0f)
:;momentum: Momentum of the particles. Before adding the acceleration the old vectors are multiplied by this. A momentum of 0 would make the acceleration a constant speed, a momentum of 1 a perfect newtonian object. (default: 0.0f)
:;die-distance: Distance in pixels to the target that causes the destruction of the particles when reached. (Default: 0.0f = not destroyed on contact)

Revision as of 15:39, 9 January 2007

This idea isn't really new. I already mentioned this topic a while ago but i decided to wait with the implementation until the new animation system is working so that it can be used for animated particles. To avoid any discussions about implementation details after the code is written i decided to document the whole system beforehand.

This article isn't finished. That is why it is in my personal directory. When i finished the article i will move it to Particle engine and link it from the front page in the RFC section.

What is a particle engine?

A particle engine is a system that allows to create beautiful dynamic special effects based on creating and managing a large number of small, independent, moving sprites. Flames, smoke, fountains and waterfalls are just some examples what can be done with such an engine. For more information about particle engines see the Wikipedia article about particle systems.

Why a particle engine?

A particle engine would allow us to create a large variety of beautiful special effects with minimal effort that would outclass those of most other sprite based rpgs. The areas where particle effects could be used include but are not limited to:

  • Environmental effects like waterfalls, fountains or smoke comming out of chimneys
  • Visualisation of mystical phenomenons like teleporters or other points of interest
  • Graphic effects for spells
  • Visualisation of status effects

Implementation

The basic element of the particle engine is the ParticleEngine class. It maintains a tree of particle effects. Each particle has a position in three dimensional space (in this case the two dimensional game world is threatet as a flat plane in a three dimensional space). Based on the type of particle it might or might not have:

  • Movement based on physical properties
  • A visual representation (a single image or an animation)
  • A list of emitters that create additional particles.

The Particles are derived class from the "Sprite" class so that they can be added to the maps sprite list to be drawn together with the other sprites by the "map" class.

Types of particles

Static particles

Static particles are spawned on a specific map position and usually stay there. Unually used for invisible emitters that create animated effects on the map.

Independent particles

Particles that have a movement vector and move around without a specific target. Usually spawned by other particles.

targetingPoint particles

Particles that move to a specific map position.

targetingBeing particles

Particles that follow or move to a specific being (like players or monsters).

Ways to create an effect

To create an effect we need 3 informations:

  • Animation file
  • Location of the effect (a being or a map location)
  • Destination of created particles (empty for independent particles, a being or a map location)

hat means that there are 6 different ways to create an effect.

The effect definition file

The effect definition files are xml documents describing one or more single particles or particle emitters. They can theoretically be loaded for all 6 ways to create effects.

<effect>

Is the root element of the XML document.

<particle>

Every effect is based on at least one particle. Often but not always these root particles function only as emitters that spawn new particles and have no appearance of their own. They have the following three required properties:

type
physical properties of the particle
positionX
(initial) position on map relative to the position where the effect is created (pixel-based)
positionY
(initial) position on map relative to the position where the effect is created (pixel-based)
positionZ
(initial) position on map relative to the position where the effect is created (pixel-based)

The following properties are optional:

lifetime
Lifetime in game-ticks. (default: unlimited)
image
path to the image that should be drawn at the position of the particle (default: no image)
behavior
Either following or independent. Determines if the effect should head toward the target if one has been defined or not. (default: independent)
acceleration
acceleration towards the target if one is defined in pixels/game-tick² (default: 0.0f)
momentum
Momentum of the particle if a target is defined. Before adding the acceleration the old vectors are multiplied by this. A momentum of 0 would make the acceleration a constant speed, a momentum of 1 a perfect newtonian object. (default: 0.0f)
die-distance
Distance in pixels to the target that causes the destruction of the particle when reached if a target is defined. (Default: 0.0f = not destroyed on contact)

In addition they can have any number of emitters

<emitter>

Emitters are always child-elements of a particle and create new particles themself which can also include emitters and so on. Their only (and required) property is the type of particle they create. They also include a number of <property> child elements that define the initial properties of the created particles. The property elements have two or three properties. Either name and value or name, min and max.

They can also have one or more <emitter> childtags. When these exist all child-particles will be equipped with these emitters.


required:

positionX
(initial) position on map relative to the parent particle (pixel-based)
positionY
(initial) position on map relative to the parent particle (pixel-based)
positionZ
(initial) position on map relative to the parent particle (pixel-based)

optional:

image
path to the image that should be drawn at the position of the particles (default: no image)
behavior
Either targeting or independent. Determines if the created particles should head toward the target if one has been defined or not. (default: independent)
horizontal-angle
clockwise angle of initial vector on horizontal plane in degree. 0° means straight right. (default: 0°)
vertical-angle
angle of initial vector in degree (default: 0°)
power
Initial speed of the particles in pixels/ game-tick (default: 0.0f)
gravity
Downward acceleration of particles in pixels/game-tick² (default: 0.01f)
randomnes
Random changes in the X, Y and Z vector. The unit is maximum pixels/game-tick/1000.
lifetime
lifetime in game ticks (default: unlimited)

optional for targeting particles:

acceleration
acceleration of particles towards the target in pixels/game-tick² (default: 0.0f)
momentum
Momentum of the particles. Before adding the acceleration the old vectors are multiplied by this. A momentum of 0 would make the acceleration a constant speed, a momentum of 1 a perfect newtonian object. (default: 0.0f)
die-distance
Distance in pixels to the target that causes the destruction of the particles when reached. (Default: 0.0f = not destroyed on contact)