After finishing the integration of the asteroids into the program, I went to add the lasers for my spaceship. By this time I had added the spaceship to the game, but I rather keep things together so I will be talking about everything about the spaceship on my next post.
The first thing I had to figure out was how to obtain the position of the tip of the spaceship, which was meant to be the point where the laser would be shot from. At first I thought I could work around with just the position of the spaceship, but then I realized that in order to things easier for myself I would have to create a class for the lasers. When creating the class I took the following in consideration:
- Laser would have a position and a direction at which it will move over time
- Laser would also count with speed and scale factors.
- Lasers would wrap around the screen as well, just like the asteroids.
- Laser would have a life time. When reaching zero, the laser would be “destroyed”.
After implementing the class, I decided to create a vector that would store the lasers being shot. This way I would not have to worry about there not being enough lasers creating overtime since the player could shoot repeatedly during a short period of time.
Calculating the Origin of the laser
This part was tricky! At first I thought that I could use the same formula for calculating the velocity of the asteroids, but this time it failed me. I had to look around for a formula that would help me calculate the origin of the laser. In the end, I was able to find it! It was a bit similar to the one I used before but this one makes use of two factors: hypotenuse and atan2 of the x and y parameters of the origin. At the same time I had to add 90 degrees to the angle of rotation of the spaceship, so that the laser would be shot towards the right direction. I create and add a laser to the vector as follows:
Although the formula works perfectly the only thing that messes things up is the deltaTime value, which is expected to happen because time between frames is never the same! This sets the origin of the laser a bit off, and unfortunately I have not found a deltaTime value that is fixed in the OpenGL libraries.
Animating the lasers
I created a method that takes care of the laser translation. Within this method I make use of the method that checks if the position is within borders that I had created previously. The formula for displacement is a bit different to the one of the asteroids’ since lasers didn’t have a velocity factor, so I use speed instead to add the displacement value. At the same time, after the laser is displaced, I check whether the lifetime of the laser has reached zero. If it has then I take it out of the vector, just like this:
And that’s about it! Know I have my spaceship shooting all over the screen! PEW PEW! In my next post I will be talking about how I integrated my spaceship into the game which was the most tedious parts of all! Until next time!
0 thoughts on “Simple Asteroids game in OpenGL: Programming Lasers”