Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
Real-time 2D particle systems (with gravitation!)
Page 1: What is a particle system, particle, system, gravitation?
 More of this Feature
• Page 2: Sample projects

• ParticleUnit.pas
 Join the Discussion
"Post your questions, concerns, views and comments to this article..."
Discuss!
 Related Resources
• Game programming
• Graphics programming
• Mathematics in Delphi

Article submitted by: Curtis W. Socha

   What is a particle system?

Prerequisites
.Delphi 4 or newer.
.Understanding how to work with the canvas
.Understanding of TList
.DirectX 4.0 or later if you wish to view the DirectDraw example.
To run the examples
.Download ParticleUnit.pas from this document.
.When you download the examples later on, be sure to include this unit in your search path.
This tutorial is going to discuss a library of routines I have put together that will help you create your own particle systems. The term system is defined to mean, "A group of interacting, interrelated, or interdependent elements forming a complex whole". Since we are obviously dealing with particles, and we have a library of methods (elements) that work together to manipulate these particles, we thus have a particle system.

OK, that makes sense, but what is a particle?
When I refer to the term particle, I am referring to an XY coordinates in space that has unique properties. Each particle is treated as an individual entity with it’s own characteristics. These days, we are all familiar with OOP so you should think of a particle as an object. It will have speed, mass, direction, and an XY coordinate position in two-dimensional space. This small group of characteristics can produce some wild special effects. Keep in mind that you do not have to necessarily put a single pixel on the screen to represent a particle, as I will in these examples. You can use that XY coordinate to display any image at that location. Let us refer to Example 1.0 now to see an illustration of a simple particle traveling through 2D space. Another name for this, according to mathematics, is a vector.

Particle system

Hmmm. So what could a particle system be used for?
Well, for starters, they make for great trajectory games. You can use the particle to represent a cannonball being shot out of a cannon, rotating star fields, explosions, molecular effects, sparks, the list goes on. That said, I would like to mention that most of all, they are fun as all heck to play with. I have yet to run out of ideas using this library.

You mentioned gravitation. How does this impact the particle system?
As I mentioned, particles can have mass. Since mass in the real universe naturally gravitates to each other, I thought it would be really cool to make my particles do this as well. To make things even more interesting, I created gravity wells. They are essentially black holes that you can place anywhere in your 2D universe. Particles will attract not only each other, but they will be attracted to the gravity well (or repelled!). If you wish, you can alter the gravity well so that it repulses particles instead of attracting them.

Well then, lets get started. Teach me!
Great. Let us get on with it. The first few things I must explain is how I have the library organized. The library has a TParticleClass that contains lists of all active particles and gravity wells. This class also contains the methods you will use to manipulate your particle universe. The next thing to mention is the TParticle record. This record contains information about a specific particle. Things like mass, force, angle, whether it has gravitation or not, and it’s coordinates. Last but not least, we have the TGravityWell record. This record contains information about the gravity well, things such as location, size, and whether it should act as land gravity (I.E. the land under your feet is considered land gravity whereas the sun is considered point gravity). In a nutshell, you basically have a TParticleClass that contains a TList of TParticle and TGravityWell.

I will now provide the source code for the particle system library and several examples on how to use it. Click to download ParticleUnit.pas.

So the library is a single unit. How do I use this thing?
Good question. To help you along the windy path, we are going to review an extremely simple application to demonstrate just how easy it is to create particle systems using this library. All you need to do to add the library is add it to your uses clause.

Next page > Sample projects > Page 1, 2

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC.
· NEXT ARTICLE: Windows Shell Extensions – Info Tip.
Creating the InfoTip Shell Extension, which lets us, control the information that appears in Explorer when the mouse hovers over a file. Creating a Delphi Infotip that will display the FileName, the project type (Program or Library), the Project Name (from the source file), and the size of the file in bytes.
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?

Explore Delphi Programming

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.