Laser Collision Detection in Vacuous
Checking for a collision between Vacuous's ship and most objects in the game is rather straight forward; a simple call to "place_meeting(x,y,obj_collideExplodeType)" will do the trick. This checks if the mask of the ship and the mask of the object indicated in the third argument overlap. If I want to create new objects that the ship can hit and explode on, I simply create them and set their parent to obj_collideExplodeType. But for objects like lasers which are drawn separate from a sprite, don't have a mask, and whose length and rotation can change per frame, this method no longer works. Therefore, I needed to write a special collision check function to detect overlap between the ship and any lasers.
First, a check of whether or not a laser generator even exists in the room is done. No lasers, no need to further waste time. If a laser is in the stage, the code grabs the room controller and iterates through its list of lasers created when the stage was initialized and checks collision with each one. Laser generators come in three flavors, shooting one laser, two lasers, or four. Once the type of laser generator is known, it is time for some light math. The x and y components of the generator's direction vector are calculated by the angle of the generator sprite. With the direction in hand, a line collision test can be made between the ship and the two ends of the laser. Since the laser starts further out from the center of the generator, the beginning point is offset by 48 pixels. The other end of the laser is already calculated for when it is drawn, but it gets extended a tiny bit by a "reach" value just to be safe. This safely connects the laser and the solid object it is hitting.
Some other elements in Vacuous needed special collision methods as well, such as water. I'm not fully aware if under the hood Game Maker Studio maintains a scene graph of some kind (for instance, a quad tree) to speed up its built in collision detection. Otherwise, this could be improved by such a feature. However, the rooms in Vacuous are so small to begin with, featuring only dozens of game elements, that it would be overkill. A sequel could implement more efficent collision checking and get away with impressive displays of 1000's of lasers in a single room, but that would only be worth pursuing if it was interesting design wise.
- J. M. Stark