Implementing movement patterns, Potato Pirates
|
This past week I have been working on creating the code for the movement patterns of the enemies in our game. I wrote about the design of the movement patterns a couple of weeks ago, and I felt it would be nice to do a follow up on that, to write about the implementation of our design. I started by setting up a new function for movement patterns in the base class of our enemies. Within this function I created the algorithms for the different patterns. In order to have several different patterns available to all enemies at all times (in case we decide to use a different movement pattern for an enemy, instead of the originally designed one) I created an enumerator in the enemy base class which corresponds to the switch-case structure I made in the function for the movement patterns. At first I set up the movement pattern function to take our delta-time, since the enemies position on screen is in relation to their speed and delta-time. The first movement patterns were easy to make, the slow and fast sinusoidal patterns and the linear pattern with the slightly randomized y-coordinate. When I started to make the homing movement pattern however, I realized that I had to get some information from the player class into the movement pattern function since the homing pattern needs to know the y-coordinate of the player in order to be able to home in towards the player. In order to get this information into the movement pattern function I added a pointer to the player class as another parameter, which allowed me to get the information needed. ![]() For the homing pattern itself I used a 2-dimensional vector and the normalize function of the glm library (OpenGL Mathematics) in order to get a normalized direction vector between the relative positions of the player and the enemy plane. The y-value of this normalized vector is then used to set the y-value in the enemy’s movement vector. After the movement vector in the homing pattern is set, the homing pattern keeps updating itself on the player’s position in order to be able to home in on the player as the player keeps moving. So, a good thing to keep in mind is to think ahead and know what you need for your functions before building them, but then again trial and error has it’s place as well :p |
