Journey on C++ and DirectX #1 - Drawing and AABB Collision

Given my background with the Unity Engine, I already take many things for granted, such as rendering, input, collisions, and so on.

However, considering the market demands, I realized I needed to learn some C++. So, why not start with the game development aspect, right?

After reading through many chapters on learncpp.com and completing some basic tasks on exercism.io, I searched for a tutorial and found 'Beginner C++ Programming DirectX' by ChiliTomatoNoodle on YouTube.

I'm only on the 5th tutorial, but I'm attempting to tackle tasks independently, sometimes even before Chili introduces them. I'm not claiming to be a master or anything like that, but I firmly believe that's where knowledge truly flourishes—when you attempt to solve problems on your own. And if you can't, that's perfectly fine too; after all, tutorials are there for guidance.

After he explained the basics, I began drawing something on the screen by manually placing pixels at specific positions. Yes, it may not look good, I know that. But when you're learning, sometimes you have to take the faster, uglier, and hardcoded approach.

Drawing a crosshair on the screen

So, this gets me a little crosshair.

Nice!

 Now it's time to make the crosshair move. To do this, we just need to change where all of these pixels need to be drawn on the next frame. They all have a position, right? So we just need to shift all of the positions at the same time and by the same value when we press a key.

For example, if we add 1 to the X position when we press the right arrow and subtract 1 when we press the left arrow, we can freely move our little crosshair horizontally on the screen.

Similarly, if we subtract 1 from the Y position when we press the up arrow (since, at this moment, it seems DirectX considers positive Y to be downwards, so to move up, we need to decrease the Y position), and add 1 when we press the down arrow, we can move the crosshair vertically on the screen.

I didn't implement any treatment for diagonal movement, so you move faster diagonally

Now it's time to check for collisions. Using the same method as before, I'm going to draw another crosshair. When my crosshair 'collides' with the other, I'm going to change its color to red.

A popular and performant way of checking if two objects are intersecting each other is the AABB collision detection. However, it's not perfect because we cannot rotate the objects; they need to be aligned with each other for the calculation to work. Fortunately, in my case, it will work perfectly because I don't rotate the objects (at least not yet).

So, I found a simple function while searching for AABB collision:

Checking if two objects are intersecting each other

Using this in my game, and there we have it:

Nice!

One little problem though: the intersection check considers the bounds of the object, and I'm considering my object to be a box. Since it's a crosshair, it has a more complex shape, so the collision triggers even outside of the object, like being a box outside of the crosshair. But that will do it for now.

:)

Comments