MV Particle Engine

A while ago, i started creating a wrapper for Proton (http://www.a-jie.cn/proton/), that can be used in MV. I've come to a workable solution, that can be accessed here:

http://pastebin.com/LgtBbkjN

For a simple demo, do the following steps:

- Get Proton from its download page (http://www.a-jie.cn/proton/build/proton-1.0.0.min.js), rename the file to "proton.js" (or anything, that doesn't contain dots in the filename) and add it as a plugin.

- Get the plugin from my pastebin and add it to your game. Leave the plugin parameter as-is, for now.

- Add the attached image to your "img" folder. It will be used to draw particles.

- During the game, paste this in the console and execute it:

var layer = IAVRA.PROTON.getLayer('default');layer.blendMode = 1;SceneManager._scene.addChild(layer); emitter = new Proton.Emitter();emitter.rate = new Proton.Rate(new Proton.Span(10, 15), .1);emitter.addInitialize(new Proton.Mass(1));emitter.addInitialize(new IAVRA.PROTON.Target('img/particle.png'));emitter.addInitialize(new Proton.Position(new Proton.PointZone(Graphics.width / 2, Graphics.height / 2)));emitter.addInitialize(new Proton.Life(1, 1.5));emitter.addInitialize(new Proton.V(new Proton.Span(1, 2), new Proton.Span(0, 360), 'polar'));emitter.addBehaviour(new Proton.Color('#C97024', '#290000'));emitter.addBehaviour(new Proton.Scale(0, Proton.getSpan(5, 10)));emitter.setLayer('default');emitter.emit();IAVRA.PROTON.addEmitter(emitter);If everything went right, you should now see an animated "fireball" above your scene, like this:


This approach has a couple of disadvantages:

- Proton updates emitters based on real time, so you can't pause them in any way (as far as i know).

- All emitters are updated by the same Proton instance, since the instance accesses global objects. In theory, it's possible to create multiple Proton instances, but everytime you do so, it would reset the Proton.pool, which holds all particle instances.

- We can't (easily) use PIXI.SpriteBatch, which should be faster than PIXI.DisplayObjectContainer, since it ignores the "tint" property (and possible others) of the sprites contained in it.

I posted this in "Development", since this is basically as far as i've come and i'd like to hear your opinions and suggestions for implementing or creating a particle engine for MV.

The last solution for me would be to reimplement Proton (unmodified version here: http://www.a-jie.cn/proton/build/proton-1.0.0.js) for MV, which would certainly take a good amount of time, but gives me the option to optimize it for MV/PIXI.

particle.png
This looks promising, Iavra. This can come in many uses, but with this types of plugins, would you be releasing a standalone property so we don't have to get the js file from proton anymore? Also, just a suggestion, which you can consider or not, a way to dissect the properties so we can install a plugin that only uses the fireball, the firefly, etc.
There is no "fireball" class or similar. All effects are created using emitters, which in turn create and update particles. The animation you see in my video is actually created by ~150 sprites animated in a certain way.


If i were to only write the wrapper, i would let you download Proton separately, so you can have the latest version. If i'm actually going to rewrite the engine, i would obviously publish the whole thing.
Where is the image?
Under "Attached Thumbnails". It's white, so you can't see it against the forum background, but it's there ^^
Oh. Is the Proton.js code suppose to look like a bomb went off?
If you mean this (http://www.a-jie.cn/proton/build/proton-1.0.0.min.js), that is the minified version of the script, which strips whitespaces and shortens variable names to save space. This (http://www.a-jie.cn/proton/build/proton-1.0.0.js) is the unminified script. Both are functionally identical.
I'm about to do something cool with this. I'll see you in an hour.

Just pretend that the video is not lagging. Working with some ancient technology here.
I'm currently in the process of rewriting Proton to fit better into the MV/PIXI environment, so i'll reactivate this thread. There isn't much to see right now, so i will only give a short overview:


Changes from Proton:


- Emitters are subclasses of PIXI.DisplayObjectContainer, which means they also serve as container for the created particles and can be added directly to the scene.


- Rendering and eventing is done by PIXI, so i dropped the Proton implementations.


Completed steps:


- Wrote the base Emitter and Particle prototypes, as well as all Initializers.


- I can currently display static particles in a given Zone (point, line, rect, circle. ImageZone is currently missing). A short demo with a few thousand particles ran stable at 60 fps (and this pc is fairly slow with on-board graphics).


To be done:


- Particle pooling (keep a certain number of Sprites in memory and reuse them, instead of creating new ones. Increases performance, but also memory usage) via plugin parameter.


- Blend Modes


- Particle behaviours. This includes (i probably miss some):


-- color (tinting)


-- scale


-- rotation


-- movement, including attraction and repulsion from/to certain points (this includes gravity)


-- collision (i'll do this one last)


-- transparency


- Emitter behaviours.


- Eventing interfaces to register callbacks (easy, PIXI provides everything needed).


I would use PIXI.SpriteBatch or RenderTexture, since both should be faster, than rendering each particle on its own, but they are limited (only 1 layer, no rotation/transparency/tinting/...).


As you can see, there's a lot to do, so i can't give any fixed date.


Additionally, i will use my easing library (http://pastebin.com/Y6kMyksm) as an optional dependency, since you might not need all those functions (and i don't feel like copying them all over...). The plugin itself will only support linear easing.
Do you know if there's already a way to have a behavior execute after another behavior has expired?


For example, a particle that grows for a few seconds, then shrinks.
Proton by default can't do that, but in my version you can define a delay for the behaviour in addition to the life parameter. This allows you to make the second behaviour's delay as long as the first one's life and have them seemingly chain together (i also fixed a bug in Proton, that made behaviour age increasing per particle affected).


Additionally (and this works in Proton, too), you can define a custom behaviour, like in this example: http://www.a-jie.cn/proton/example/zone/imagezone/imagezone.html


function customScaleBehaviour() {
return {
initialize : function(particle) {
particle.oldRadius = particle.radius;
particle.scale = 0;
},
applyBehaviour : function(particle) {
if (particle.energy >= 2 / 3) {
particle.scale = (1 - particle.energy) * 3;
} else if (particle.energy <= 1 / 3) {
particle.scale = particle.energy * 3;
}
particle.radius = particle.oldRadius * particle.scale;
}
}
}


This will continue to work in my engine (though the functions have different names), but i will also add API calls to create custom initializers/behaviours.
I know this is technically a double post, but probably nobody else will post in here if nothing changes and there isn't much of a point to hold back the plugin for 2 days.


I've completed porting over Proton to MV in the first beta version 0.1. There are some features missing (like the "cross" crossType for zones), but those are minor and i might include them at some point. I ended up rewriting a lot more, than i had planned because of bugs, performance and changes needed for the PIXI/MV integration:


http://pastebin.com/zmV51U1v


The documentation is currently a bit lacking, but i've commented on every class and function as much as i could, so it shouldn't be too hard to find out, what everything does.


I've attached an example particle image (although it's attached to the OP as well). Below is an example, that creates the fireball demo from Proton:


var emitter = new P.Emitter()
.setBlendMode(1)
.setRate(new P.Rate(new P.Span(10, 15), 6))
.addInitializers(
new P.Image('img/particle.png'),
new P.Position(new P.PointZone(Graphics.width / 2, Graphics.height / 2)),
new P.Life(60, 90),
new P.Velocity(new P.Span(1, 2), new P.Span(0, 360), 'polar')
).addBehaviours(
new P.Color('#C97024', '#290000'),
new P.Scale(0, new P.Span(5, 10))
).start();


The emitter can be added as a child to the scene, like every other sprite or window you create.


As you can see, the engine is fairly complex and i probably won't manage to find every bug in time, so i'm happy about everyone who gives this a try and reports bugs (or feature requests) back to me.


/edit: I finished the documentation and fixed a bug, that caused behaviour energy to not be calculated correctly, when life and/or delay were set. There also was a bug with particles not being fully reset before putting back into the pool.


On a sidenote: The plugin has now surpassed 3000 lines.


//edit: Changed the way behaviour life, delay and easing are handled.


Old syntax:


new P.Color(color1, color2, life, delay, easing);


New syntax:


new P.Color(color1, color2).limit(life, delay).easing(easing);


It's longer, but doesn't bloat constructors as much as before and keeps a clear line between behaviour parameters and inherited properties.


///edit: The GravityWell behaviour got fixed in version 0.4 and now works similar to this one: https://github.com/richardlord/Flint/blob/master/src/org/flintparticles/twoD/actions/GravityWell.as


////edit: Simple eventing has been added to the engine. For example, to register a listener, that gets called before every emitter update, use this:


emitter.addEventListener(P.EVENT.UPDATE_BEFORE, new function(emitter) { /* do stuff */ });


particle.png
Here's a different approach, which doesn't use initializers or behaviours, but let's the user inherit from the particle class:


http://pastebin.com/ss7T05b0


Example usage:


var Particle = function() { IAVRA.PARTICLE.Particle.call(this); };
Particle.prototype = Object.create(IAVRA.PARTICLE.Particle.prototype);

Particle.prototype.setup = function(emitter) {
this.texture = IAVRA.PARTICLE.Util.imagePathToTexture('img/particle.png');
this.position.set(Graphics.width / 2, Graphics.height / 2);
this.life = 200;
};

Particle.prototype.update = function(emitter, index) {
this.velocity.x += IAVRA.PARTICLE.Util.random(-1, 1);
this.velocity.y += IAVRA.PARTICLE.Util.random(-1, 1);
};

var Counter = {
start: function(emitter) { return 0; },
update: function(emitter) { return 1; }
};

SceneManager._scene.addChild(new IAVRA.PARTICLE.Emitter(Particle, Counter).start());


This gives more freedom to the user, but comes out with less options out of the box. It also makes the plugin much shorter (~400 lines compared to 3000).


Which option do you prefer?


/edit: Personally, i prefer this one, since it uses up less space in its "bare" version, while giving more flexibility, because it's not constricted to the initializer/behaviour pattern. Also, it's rather easy to write a plugin defining a Particle subclass, that takes behaviour and initializer objects, ending up exactly the same.
So basically, instead of using proton's provided initializers and behaviors which I would consider to be an interface of sorts, you would just set up the particle directly yourself and define all of those properties manually instead of using what proton offers.


I'm curious whether there would be a need to proton at that point, since it seems you've basically written your own particle engine.
The basic concepts of a particle engine are pretty simple, since you only need 3 things:


- A particle class


- An emitter


- The particle system itself (i shared this between the emitter and MV/PIXI)


Beyond that, there are multiple ways to setup emitters:


- Proton registers initializers/behaviours on the particles and executes them inside.


- Flint registers initializers/behaviours on the emitter and treats particles as pure data holders.


- My current approach uses the particle class (not the particle itself) to handle updates.


I'm currently writing 2 addons for the engine:


- A small counter library, that can be used to create counters.


- An initializer/behaviour framework, coming with all predefined behaviours, that Proton has (and a few more) and can be used on top of the core engine.


Though, the second one might turn out as its own engine, if the performance impact is too noticable.


/edit: I'll write my behaviours in a way, that allows them to be executed from both the emitter and the particle. By writing a small adapter for the simple engine, they can be used in there.
I like the idea of using behaviors and initializers, because it's blackboxing all the details and allows me to use use external tools easily.


At a high-level, I know what it means for a sprite to scale from 0% to 100% over a period of time. However, it would be nice if I could simply say that directly, instead of having to implement the scaling logic myself (cause it might not be as performant as someone else's implementation)


I don't have any particular preference for libraries or approaches: whichever works. Since your engine is an MV-first approach, that would be preferable.
Yeah, i'm currently modifying the core engine to be more flexible and accepts both behaviours and custom code. It basically looks like this:


var emitter = new IAVRA.PARTICLE.Emitter()
.setCounter({
start: function(emitter) {},
update: function(emitter) { return 1; }
})
.addBehaviour({
setup: function(emitter, particle) {
particle.texture = PIXI.Texture.fromImage('img/particle.png');
particle.position.set(Graphics.width / 2, Graphics.height / 2);
particle.life = 200;
},
update: function(emitter, particle, index) {
if(++particle.age > particle.life) { particle.dead = true; }
particle.velocity.x += Math.random() - 0.5;
particle.velocity.y += Math.random() - 0.5;
}
});


There are a few characteristics to note:


- There are no "Initializers", anymore. A behaviour can implement both "setup" and "update" methods and will be called accordingly. I'm pondering about adding a third possible function, that gets called each update after new particles have been creates, but before they are looped. Flint calls this "activities" and it can be used to update a behaviour's state independent from individual particles.


- You need to manually age the particle and mark it as dead. This is experimental, since automatically aging particles wastes performance, if you don't want them to die, anyway. Flint even goes a step farther and adds a "Move" behaviour, that handles velocity, but that went to far for me.


The core plugin contains the classes Particle, Factory, Emitter, Counter, Behaviour and Zone (not used in the core, but will be used by several behaviours). Counters, behaviours and zones each get their own plugin (for now), because the core plugin can work on its own.
What about specifying multiple behaviors as a sequence?


For example, scale from 0% to 100% for over 60 frames, then scale from 100% to 0% over another 60 frames, and loop.
The first batch of behaviours will deal with full life-time transitions, only. Afterwards, i'll think about a generic way to make limited (behaviour life/delay) or looped. In the meantime, feel free to create your own behaviours. I certainly won't be able to support everything out of the box, but the engine is designed to be open for that very reason.

Original thread: https://forums.rpgmakerweb.com/threads/mv-particle-engine.54336/