Making an Android game in three hours
programming·@oyvindsabo·
0.000 HBDMaking an Android game in three hours
 <strong>About a week ago, I wrote that I had just started learning app development in Android Studio, utilizing the LibGDX framework. After spending a couple of hours doing simple animations and making objects bounce off each other, I started to feel confident about making an actual game.</strong> <h1>Pong</h1>As part of software architecture class, we were given the exercise of implementing the classic game pong.  (Source: http://uncyclopedia.wikia.com/wiki/Monitor) We were, however, told that we were free to make any changes we would like, so rather than simulating table tennis as seen from above, I wanted to implement it as seen from the side. My main motivation for this was that I wanted to see how well I could simulate gravity.  <h1>More like badminton?</h1>As you can see from the image above, my "realistic" side view doesn't really represent a good model of actual table tennis. There is no table for the ball to bounce on, so I guess my game is closer to representing badminton or volleyball. <h1>Adding gravity</h1>Making the ball travel in a nice parabolic trajectory proved to be way easier than I first thought. At any moment, the ball has a velocity in the x-direction and a velocity in the y-direction. For every frame of the game, the velocity in the y-direction is reduced (I have defined positive y-direction to be upwards) by a chosen gravity constant. <h1>Initial condition</h1>Having simulated gravity successfully would have been of little use if there was no way to get the ball in the air in the first place. I spent some time considering different ways to start the game. Would the ball just be dropped on either side of the net or would it be thrown from the middle of the screen? In the end I decided to throw it from the middle of the screen with a random velocity in the x-direction and a random positive velocity in the y-direction. Both of these velocities are within a range of defined start velocities. <h1>Basic bouncing</h1>The way I have implemented the game, the ball will bounce off the top of the screen, the sides of the screen, the net and the rackets. Initially, I just flipped the y-direction of the ball's velocity whenever it hit either the top of the screen, the top of the net or the rackets. Likewise, I flipped the x-direction of the ball's velocity whenever the ball hit the sides of the screen or the sides of the net. <h1>More advanced bouncing</h1>I wanted to let the player gain better control of how the ball would bounce off the racket, so I decided to introduce friction and conservation of momentum. What this basically means, is that the speed of the ball after it has bounced off the racket depends on the speed the racket had during the bounce. I implemented this simply by letting the ball's velocity in the x-direction after the bounce be the sum of the ball's velocity in the x-direction before the bounce and the racket's velocity in the x-direction before the bounce. I used the same logic in the y-direction, although I had to flip the ball's velocity in the y-direction first. Defining how the ball would bounce off the corners of the net was difficult, because it wasn't always easy to determine whether it should bounce sideways, upwards or both. I guess I could have used something similar to the Cohen-Sutherland line clipping algorithm to determine the dominant side from which the ball hit the net, but I went for an easier approach and let the bounce-rate in the x-direction be proportional to the distance from the center of the net. This way I also reduced the chance of the ball getting stuck on top of the net. <h1>The opponent and the need for inertia</h1>On a smartphone, this isn't really the ideal game for multiplayer, unless it's online. On the desktop emulator, I used the mouse to control the racket, so this didn't lend itself to multiplayer either. The solution was to let the left racket be controlled by the app itself. In the beginning, I programmed the left racket to always position itself under the ball. This effectively meant that it could not lose, so I introduced inertia to both rackets. I did this by defining the velocity vector of the racket as an inertia factor multiplied by the distance vector between the racket and the registered touch of the user's finger (or the click of the mouse for the desktop version). <h1>Opponent strength</h1>To start out easy, I only gave the opponent the ability to move his racket in the x-direction. Since the ball's momentum was always conserved, this meant that as long as the ball had already come from above the net, the bounce off the racket would always be enough to bounce the ball high enough to get back over the net. I did, however, introduce air resistance (the velocity in each direction is reduced by a resistance factor multiplied by the velocity vector for each frame) and energy absorbation in each bounce (similar to air resistance, except the reduction of speed happens only during a bounce). I did this to make sure that the ball does not reach crazy speeds and also to make sure that the ball would not bounce up and down forever. Having introduced these features, I suddenly ran the risk of the opponent not managing to bounce the ball hard enough to get it over the net. To solve this problem, I introduced the bounce strength constant. It defines the velocity in the y-direction with which the ball will travel after bouncing off the racket. This means that once the ball approaches the opponent's racket, it will adjust it's velocity in the y-direction to make sure that it's own velocity in the y-direction minus the ball's velocity in the y-direction are equal to the bounce strength constant. <h1>A preview</h1>Below I have included a gif showing the gameplay about two hours into developing. It probably lags a bit:p  <h1>What's next?</h1>Now that I've worked my way through simulating all the laws of physics in one simple game, I think it would be fun to make a game engine or framework from scratch to simplify the process of making games where objects have to interact with each other in a physical world. I also want to try moving on to 3D games. The possibilities are endless and it's exciting to have started on this journey. I hope I inspire some of you to do it too. I guess I could have written a lot more, but I want to go eat some spaghetti now.