Topic: Game Programming / Game Idea

There are only 3 things in game programming – arithmetic, comparisons, and storage (there are 4, but 1 is the low level stuff you don’t need to worry about at this stage if you don’t even know the other three). All functions that are “DoSomething” fundamentally break down into these three categories.

**Arithmetic**

Value = Value + Value2

Value = 1/Value – Value2

etc…

**Comparison**

if (x < y)

if (x == y)

if (!x)

etc…

**Storage**

x = y

x.addComponent(y)

x.setColor(y)

Classes and objects can be thought of as storage units. Operations are a combination of comparisons and arithmetic. If x is true, do some arithmetic, else do some other arithmetic. When it comes to collision detection, you have 2 colliders (storage). Think of these colliders as 2D boxes. Are these colliders overlapping (comparison)? I’ll give you some code. I expect you to understand what everything means by the end and I hope you go back to that C++ book and actually learn what is in it.

class Collider { int X; //the x coordinate, top left int Y; //the y coordinate, top left int Width; //the width int Height; //the height //This makes a box. Do you see why? With these four variables you know where infinite points are in the box. } class SomeClass { bool isCollision(PlayerCollider, OtherCollider) { //Get the rightmost X int x1 = Math.Max(PlayerCollider.X, OtherCollider.X); //Get the leftmost X int x2 = Math.Min(PlayerCollider.X + PlayerCollider.Width, OtherCollider.X + OtherCollider.Width); //Get the top bottom-most Y int y1 = Math.Max(PlayerCollider.Y, OtherCollider.Y); //Get the bottom top-most Y int y2 = Math.Min(PlayerCollider.Y + PlayerCollider.Height, OtherCollider.Y + OtherCollider.Height); //What we have done above is create a new rectangle that covers the entire intersection area. //Checking if this rectangle occupies a positive space. if (x2 >= x1 && y2 >= y1) { return true; } return false; } }

Assume we are creating a new box that covers the collision area.

x1 gets the left x coordinate.

x2 gets the right x coordinate.

y1 gets the top y coordinate.

y2 gets the bottom y coordinate.

If the right X coordinate is greater than the left X coordinate &&

If the bottom Y coordinate is greater than top Y coordinate, this creates a box with positive area.

Take a look at the image below that demonstrates why this works.

The following image shifts the green block over 6 places to the right. This one has no collision. Why? While the Y creates a positive area space, the X coordinates create a negative space, meaning there is no collision.