Light Racer 2.0 - Days 19-20 - Laser Sweep

Implementing the laser sweep was one of the most fun parts of the development so far. I wanted the laser to feel like it was controlled by a conscious being and I believe I achieved that effect. The turret is menacing; it locks on to a player and tracks them before charging up and firing a sweeping blast of laser. I was originally planning on having the laser also decimate any trails it touched but after playing with it for a while, I decided that it was plenty effective just as a player killer. The art really worked out well for it too. I'll include what I drew in photoshop so you can see what I did to make it turn out nicely in the game.

Day 19 - What was done:

Graphics for Laser Sweep
Basic Laser Sweep Code, no collisions

Laser Sprites in PhotoshopLaser Sprites in PhotoshopI started by using the same "Base" as the power line. I then drew a little laser cannon and used some effects in photoshop to give it a shaded and sci-fi look. I made a few layers with different lighting on them so that I could have the regular state and the firing state. I also made a square laser beam image that will be stretched by the code. Yes, it could have been 1 pixel thick but it's easier for me to see it if its square.

In the coding side, I created my LaserSweep class which extends MapObject and I started putting in all of the fields and constants I know it will need. I copied the state management code from the PowerLine as a starting point. The laser will work mostly the same but will move around a bit before firing. I thought it would be really cool if it would "Target" players before charging and firing so I added some code to have it randomly choose a target and turn to face the target. This gives it the effect that it is "Watching" you and may fire on you. It worked out well.

The code to turn and face the target originally caused it to flip back and forth and basically move at an infinite speed. I didn't like that so I set a maximum number of degrees per second the laser could turn to and had the update code use the tick delta to adhere to it. Now it looks much more controlled.

I also added in a sweep in a random direction when it fires. It really looks cool now. I would like for the laser to "hit" the wall and also would like it if the beam itself had a little animation in it, but I'm worried that I simply won't have enough CPU to do all of these things with how much is happening in the game already. I think I may wait until the last few days of polishing to use up any extra CPU making it fancy.

My next task is collision detection, which will be my hardest yet. So far I have not had to deal with a thick angled line going through the board. What makes matters worse is that I want the laser to destroy any light trails that it touches. That's what I will spend much of the day tomorrow working on.

Day 20 - What was done:

Laser Sound
Laser Sweep Collisions
Started Portals

I was originally planning on having the laser sweep take out trails that it touched but once I finished with the basic collisions, I was happy with how it worked. I had a little problem with the math to detect collisions with the beam but after I read about a few tricks for checking that a point lies within a rotated rectangle, I got it all to work.

Here are my first attempts at checking my math for collision detection. I used some basic trig to come up with the points that would be affected by the laser and then drew it out to make sure the math was good. The "beam" in these screenshots and on the video is bigger than the real laser will be but I wanted to see it clearly to make sure things were right.

Laser Collision Detection 1Laser Collision Detection 1 Laser Collision Detection 2Laser Collision Detection 2

Here is a video showing it in action:

I didn't end up using that code because I found a different way to do it that is easier.

I was considering switching to vectors so I could use this - - but instead I found it easier to use this method -

When doing the collision checks, I pretend that the rectangle (laser beam) is straight up and down, starting at the point where the lase base is. I then find the relative position of the racer I'm checking for collisions on (think racerX - laserBaseX, racerY - laserBaseY) and that gives me coordinates in a space that I can rotate them in. I then rotate them exactly the opposite amount that the laser is rotated and then translate them back into coordinate relative to the world, since the collision detection code uses world coordinates. A regular rectangle check later and we're done.

Before this, I hadn't really dealt with line intersections at angles before so it was a slow process getting the collision stuff in and making sure it was as efficient as it could be. It seems quick and is working well though so I'm moving on to the portal map.

Here is the final product:

Light Racer Finished LaserLight Racer Finished Laser