Sunday, June 17, 2012

Rocket Tank - Fun with Physics

The main mechanic of this game is that rockets will bounce off of walls. The player will have to judge their bank shots correctly in order to hit a certain target (or avoid hitting other targets). This will all be made possible by this Physics-related type thing called the Law of Reflection.


This law states that a ray cast towards a surface will reflect off of it at the same angle it hit: like in the diagram above, the angle of incidence will be equal to the angle of reflection.

Though the law of reflection initially applies to light rays and specular reflection, it can also be applied to the trajectory of moving objects. If you think of a game such as "Breakout," this law holds true: the ball bounces off of a wall at the same angle it hit it. Now this effect is easily reproducible for round objects (such as a ball or sphere), but I'm trying to bounce cylindrical rockets (which actually have a simplified rectangular collision mesh). The collision system won't naturally allow a box to reflect in this manner, so I get to force it manually. Fun.

So, does this equation look familiar to you?


Unless you watched the video I did on the kart racer's artificial intelligence system and paused the video at this exact point with the express purpose of knowing and learning this equation, probably not.

But here's where the kart racer's AI system comes in. Part of the script includes an equation that outputs an angle measure when given two vectors. The two vectors I worked with were the kart's world orientation vector (what direction it was pointing in), and the steering vector (which pointed in the direction the kart needed to go).

The exact same mechanic applies here. I still have to calculate the angle between two vectors: only this time, between the direction the rocket is facing with the direction the wall is facing. I can get these by grabbing the rocket's world orientation vector and the wall's surface normal.


The only trouble I foresee is actually rotating the rocket. With the kart's AI, the script ran continuously because it always needed to steer the kart. But to control the rocket, I have to save the calculated angle and rotate the rocket by that amount the instant it collides with the wall, then recalculate the angle for the next collision. I'm not immediately sure how to pull this off, but I'll see when I get there.

Wednesday, June 13, 2012

Rocket Tank - Cursor Control and Particle Effects

I've added some basic functionality to the game. There's now a crosshair shown on screen that will follow the mouse. The gun from the tank will always point towards this crosshair, so that the player can actually aim their rockets. (Ya know, aiming is usually a good thing in a game where you have to shoot stuff.) I'm considering also rendering a laser sight that will make it easier for players to judge the angles of their shots, but only if it's absolutely necessary. Rendering a good looking laser seems pretty difficult.

Also, I tried messing with an addon for Blender called EasyEmit: it's basically a particle system designed to work in the game engine. I was able to tweak it to get a good flow of fire shooting from the back of the rocket. 


Just one little problem though- when the rocket object ends, the flame particles just freeze in midair and stay there. I'm guessing this is because the particle emitter is simply parented to the rocket, and gets deleted along with the rocket. This leaves the emitted particles to freeze, since the emitter object that tells them what to do is gone. Not sure what to do about that; I'll have to look into it.

I'm also working on a custom particle explosion using EasyEmit- it's pretty easy to use and implement (hence the name EasyEmit :P), and it's great that I can get some decent looking particle effects even with the presets.

The next thing to work on would be the actual rocket-bouncing mechanic. This will be a bit tricky, but I realized that I can actually use part of the code from the kart racer's AI system and implement it here. I'll explain it more in depth in my next post. Until then..... lemons. Don't take 'em.