# Hardcore Particles

**URL:** <https://forum.kirupa.com/t/hardcore-particles/265118>\
**Category:** programming\
**Created:** [July 4, 2008, 6:30am UTC](https://forum.kirupa.com/t/hardcore-particles/265118 "2008-07-04T06:30:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krabex](https://avatars.discourse-cdn.com/v4/letter/k/4da419/32.png) [@Krabex](https://forum.kirupa.com/u/Krabex)\
**Post date:** [July 4, 2008, 6:30am UTC](https://forum.kirupa.com/t/hardcore-particles/265118/1 "2008-07-04T06:30:27Z")

</div>

Hey, a recent post on the forums got me thinking about particles and bitmapData. Now ive never attempted anything like this before so what i am going to show you may blind you with my hideous ideas + code, but basicly ive used the same principles as Sirsian’s Bunny code.

Anyway tbh, it has come out pretty well… but i can do about 300 particles before it chugs and i have seen 10000 particles running smooth, so my code obviously has flaws. Can someone explain to me where im going wrong and what i should be doing!? :party:

Below is a rar with all the files!!

Ty in advance guys!

---

<div class="post-metadata">

**Author:** ![ArmoredSandwich](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@ArmoredSandwich](https://forum.kirupa.com/u/ArmoredSandwich)\
**Post date:** [July 4, 2008, 3:49pm UTC](https://forum.kirupa.com/t/hardcore-particles/265118/2 "2008-07-04T15:49:18Z")

</div>

[quote=Krabex;2353483]Hey, a recent post on the forums got me thinking about particles and bitmapData. Now ive never attempted anything like this before so what i am going to show you may blind you with my hideous ideas + code, but basicly ive used the same principles as Sirsian’s Bunny code.

Anyway tbh, it has come out pretty well… but i can do about 300 particles before it chugs and i have seen 10000 particles running smooth, so my code obviously has flaws. Can someone explain to me where im going wrong and what i should be doing!? :party:

Below is a rar with all the files!!

Ty in advance guys![/quote]

Argh, I can’t open it with flash8, I guess it’s AS3.0? By looking at the classes I immidiately notice two things:

One, why do you have a pCount? If you push every particle in an array simply use pArray.Length. This is a bit more dynamic. This will not necessarily speed things up though.

Two, why do you create a new Rectangle when you’re rendering? This very slow since you do this for every particle every time it is rendered. If you have different sizes of particles atleast just save it in the constructor. Even better would be to reference it to 1 rectangle. I’m pretty sure doing this will speed things up significantly.

EDIT:

You’re also using memory with saving the xSpeed and ySpeed, even the gravity shouldn’t be a local variable. I still don’t think this is what’s causing the huge \*\*\* difference though. But I’ll keep looking 😛

Again, save rectangles if they’re not changing (in the render method of Emitter.as). I’m not sure what screen.fillRect and screen.applyFilter does but I’m pretty sure blur WILL take a lot of memory. Try it without the filters it’s atleast worth a shot.

Also, you’re particles are 4x4 = 16 pixels. The examples you talked about might have 1 pixel particles. Which means they wouldn’t even have to use copyPixels but rather .setPixel(x, y, color). Which could be a freaking lot faster. That might actually explain a lot xD

---

<div class="post-metadata">

**Author:** ![Krabex](https://avatars.discourse-cdn.com/v4/letter/k/4da419/32.png) [@Krabex](https://forum.kirupa.com/u/Krabex)\
**Post date:** [July 4, 2008, 4:26pm UTC](https://forum.kirupa.com/t/hardcore-particles/265118/3 "2008-07-04T16:26:29Z")

</div>

Some good ideas there, i will take into account what you have said… thanks 🙂

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 4, 2008, 6:23pm UTC](https://forum.kirupa.com/t/hardcore-particles/265118/4 "2008-07-04T18:23:46Z")

</div>

> 

One, why do you have a pCount? If you push every particle in an array simply use pArray.Length

important to notice that you should never loop something like this;

for (var i:int = 0; i \< pArray.length; i++) {  
};

didn’t check the file, sorry. Bit lazy.

I posted a particle experiment on my blog, feel free to download the source and check it out.

---

<div class="post-metadata">

**Author:** ![ArmoredSandwich](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@ArmoredSandwich](https://forum.kirupa.com/u/ArmoredSandwich)\
**Post date:** [July 4, 2008, 9:18pm UTC](https://forum.kirupa.com/t/hardcore-particles/265118/5 "2008-07-04T21:18:58Z")

</div>

[quote=sekasi;2353712]important to notice that you should never loop something like this;

for (var i:int = 0; i \< pArray.length; i++) {  
};[/quote]

And why is that?

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [July 4, 2008, 11:37pm UTC](https://forum.kirupa.com/t/hardcore-particles/265118/6 "2008-07-04T23:37:13Z")

</div>

Because it recounts the number of elements in the array every time it is called which is slower than just counting once and storing the value in a variable.

---

<div class="post-metadata">

**Author:** ![McGuffin](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mcguffin/32/408_2.png) [@McGuffin](https://forum.kirupa.com/u/McGuffin)\
**Post date:** [July 5, 2008, 12:00am UTC](https://forum.kirupa.com/t/hardcore-particles/265118/7 "2008-07-05T00:00:29Z")

</div>

```auto

var len:Number = pArray.length;
for ( var i:int = 0; i < len; i++) {

}

```

---

<div class="post-metadata">

**Author:** ![ArmoredSandwich](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@ArmoredSandwich](https://forum.kirupa.com/u/ArmoredSandwich)\
**Post date:** [July 5, 2008, 12:28am UTC](https://forum.kirupa.com/t/hardcore-particles/265118/8 "2008-07-05T00:28:35Z")

</div>

Hmm I suppose that’s true, I thought of that actually, but I thought there was a better answer to be honest. I mostly have arrays that are not static. And I can’t believe McGuffin’s code is faster, quite the opposite actually.

---

<div class="post-metadata">

**Author:** ![McGuffin](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mcguffin/32/408_2.png) [@McGuffin](https://forum.kirupa.com/u/McGuffin)\
**Post date:** [July 5, 2008, 1:11am UTC](https://forum.kirupa.com/t/hardcore-particles/265118/9 "2008-07-05T01:11:12Z")

</div>

[QUOTE=ArmoredSandwich;2353783]Hmm I suppose that’s true, I thought of that actually, but I thought there was a better answer to be honest. I mostly have arrays that are not static. And I can’t believe McGuffin’s code is faster, quite the opposite actually.[/QUOTE]

That seems a strange assumption. The idea behind declaring a variable is that it will remain constant, using pArray.length inside of the FOR loop declaration means that every time the FOR loop goes through, it needs to recheck the pArray.length property to see if it changed during a previous loop.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [July 5, 2008, 1:11am UTC](https://forum.kirupa.com/t/hardcore-particles/265118/10 "2008-07-05T01:11:13Z")

</div>

[QUOTE=ArmoredSandwich;2353783]Hmm I suppose that’s true, I thought of that actually, but I thought there was a better answer to be honest. I mostly have arrays that are not static. And I can’t believe McGuffin’s code is faster, quite the opposite actually.[/QUOTE]Well, it is.

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 5, 2008, 2:28am UTC](https://forum.kirupa.com/t/hardcore-particles/265118/11 "2008-07-05T02:28:15Z")

</div>

[QUOTE=ArmoredSandwich;2353783]Hmm I suppose that’s true, I thought of that actually, but I thought there was a better answer to be honest. I mostly have arrays that are not static. And I can’t believe McGuffin’s code is faster, quite the opposite actually.[/QUOTE]

It’s not only faster, it’s exponentially faster.

---

<div class="post-metadata">

**Author:** ![ArmoredSandwich](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@ArmoredSandwich](https://forum.kirupa.com/u/ArmoredSandwich)\
**Post date:** [July 5, 2008, 12:16pm UTC](https://forum.kirupa.com/t/hardcore-particles/265118/12 "2008-07-05T12:16:42Z")

</div>

I believe you guys, of course I do 😛 I guess I’ll run some benchmark tests to see how big the difference is.

---

<div class="post-metadata">

**Author:** ![Krabex](https://avatars.discourse-cdn.com/v4/letter/k/4da419/32.png) [@Krabex](https://forum.kirupa.com/u/Krabex)\
**Post date:** [July 5, 2008, 4:24pm UTC](https://forum.kirupa.com/t/hardcore-particles/265118/13 "2008-07-05T16:24:29Z")

</div>

Yes back to topic at hand Ⓜ, i redid my code but with set pixel and some other little tweaks, now i can spawn 2000 particles at a time without any lag, but any higher and fps drops dramaticly.

Any other techniques i can try to really get the most out of this ? :hat:

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 5, 2008, 5:01pm UTC](https://forum.kirupa.com/t/hardcore-particles/265118/14 "2008-07-05T17:01:30Z")

</div>

attach your new code, I’ll try and see if I’m able to improve anything (prolly not! :P)

---

<div class="post-metadata">

**Author:** ![Krabex](https://avatars.discourse-cdn.com/v4/letter/k/4da419/32.png) [@Krabex](https://forum.kirupa.com/u/Krabex)\
**Post date:** [July 5, 2008, 5:53pm UTC](https://forum.kirupa.com/t/hardcore-particles/265118/15 "2008-07-05T17:53:38Z")

</div>

Ty alot 🙂

Here ya go!

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 5, 2008, 6:23pm UTC](https://forum.kirupa.com/t/hardcore-particles/265118/16 "2008-07-05T18:23:45Z")

</div>

in emitter.as;

Change update and render to this;

```auto

	public function update():Void
	{
		var p:Number = pArray.length;
		for (var i = 0; i < p; i++)
		{
			pArray*.update();
		}
	}
	public function render():Void
	{
		world.copyPixels(clean,sourceRect,destPoint);
		var p:Number = pArray.length;
		for (var i = 0; i < p; i++)
		{
			pArray*.render(world,pColour);
		}		
	}

```

I’m not quite sure what to do apart from that… the way you are doing this is to have a huge bigmap covering the screen and setting a pixel in a position in it etc… so flash has to redraw that huge bitmap each tic. I think a more moderate approach would be to use a 1x1 object and move that instead.

At least that’s my approach when I play with particles, but I code mostly in AS3 nowadays… :x

---

<div class="post-metadata">

**Author:** ![ArmoredSandwich](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@ArmoredSandwich](https://forum.kirupa.com/u/ArmoredSandwich)\
**Post date:** [July 5, 2008, 10:16pm UTC](https://forum.kirupa.com/t/hardcore-particles/265118/17 "2008-07-05T22:16:56Z")

</div>

Above is exactly what I thought would be slower instead of faster. You still calculate the length of the array each time and even worse you have a variable for it. This is what I meant with McGuffin code being worse.

I just figured you manually assign a variable for length, in the constructor: once.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [July 5, 2008, 10:20pm UTC](https://forum.kirupa.com/t/hardcore-particles/265118/18 "2008-07-05T22:20:34Z")

</div>

[QUOTE=ArmoredSandwich;2354068]Above is exactly what I thought would be slower instead of faster. You still calculate the length of the array each time and even worse you have a variable for it.[/QUOTE]Not in each iteration of the loop though.

---

<div class="post-metadata">

**Author:** ![ArmoredSandwich](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@ArmoredSandwich](https://forum.kirupa.com/u/ArmoredSandwich)\
**Post date:** [July 6, 2008, 12:40am UTC](https://forum.kirupa.com/t/hardcore-particles/265118/19 "2008-07-06T00:40:05Z")

</div>

> [@TheCanadian;2354069](#):
>
> Not in each iteration of the loop though.

Ooooooh!! That’s what you guys mean… OMG?? Never mind me guys ☹

:ponder::ponder::ponder::ponder::ponder::ponder::ponder::ponder::ponder::ponder:

Sorry, I’m pretty ashamed right now. But thanks for keeping patient 😮 How could I not see that??

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 6, 2008, 1:27am UTC](https://forum.kirupa.com/t/hardcore-particles/265118/20 "2008-07-06T01:27:54Z")

</div>

Noone knows everything.

Except Senocular. 😕

[Next page](https://forum.kirupa.com/t/hardcore-particles/265118.md?page=2)
