# Put some Java to your next Valentine’s Day

As I told you in my description, I like waste my time to code just for fun… But don’t misunderstand me, it could be fun, but also enlightening in many ways.

When I began to communicate in machine-languages ( I mean to code lol), of course as every child I want to make a video-game ! After some tries, I conclude that what I prefer is to code graphics effects.

One of my favourite effect is the simple fireworks effect ! As my title suggests, I will give you some codes and explanations to help you design (big word!) a beautiful fireworks effect to make falling down the heart of your girlfriend (depending on her… 😉 ).

So now, let’s put some Java into your love !

By my way, we could break up the fireworks in three effects:

- the movement effect (bomb movement, explosion movement)
- the fire effect
- (surprise)

In this first code post, I will explain the movement effect, all in Java.

If you want to learn Java, without the boring “Hello World !”, this is a good start ! The code is simple not difficult.

**The movement equation**

We have to “create” an equation which could simulate a throw. This is the mysterious part for anyone of you who totally forgot the famous functions: sines and cosines !

You could take a look on this page if you think needed.

This is the code you’re looking for:

public double move(int t_plus){ set_x((this.x_initial + this.v_initial*Math.cos(this.theta)*this.t)); set_y((this.y_initial + this.v_initial*Math.sin(-this.theta)*this.t + (0.5)*this.a_initial*(this.t*this.t))); this.t = this.t + t_plus; return (double) this.t; }

- For X and Y, I used respectively Math.cos and Math.sin
- this.x_initial and this.y_initial, are the initial coordinates of the ball
- this.v_initial means initial velocity of the ball, but in fact it’s just the amplitude of my sin or cos functions, more this value is high, faster will be the rise and the descent of the ball (to figure it out simply)
- this.theta is the start angle of your throw
- this.a_initial is the acceleration.
- finally, this.t it’s the time parameter (iterator).

To figure out how it works, it’s very simply, we could imagine the circle of trigonometry, and all we have to do, is to find a way to distort the circle on the ellipse, which match with the form we want for our throw:

Try to modify some parameters, and find your own ellipse ! You could also try to find a new trajectory for your ball, for example, you could find something which similar to a flight of fairy !

**Mathematical explanations**

Ok if you want a full explanations, it’s not a good place, check these links (because I’m not a good physicians, and latex users too ! 😉 ):

- http://en.wikipedia.org/wiki/Parabola
- http://en.wikipedia.org/wiki/Trajectory_of_a_projectile
- http://freephysique.free.fr/cours%20403.html (really a good explanations, but unfortunately it’s in french)

To summarize, the first Newton’s law gives us :

and the second law give us:

So by joining these two equations it gives us :

But we need the x,y coordinates of the velocity vector, and then integrate them in our frame ()

the x component is :

the y component is:

We just had project vector on X and Y ! (the same for Z if you want a 3D frame)

Ok, now remember what you have learned so many times agos…

And we know that the only force which influences is the gravity , so it gives us:

the gravity value is negative, of course…, then by integrating we finally have:

Now by replacing the initial velocity components we find:

**MAGNIFICUS 🙂 !!!!** , by integrating one last time we find , and our final equation:

**Finish him !**

Ok now for all of you, who red this, the right question is about my Y equation, in fact in java I used this one:

It’s because of the origin of my frame, it’s not the bottom-left corner, but the top-left corner…

Now try to compile and look this balls explode ! 😉

I’m trying to upload Zip source-code but, WordPress seems to do not authorize this kind of files…

Finally I found a good solution, simple and fast upload: mediafire

Here’s the unclean version of sources:

there are 4 files :

- point.java: basic class
- projectile.java: inherited from point class, defines basic movement.
- bomb.java: inherited from projectile class, defines a special movement, and explosion of projectiles
- graphique.java: main class

For the next part, I will clean and comment the code.

I open this blog to train my English writing , be indulgent please ! thanks to comment and correct my mistakes

- Posted in: code ♦ IT ♦ java
- Tagged: demo, fire effect, firework, java, Valentine's Day

## 1 Comment

## Trackbacks