Which student in computer science hasn't been tempted to create their own physics engine? In this case I think I've postponed it enough and decided to actually go for it.

Numerous examples of particle engines can be found all over the place. Sincerely I'm not claiming that this is the best possible implementation (or even one that is good to follow). The driver for this post is rather the part I found most difficult of them all, and that's how to transpose simple Trigonometry within the world of a Java program. I've found myself struggling with this problem in the past and I didn't find anyting useful online. One could argue it's to simple a problem to post anything about it.

Let's take a step back and see the Physics behind this simulator. Gravity is the force between two objects and it's decsribed by Issac Newton's *Law of Universal Gravitational*. Newton had this idea of the attraction between two object but in order for this idea to be captured in a formula there was still somethign missing. It wasn't until later on that Henry Cavendish made the formula possible by experimentally verifying the gravitational constant . where : the Gravitational constant ,

: the mass of the more massive object ,

: the mass of the less massive object ,

:the distance between the two masses

The nice thing about forces is that they don't interact with each other, so we can simply calculate all the forces between objects and then add them together to find the resulting force of each ojbect at each given time. After we have calculated the force that is acting on an object we can easily calculate the acceleration at each given moment, again thanks to our old friend Newton and his second law of motion. Which gives us a quantitative relationship between the object's mass, acceleration and the net force acting on it.

Here's where I found my first objstacle in the world of Java there is no class that would allow me to add vectors. When I'm talking about vectors, I realise it's a bit of an overloaded term in programming languages (see C++), but in their purely mathematical definition of it. A vector has a magnitude and and a direction. Now all good so far I've got my acceleration ready but how do I tarnslate that into velocity? We have a formula for it.

Looking at the formula and then the ceiling and the other way around...

So in order to overcome this obstacle of adding vectors in java I decided to use my knowledge from high school and replace the net acceleration with it's constituents in a previously decided 2d plane. So I'd have the acceleration in the axis I called x and the one perpendicluar to it in axis y. So the formulas become ,, where is the angle that the vector forms with the x axis and the magnitude of the acceleration (note it doens't have the vector symbol above it). and in order to calculate the constituent velocities we can use ,, where t the time in seconds. In my case I have assumed one second intervals to make my life easier.

The feeling of success didn't last long because one of my own requirements was that I could also draw all vectors, should ever I want to. The second obstacle was how to go back to the net vector by combining the constituents. In order to find the magnitude (easy part) you can refer back to a few thousand years to our other old friend Pythagoras and his right triangle theorem, which we can use since by definition our two constituents are always parallel. But what about the resulting angle? I initially thought it wasn't going to be an issue but then i realised that I had little idea of how atan works in java only giving results in the range . Thanks to my ignorance I went through tons of clauses and cases in order to end up with the simplest one. Then I realised the simplest rule

`public double calculateAngle(double x, double y) {`

if( x > 0 ) {

return - atan( y/x );

}

return atan( y/x );

}

I was mindblown by the simlicity of the solution compared to the head-squeezing moments I'd passed through that evening.