Game Development Community

Particles' Collisions in TX2D: Nice and neat :)

by Giuseppe De Francesco · in Torque X 2D · 06/13/2010 (5:02 am) · 18 replies

I needed a flamethrower... no luck to get it with the engine as it is so... I rushed some code into it to get what I need, here's the very first unit test:



This has been added to the SVN repository (Commit Revision 28) ;)

- Particles' Collisions
- Ability to kill the particle on collision so to stop them when an obstacle is hit (see video)

The Unit Test shown in the video can be downloaded from LIVE here.

How to implement the collisions:

  1. First of all recompile the game (with the engine!) because we need to update the schema (also the engine schema!)
  2. open your level
  3. define at least one level object type to identify what will collide with the particles
  4. select the particle effect you want to use and add the component T2DParticleCollisionComponent
  5. in the T2DParticleCollisionComponent write the name of the emitter that will generate the colliding particles, declare the target object type, check the Enabled checkbox (and also the KillOnCollision if you want to stop the particles when hit a target) and see if you want to have the Flip-Flop enabled.
  6. In Visual Studio create a helper component to assign the SceneObject.Collision.OnParticleCollision delegate (or create the static delegate in your game events class if you want to avoid the helper component)
  7. Select in the editor your target objects and add them the target object type and the helper component (or populate the OnParticleCollision in the Collision component if you are using a game event class).

Note on the Flip-Flop flag: to ease the CPU job if this flag is enabled the collisions are processed every 2 rendered particles. This is great if the particles are not too fast. In case you have fast particles and you notice that some collision is missed then disable the Flip-Flop flag.

The FrameInterval property is a more strong way to ease the CPU load: while the Flip-Flop skips half the live particles on each frame you can choose to skip them all, processing collision every <n> frames. this is good for slow particles which will be on the same location for many frames, still be aware that if a fast object goes through the particles within that interval then no collisions will be detected: this feature is to be used wisely.

Obviously put some code in the OnParticleCollision delegate if you want to do something on collision (besides killing the particle).

That's all folks!

About the author

In the software eng. field since 1981, in charge of R&D during last 10 years. IEEE Senior Member (and volunteer).


#1
06/13/2010 (6:21 am)
That looks cool!
#2
06/13/2010 (11:17 am)
I've just updated the OP video to reflect the current state of this feature: I consider it now a Beta, well working, just needing some tweaks. There are no unwanted effects on the game's frame rate so it's quite good so far.
#3
06/13/2010 (11:55 am)
Very interesting, this is something I wanted for some time :)

Now, how hard it would be to implement particle phisics?
(Like a fire that is pushed or blocked when an object is on its way)

I'm not suggesting you implement it, just curious because it was also something I wanted to do some time ago :)
#4
06/13/2010 (12:17 pm)
@Diego: Actually I'm already working on that as well because it's the natural evolution of this feature ;) It won't take a lot to have a good looking implementation of basic phisics behaviour so you won't wait long to get it :)
#5
06/13/2010 (4:44 pm)
Hey Guys,

I've released this one and updated the OP video. What's in this release:

- Particles' Collisions
- Ability to kill the particle on collision so to stop them when an obstacle is hit (see video)

The Unit Test shown in the video can be downloaded from LIVE here.

How to implement the collisions:

  1. First of all recompile the game (with the engine!) because we need to update the schema (also the engine schema!)
  2. open your level
  3. define at least one level object type to identify what will collide with the particles
  4. select the particle effect you want to use and add the component T2DParticleCollisionComponent
  5. in the T2DParticleCollisionComponent write the name of the emitter that will generate the colliding particles, declare the target object type, check the Enabled checkbox (and also the KillOnCollision if you want to stop the particles when hit a target)
  6. In Visual Studio create a helper component to assign the SceneObject.Collision.OnParticleCollision delegate (or create the static delegate in your game events class if you want to avoid the helper component)
  7. Select in the editor your target objects and add them the target object type and the helper component (or populate the OnParticleCollision in the Collision component if you are using a game event class).

Obviously put some code in the OnParticleCollision delegate if you want to do something on collision (besides killing the particle).

That's all folks!

This is a bit basic but it does the job... it's enough for a one day work... tomorrow I'll think about how to make it better also using your kind feedback ;)

Enjoy!
Pino
#6
06/13/2010 (4:55 pm)
I'm interested in this, using the particles themselves for collision, rather then a blank scene object, could have great results. Should be a really cool resource.
#7
06/13/2010 (5:04 pm)
@Will: I hope so :) Give it a try and let me know your thoughts!
#8
06/14/2010 (5:31 am)
Wow, that's pretty dang impressive.
#9
06/15/2010 (5:26 am)
Some update: I've tweaked the position computing (Commit Revision 29) and tested the implementation in a laser weapon. In this video the laser looks bad because of YouTube format frame rate... sorry for that :)



I'm going to commit some changes to allow you to decide if to use or not the detection Flip-Flop because as you can see in the video (with Flip-Flop) sometimes the collision is missed because the particle speed here is very high and its surface very small. You can leave the Flip-Flop for flamethrowers, gas leaks, fire ans so on, but better to disable it for very fast particles.
#10
06/15/2010 (8:04 am)
SVN Commit Revision 30: Flip-Flop flag:

To ease the CPU job if this flag is enabled the collisions are processed every 2 rendered particles. This is great if the particles are not too fast. In case you have fast particles and you notice that some collision is missed then disable the Flip-Flop flag.
#11
06/15/2010 (8:42 am)
I had a particle weapon in my last game but removed it because we did not have particle collision. So only the "head" did damage and we thought that was frustrating. we wanted it to slice like a knife. This is cool.
#12
06/15/2010 (9:56 am)
@Henry: I'm glad you appreciate this small contribution ;)

I've produced another video with a different weapon, a more slow one to have it well rendered on YouTube. This has the Flip-Flop enabled because the particles are not so fast and as you can see no shot is missed :)

#13
06/15/2010 (3:12 pm)
First off, thanks, it's great!

Now, 2 ideas, if you will:
1: How about an integer value for the number of frame to skip (or every each to test, whichever?)
2: A way to spread the collisions on various frames, especially useful if you implement 1. Aka, you have 10000 particles and test every 10 frames, you could test 1000 particles each frame.

Thanks again, your work is as always, immensely useful!
#14
06/15/2010 (4:22 pm)
Hey Olivier,

#1 is nice, #2 is risky and very CPU consuming because the pool is parsed sequentially, so if I stop checking after 1000 particles I've to store a pointers' list to know what to skip next frame and also manage the dead pointers... it will consume way more CPU than just parsing them all :)

The #1 as a counter or skipper is not very efficient but I'll implement that as a modulus divisor such as

if ((frameCounter++ % yourDivisor) == 0)
perform collision check...

so that is quite simple to decide the frame interval for checking the collisions.

I'll do that first thing tomorrow ;)
#15
06/20/2010 (2:47 am)
Sorry for being late but I've been busy.

I've just commited the suggested improvement to T2DParticleCollisionComponent to implement a frames skipping in collision detection.

It's the Commit Revision 31.

Pino

Edit: OP updated with usage notes.
#16
08/02/2010 (3:57 pm)
2D Particle Collision update: just added the Alpha Edge parameter (integer 0-255). This allows you to define the minimum Alpha value needed to generate a collision. If a particle has a value below this value no collision will be generated.
#17
08/02/2010 (4:54 pm)
Looks like the revision fixed the memory leak issue I was having in the setTexture method. I think I'm code 3- free now.

Thanks!
#18
08/02/2010 (5:20 pm)
Thanks, wrong thread though :)