What I learned creating a collision engine pt-1.

During my journey into coding, I’ve felt like a small child handed a rubiks cube, given a cursory explanation of what the box is and what the colors are, and the objective is to make the colors of each side the same. Sure I “know” everything now, but can I solve the puzzle?

The project I wanted to create was an arcade where I could continue to create games for my own entertainment and edification. To do that I needed to create an avatar that could walk around the arcade. I knew how to do that create an HTML element with a CSS class name, and add an event listener! That classic recipe tasted just like mom used to make. Then with a pinch of logic and some CSS garnish I had a little blue avatar that would move whenever the keyboard registered a key down event.

The first real problem I encountered is the avatar was magic and would “walk” right out of bounds. *facepalm. Okay so I needed to build a conditional set of rules for when the avatar is allowed to move. I created four functions for moving up, down, left and right. Inside those functions I put a an if else statement so the avatar would only move if wasn’t moving out of bounds. I took great joy at my avatar ̵“bumping” into walls and stopping.

Then I started building the first arcade attraction and it dawned on me I had a bigger problem to solve than making an avatar walk around and interact with objects. I had to program the avatar’s little world so it wouldn’t phase through all the arcade games. I knew I had to create a rule-set for all of the new objects I’d add to the arcade to make them “solid”. I just didn’t have much experience with abstraction at this point, so little in fact I didn’t even notice I’d already done some.

Looking back now, no matter what else broke while I was coding it (and believe me everything broke in many strange ways that hurt my brain), my avatar never went outside of the boundaries ever again. I should’ve noticed that, but I was too consumed in the project to take a step back and see it. I’ll continue telling you how I built the collision engine another day. I just want to stop on this incredibly powerful tool of abstraction.

Abstraction has so many applications I can’t possibly cover them all in detail. So far I’ve noticed it used in functions/methods. Once you have a tool that takes in an input and spits out the desired output you can reuse that tool over and over and over again millions of times per second. The only limitation you face with the function you design are the use cases for the function. To implement abstraction in coding I go for smaller bite sized pieces of code instead of the larger, (seemingly) more elegant solution. Through the use of abstraction all you need to do is build functions that get you one step closer to the desired solution and link them together. Not only does this reliably solve the problem you’re looking to solve, it creates a template you can use to solve all the problems like it. And it’s scalable, the same function that moves you across town is the same one that can move you across from Paris to Istanbul.

That’s the power of abstraction, and I think that’s where I’m going to leave you today.

A software engineer living in lonely world! He took the midnight train going anywhere.