It has been about a month since I really worked on the game. It was essentially do to an issue I wasn’t quite sure how to overcome. Back in December I thought that one essential thing I’d like to add to the game is the ability of enemies to fire in the direction of the player. Usually in shmups you have enemies flying in a specific pattern and shooting straight of shooting out in a spray of bullets. Although I’ll have plenty of that, I’d like to add more difficulty by having enemies shoot AT you. The same concept is carried over into heat seeking objects. It was clear that knowing how to implement “directed forces” on my object was important.
Vectors are some quantity with a direction and a magnitude. They are used every millisecond in AAA games so they are a pretty big deal in games in general. Usually vectors in games take the form of a force in a specific direction. Sound familiar? Ya well, this is what I need. But working with vectors requires an understanding, albeit simple, of some trigonometric principles. The reason why trigonometry is the coolest thing ever is because it allows you to calculate distances and angles between points you can’t touch or measure, using points you already know. Trigonometry is used in GPS to give you super-detailed distances between places in your car or phone. It’s used for a lot of military applications. And it’s used in physics.
Anyways, I took trig last semester and became pretty familiar with how to get points, lengths of sides, and angles. But what I needed was a vector velocity in (change in x per second, change in y per second). An angle is in units of radians or degrees and is not in x,y because an angle describes a relationship between two points without any information as to how big, far, long distances are.
Taking the month to find the answer to this problem, I discovered the proper mathematical way to convert an angle to vector x and y. The actual x, y are very small, only big enough to describe the direction between to points (the player and an enemy). By multiplying the x,y values by an arbitrary magnitude (I think in the game mine is like 750), you increase the ‘force’ of the vector. Then I assign this force/direction (aka vector) to the velocity of the bullet I create and VOILA! The bullet shoots in the direction of the player.
In the game I also set up a delay so that enemy ships shoot every 100 frames or so. This is fully adjustable and will be cool to work with as I continue to build the game and the enemy patterns.
Oh and I made the basics of a shield. 🙂