# 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:

1. the movement effect (bomb movement, explosion movement)
2. the fire effect
3. (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: distort the sin and cos functions to find the right throw to our ball

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 : $\vec{P}=m\vec{g}$
and the second law give us: $\sum{\vec{F}_{ext}}=m\vec{a_G}$
So by joining these two equations it gives us : $\vec{a_G}=\vec{g}$
But we need the x,y coordinates of the velocity vector, and then integrate them in our frame ( $\vec{OG}$)
the x component is : $v_{0x}=v_0\cos{\alpha}$
the y component is: $v_{0y}=v_0\sin{\alpha}$
We just had project vector $\vec{V}$ 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… $\vec{a}=\dot{\vec{V}}$
And we know that the only force which influences $\vec{a}$ is the gravity $\vec{g}$, so it gives us: $a_x=0$ $a_y=-g$
the gravity value is negative, of course…, then by integrating we finally have: $v_x=v_{x0}$ $v_y=-gt + v_{y0}$
Now by replacing the initial velocity components we find: $v_x=v_0\cos{\alpha}$ $v_y=-gt + v_0\sin{\alpha}$
MAGNIFICUS 🙂 !!!! , by integrating one last time we find $\vec{OG}$, and our final equation: $X = v_0\cos{\alpha t} + x_0$ $Y = -\frac{gt^2}{2} + v_0\sin{\alpha t} + y_0$
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: $Y = \frac{gt^2}{2} + v_0\sin{-\alpha t} + y_0$
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 __ATA.cmd.push(function() {
__ATA.initVideoSlot('atatags-370373-5c93d64793a79', {
sectionId: '370373',
});
});

__ATA.cmd.push(function() {
collapseEmpty: 'before',
sectionId: '26942',
width: 300,
height: 250
});
});

__ATA.cmd.push(function() {
__ATA.initSlot('atatags-114160-5c93d64793aaf',  {
collapseEmpty: 'before',
sectionId: '114160',
width: 300,
height: 250
});
});

Related

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