|
|
This thread is locked; no one can reply to it.
|
1
2
|
| A Simple Particle Explosion |
|
yohosuff
Member #10,022
July 2008
|
I've been searching like a bloodhound through forum archives for some working source code of a particle explosion. Does anyone think they could point me towards a tutorial or just provide some code directly? I'll keep searching but I think human's understand what I'm looking for better than the search algorithm does. |
|
blargmob
Member #8,356
February 2007
|
Create a particle struct or class containing x, y, vel, dir, color. Array or vector containing the amount of particles you want in the explosion.\ When explosion happens For each particle give particle random dir give particle a vel After explosion happens For each particle update particles position (calculate xS and yS from vel and dir) --- |
|
amber
Member #6,783
January 2006
|
Blargmob's basic approach is good, though I'd recommend that you dispense with the array and just simply maintain a big list of "graphical objects" in your program, all of which can have individual velocity, acceleration, lifetimes, and so on. In my recent projects, having every sprite/etc. on the screen be considered a "Particle" and treated the same has simplified rendering and updating significantly, at the expense of slightly more memory and CPU usage (that is not an issue at all on a PC built this century). |
|
SiegeLord
Member #7,827
October 2006
|
If you use the hardware accelerated approach, blargmob's method is better, since you don't have to switch textures between each particle, leading to enormous speed benefits. You could look at the sources of these demos I guess. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
|
amber
Member #6,783
January 2006
|
That's assuming all of your particles in the system are the same texture, and your "big list" isn't optimized to provide similar benefits. In my case, neither of those assumptions are true, but I agree, mileage may vary. |
|
yohosuff
Member #10,022
July 2008
|
I appreciate your replies, but I have to admit that I don't know what you are all talking about. I tried the sources of the demos you provided a link to, but they are all way to complex for me to understand with my limited knowledge of this particular aspect of programming. Is it difficult to write the code for a simple explosion? I learn best from working code. I heard that there are simple examples somewhere, but I haven't been able to locate them. Thanks again for all your help so far. |
|
Onewing
Member #6,152
August 2005
|
Quote: I learn best from working code. I think you'd learn even better by trying. Write some thoughts down on how you think it would be done and then try to code it yourself. When you get stuck at this point, then come here and show your work. ------------ |
|
blargmob
Member #8,356
February 2007
|
Quote: I have to admit that I don't know what you are all talking about. First learn what we say. Then try to code it. --- |
|
yohosuff
Member #10,022
July 2008
|
Thanks for the advice, but I'm still going to go with learning by example. I believe the phrase goes something like, "Why reinvent the wheel?" I'd really appreciate it if someone would post something that I could learn from. I want to learn. I want to.........LEARN!!! :p |
|
Schyfis
Member #9,752
May 2008
|
Try something like this.
I didn't test it, but that should get you started. If you want images attached to your particles, you may not need oldx or oldy, they're just for drawing the line. ________________________________________________________________________________________________________ |
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
And a good way to learn is by doing... Blargmob gave you an algorithm you could implement in your very first reply. Translating it to code shouldn't be too difficult if you just try. Here's an example based on his algorithm (untested, uncompiled) :
And a quick example of usage :
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|
Timorg
Member #2,028
March 2002
|
Schyfis said:
(*p).x = (*p).oldx = x;
perhaps going p->x = p->oldx = x; would actually be clearer. Edit: Particles lend themselves to C++, with a pixel class, but I did my example in C. You could easily roll this up into a class, but I am in a C mood. (The code, binary and code::blocks project is attached.)
____________________________________________________________________________________________ |
|
Neil Walker
Member #210
April 2000
|
Schyfis said: malloc(sizeof(particle)); Why use malloc when you're using c++, why use doubles? you don't seem to have the ability to end the life of a particle, or group particles together as distinct units, or reuse the memory for new particles. Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
|
Schyfis
Member #9,752
May 2008
|
Quote: Why use malloc when you're using c++, why use doubles? you don't seem to have the ability to end the life of a particle, or group particles together as distinct units, or reuse the memory for new particles. Because it's a simple example, I didn't include anything for deleting particles, but you could easily give each particle a lifetime and do something like this:
What would you recommend for my example other than malloc? I'm still working with my first project that uses structures, so I'm still quite new to the various uses of malloc, new, free, and delete. You should be able to malloc or new a structure, right? ________________________________________________________________________________________________________ |
|
Timorg
Member #2,028
March 2002
|
The thing is new and delete are part of C++, that is memory allocation and freeing are part of the language, when in C (malloc and free), they are provided by the system library. When you use new it calls that class constructor if there is one, and delete calls the destructor. If new for some reason can't allocate the memory, the default action is to throw an exception, where malloc just returns a NULL pointer. You can use malloc to allocate an area in memory for a struct, but you can't really use it to create a class on the heap. (AFAIK you can use malloc, then manually call the constructor, then manually call the destructor before freeing it, but you are really asking for trouble if you go down that path.) Edit: ____________________________________________________________________________________________ |
|
Idealius
Member #1,619
November 2001
|
I think there's a little miscommunication in this thread... as to the skill of the original poster (Op). Not taking in account sound and keyboard input.. this is how I (self taught game dev) learned: 1-Create a program that draws a rectangle. I'm curious, which step is the OP at? :p |
|
Neil Black
Member #7,867
October 2006
|
What is the specific task the OP is having trouble with? Idealius: your example fails. It took me less than a day to learn how to bounce rectangles around on the screen. Ok, I used circles instead of rectangles, but it's the same thing. A particle explosion is much different.
|
|
Idealius
Member #1,619
November 2001
|
You're probably right as I learned on Qbasic long before Allegro ever existed.. I guess I'm just wondering if the OP has a basic understanding of arrays. Once you have that a particle explosion should be easy to create. Of course creating, tracking, and deleting it as an object is another layer of complexity.. kudos to circles =) |
|
Neil Black
Member #7,867
October 2006
|
I learned in QBASIC too! My biggest problem with a particle explosion was always the random direction thing. I could make things go up, down, left, right, or at a 45 degree angle to any of those, but it wasn't until just recently that I learned how to make things go in any direction. I still have trouble with particle effects, though.
|
|
Idealius
Member #1,619
November 2001
|
Oh cool. I guess I just assumed you started on Allegro. Yeah I think I bridged that gap by using float's instead of int's for the particle's coords and deltas. And of course knowing how to make a random value that can be negative OR positive helped, too.. Then there are more complex ways to do it, by giving the particles' starting points based on the shape of a circle so when they travel the same basic circular plot is maintained.. like fireworks. ..using vectors (not the class)..etc. Ah memories. Thanks.. |
|
yohosuff
Member #10,022
July 2008
|
I began working with 2d environments a little over 2 weeks ago. Before that, it was all text, learning for and while loops, arrays, etc, etc, all the basics. I'm still not that familiar with implementing physics for objects in 2d. I made a bouncy ball program but I'm pretty sure I cheated. I'd say I'm probably around step 5. I'm going to try out some of the examples posted here, although I can already tell that it may be difficult for me to understand. I'll post back later with how I did. Thanks for all the replies! ---------- I can't seem to figure out how to add a post right after my last post, so I'm just going to use the "minus bar" and edit this post. Schyfis, I tried out your code and I threw together a working program below (oh yeah, I don't know how to put it in a cool code window). I'm a little confused about it though, mainly this line: particle* p = (particle*)malloc(sizeof(particle)); This allocates memory for each particle in the vector, am I right? But, why is it necessary to do this? Is it because particle p has been declared as a pointer? And if so, why not just directly declare p as not a pointer? Full working program below: #include <allegro.h> using namespace std; BITMAP* buffer; //define a particle structure int main(){ // particle.x = 200; //an example of your game loop srand(time(0)); clear(buffer); |
|
someone972
Member #7,719
August 2006
|
To put code into the codebox just put [code] and [/code] around it. The particles are declared as a pointer so they can be allocated on the heap(correct me if I'm wrong). This way they can be deleted. The stack is what items are declared on if you don't allocate memory, and it is hard to delete them off of it. The stack will also go out of scope. Ex: int* return_stack(void) { int number = 10; //declare on stack, valid now return &number; //return address of number }; int main() { int* p_num; p_num = return_stack(); //number goes out of scope, printf("%d",*p_num); //causing p_num to be some random value } The heap is where memory that was allocated by malloc or new resides. It doesn't go out of scope Ex: int* return_stack(void) { int* number = (int*)malloc(sizeof(int)); //declare on heap, valid now *number = 10; return number; //return address pointed to by number }; int main() { int* p_num; p_num = return_stack(); //number doesn't go out of scope, printf("%d",*p_num); //causing it to stay 10 //deallocate it (forgot the code) } Always be sure to deallocate memory or there will be a memory leak. ______________________________________ |
|
yohosuff
Member #10,022
July 2008
|
Thanks someone972, for showing me how to do the code thing. By the way, what's a memory leak? I'll play around with the particles for a while a post back if I run into more trouble with it. Thanks for all the help everyone! |
|
Trezker
Member #1,739
December 2001
|
Memory leak occurs when allocated memory is not freed when you're done with it. |
|
Onewing
Member #6,152
August 2005
|
Quote: By the way, what's a memory leak? Whenever you allocate memory, either using malloc (plain C) or new (C++), you are putting your hands around a bit of memory that can't be used for anything else. It is good practice to deallocate anything you allocate, either using free (plain C) or delete (C++). Most OS (that is, I believe it is the OS that does this), deallocates memory on the program's exit. However, it's still good practice to do it yourself, and if you are allocating memory for things that only exist for a moment in your program, you are wasting memory and this can cause the entire computer to run out. ------------ |
|
|
1
2
|