DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   2D Java Fireworks Explosion (http://www.daniweb.com/forums/thread108414.html)

CaffeineCoder Feb 9th, 2008 8:51 pm
2D Java Fireworks Explosion
 
Hey, I'm new to the Java arena, and am trying to create an application (using JForm and JPanel) displaying a fireworks show. The problem is creating an explosion for the display. Does anyone have any recommendations for creating an explosion? I've been stuck on this for quite some time and thought I'd see if anyone had any recommendations. Thanks for any advice you can provide.

majestic0110 Feb 10th, 2008 12:03 pm
Re: 2D Java Fireworks Explosion
 
here you go buddy, this example has source code too.
http://javaboutique.internet.com/fireworks/

CaffeineCoder Feb 10th, 2008 2:11 pm
Re: 2D Java Fireworks Explosion
 
Quote:

Originally Posted by majestic0110 (Post 531343)
here you go buddy, this example has source code too.
http://javaboutique.internet.com/fireworks/

Thanks majestic0110, I'll try that. I've been playing around with another version I found a while back, and have trouble integrating it into the rest of my app (get errors that G is null from a NullPointerException). I'll try this other one and see what I can get.

majestic0110 Feb 10th, 2008 6:18 pm
Re: 2D Java Fireworks Explosion
 
ok let us all know how you got on! Send us the code if you are still struggling.

Phaelax Feb 11th, 2008 10:49 am
Re: 2D Java Fireworks Explosion
 
You can use the following equation of projectile trajectory to make your explosions.

x = v*cos(a)*t
y = v*sin(a)*t - ½gt²

Keep an array of 100 or so particles that will make up the firework. Given a random velocity (v) for each particle and a random angle (a). Then just step through the time, updating their positions.

CaffeineCoder Feb 12th, 2008 10:45 pm
Re: 2D Java Fireworks Explosion
 
Quote:

Originally Posted by Phaelax (Post 532192)
You can use the following equation of projectile trajectory to make your explosions.

x = v*cos(a)*t
y = v*sin(a)*t - ½gt²

Keep an array of 100 or so particles that will make up the firework. Given a random velocity (v) for each particle and a random angle (a). Then just step through the time, updating their positions.


Thanks Phaelax, I'll try some of that. Was about to post something when I saw your reply so I'll hold off posting problem code until I play with that a bit more.

Thanks.

CaffeineCoder Feb 15th, 2008 10:14 pm
Re: 2D Java Fireworks Explosion
 
Forgive me if this is something obvious, but I'm still getting stuck on this. Right now the elements in the array don't seem to be initializing since I get NullPointerExceptions repeatedly in the loop. Does anyone see anything I'm missing? When I placed println statements within the code I could see that the initializations didn't work.

int gravity, a, v, T, L, x, y;
          double valueA, valueB, dx, dy;
          int t;
          Random r = new Random();
          for(t = 0; t < Explosion.length; t ++)
          {
          gravity = 20;
          a = r.nextInt(50);
          v = r.nextInt(50);
          valueA = v*Math.sin(a)*t;
          valueB = (.5)*gravity*(t*t);
          dx = (int)v*Math.cos(a)*t;
          dy = (int)valueA - valueB;
          x = (int)dx;
          y = (int)dy;
          Explosion = new FireworksSprite[100];
          //for(t = 0; t < Explosion.length; t ++)
          //{
              System.out.print("Iterating through Array" + t);
              Explosion[t].setSpriteH(15);
              Explosion[t].setSpriteW(10);
              Explosion[t].setLocx(x);
              Explosion[t].setLocy(y);
              Explosion[t].setActive(true);
              Explosion[t].setVisible(true);
              Explosion[t].setVel(0, v);
             
          }

CaffeineCoder Feb 16th, 2008 4:10 pm
Re: 2D Java Fireworks Explosion
 
It WAS something simple, I left out a line initializing the array's objects. I'll leave this thread open in case I run into any further issues, and will close out once this is completed. Thanks to those who have replied, you've been great help.


All times are GMT -4. The time now is 6:34 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC