Sparkling magic wand effect

Started by TheMagician, Fri 05/08/2005 10:01:16

Previous topic - Next topic

TheMagician

Hi everybody.

I'm looking for a general approach to this problem:

I want to have a very small sprite, say a little star, which is emitted from the mouse cursor (which looks like a magic wand) randomly in different directions at a very fast rate and gets deleted soon after its emission to create the effect of a "sparkling" magic wand.

Should I use characters or objects for the sprites? Or even overlays?
And how would I manage to have more and more stars emitted over time (with the old ones getting deleted, of course) without creating endless new objects?

I'm looking forward to your ideas!
Thanks in advance,
Stefan

DoorKnobHandle

#1
What you are looking for is a small particle system.
This particle system is best based on a class like this one:

Code: ags

struct s_particle
{
     float x, y;
     int color;
     int time;
};

function create ( float x, float y )
{

}
function handle ( void );
{

}

s_particle star[MAX_STARS];


As you can see every star has its own position ( x and y ), color and a time variable, that should store how long ( read how many frames ) the star has been alive. Then we create an array of stars.

Also every star has a function to be created and a function that needs to be called repeatedly, that handles this star.

You would call that handle function in rep_ex like this:

Code: ags

int i = 0;
while ( i < MAX_STARS )
{
     star[i].handle ( );
     i++;
}


This will loop through all stars and handle them ( if you have setup a MAX_STARS constand variable to some value.

Then you can create stars at mouse position like this:

Code: ags

int j = 0;
while ( j < MAX_STARS )
{
     if ( star[j].time == 0 )
     // if this star is not on the screen at the moment
     {
           // then create a new one at mouse position
           star[j].create ( (float)mouse.x, (float)mouse.y );
     }
     j++;
}


Now you just need to define those two functions and you are set up.

You will run into some other problems though, you might only check to create a new star every 40th game cycle or every 20th, otherwise they'll be created way too fast.

To display I would recommend using the DynamicSprite.GetFromFile function, because there is a limit of 20 overlays and 50 characters at the moment.

Hope this helped and remember that this code is not tested in anyway, it might as well create an evil virus on your harddrive.

Gilbert

It's currently not as simple as it seems.

If you use characters or objects for it you need to make sure that their baselines are as low as possible so they're drawn over other stuff.
However, since objects are local to rooms, say, when you need to have 5 stars following the cursors, you need to create 5 objects (and sacrify them) for EACH room, so it's not prefered.

I myself like using overlays more, since they're always drawn on top of most of the stuff and you don't need to sacrify some characters or objects, but the drawback is,, if you want to animate the "stars" you need to change the graphics dynamically yourself.

Here is a demo I quickly mucked up using overlays. (compile it with V2.62, I for some reasons use V2.62, but if you want V2.7+ I may muck up a V2.7 one, oh and I didn't have time to make those "random direction emmision" thingie).
I'm sorry but there're no comments as I'm not in very good condition to comment it currently (I blacked out in a hospital yesterday, and had to go to the A&E section afterwards), you may need to decipher the global script on your own.

Unfortunately, currently, there's still a BIG problem if you use characters, objects or overlays, is that they're drawn behind the GUIs.
If you really need them to be over a GUI, I'm afraid the only way is to use several GUIs as stars (unless you use a plugin), with their Z-order set so they're always on top of other GUIs, the problem is, you must sacrify some of the precious GUIs (it'll waste many GUIs if you want many stars to be displayed simultaneously), so I don't know if that is a recommended method.

Rui 'Trovatore' Pires

#3
Or, you could animate the mouse cursor and give it as many frames as you wish. It's not random, but the effect is as good as.

EDIT - Or make a character which always animates as you want the stars to behave, and have it run whenever you want at the same coordinates (give or take an offset) as the cursor.
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

DoorKnobHandle

Of course you could always use the RawDraw functions and draw a little yellow line (2 pixels wide one pixel 'height' ) as a star and have unlimited amounts of them...

That would be what I'd do.

I wrote a demo in standard c++, which does exactly that. It shows a mouse cursor and whenever you click, some stars are generated at the cursor. Looks cool and is   a great effect.

Gilbert

But when using raw draw, everything would be drawn onto the background, unless you won't mind objects, etc., are over the stars.

Rui 'Trovatore' Pires

If it's "animation upon clicking" as BracketEllipsisBracket suggests, you could also set it so that one of the things it does in on_mouse_click is to trigger said animation, place the character (which is the stars) accordingly, and make it visible, and the process the click.
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

DoorKnobHandle

#7
Quote from: Gilbot V7000a on Fri 05/08/2005 11:45:39
But when using raw draw, everything would be drawn onto the background, unless you won't mind objects, etc., are over the stars.

That's true. Also, RawDraw happens to be drawn behind walkbehind areas. I noticed that while working on my part-time project Troopers.

I'll also try to create a demo showing this effect. For the heck of it. ;)

EDIT: Gillbot, your demo looks great, but I was thinking of a slightly different effect!

DoorKnobHandle

#8
MY VERSION OF THE EFFECT AVAILABLE!

Here, I wrote it in 30 minutes.

Shows my version of the effect rather nicely, I think.

Notice also the Troopers screenshot as background... ;)

Try it out, HERE is the link.

NOTE: You'll need to copy&paste the link into a new window for it to work!!!

TheMagician

Thank you very much for your input!  :o
Thanks for even spending your time to create demos!!

First...
QuoteOr, you could animate the mouse cursor and give it as many frames as you wish. It's not random, but the effect is as good as.
EDIT - Or make a character which always animates as you want the stars to behave, and have it run whenever you want at the same coordinates (give or take an offset) as the cursor.
Yes, I thought of that at the very beginning. And perhaps it is really the choice which causes least problems.

However I got curious (as some of you obviously) if it is possible to create something like a small particle system, so I'll definitely have a good look at your code [...] and see what I can make of it (there is much new stuff in it for me).

It's very interesting how the different approaches cause different problems:
-characters drawn behind GUIs
-objects have to be created anew in every room
-RawDraw draws everything on the background

When I'm home tonight I'll have a good look in the source code of your demos! I already love the effect in your demo [...]
I guess I'll try an approach using overlays tonight and see how it works out!


Thanks again for all your work and help!
And I hope you get well soon, Gilbot V7000a!

Stefan

P.S.: Sparkling Roger in that military backdrop is hilarious!

DoorKnobHandle

#10
Thanks for the flowers. ;)

EDIT: Now I feel bad for not commenting the code out, so if you have any questions or don't quite understand what a certain line is doing, feel free to send pms or add me to icq/msn.

DoorKnobHandle

VERSION UPDATED!

If you were interested in the source code, then simply re-download the zip-file again.

It now has everything heavily commented out, that should make it A LOT easier for you to understand everything.

NOTE: You still need to copy&paste or drag&drop that link into a new window for it to actually work!

Thanks.

SMF spam blocked by CleanTalk