Part 2: Coordinate System

Fortunately early on I am realizing we need some sort of coordinate system - otherwise, how would we know where anything is at any given time? This will be used to draw the map of where the game is played and for collision detection.

The first step I took towards doing this was just to visualize what our coordinate system might look like, so I drew a grid over the whole screen, looking something like this:

Oh yeah, I’m drawing circles now, too! That was pretty exciting, and it went a lot smoother than drawing a line. All I did was look up the math to find the points on a circle, and I came up with this:

This will likely be a part of our Asteroid generation, though instead of a perfect circle we’ll add some variance for all the jagged edges an Asteroid might have; but that’s just me thinking ahead.

Also as a quick update, I’ve updated my RenderLine code to handle points on either side - previously the first point passed into the function had to be ‘less’ than the second point, but I’ve added a simple reset if that’s the case:

Ok, a little detour there, now back to the coordinate system! My first grid was looking pretty good, but the first thing I needed to address was to actually store some of this information. Just drawing the lines on the screen doesn’t do me much good. So what do I want out of a coordinate system?

I obviously need some position values for where these ‘squares’ in the grid are located (from now on, I’ll call these Tiles). So what do we actually need to store to get this information? All we need is a tile size and a point. That’s it. While we are at it, I’m also going to define the size of our ‘map’ (the playable game area) so it doesn’t get cut off at the edges and we have nice even numbers to work with.

So the idea of a ‘map’ is our playable game area, and the idea of a ‘tile’ is an individual “square” within our game area. We have our tile size and the dimensions of our map, we can now go about storing this.

I’m using a 2d array (y, then x because of C’s way of storing things) of v2 points on the screen.

Now I’ll admit, I had a couple silly mistakes while trying to initialize and draw our grid. Simple mistakes like using the x and y coordinates as my array index inside a for loop, writing to memory outside of of the array bounds, drawing our grid on the first frame but clearing every frame afterwards, etc. I got through all of these by debugging and figuring out what was going on, and I just want to say that this is ok! I really want to hammer down the point that I am a beginner, and if you are too, these mistakes will happen! But after we make these mistakes multiple times, things will get easier and easier, and we can progress further and further.

Ok, back to the code. Here is what I have for rendering our grid. I’m only drawing squares at each point to visualize it, and it seems to look correct. Also, I’m adding an Offset for x and y, so it’s not directly in the bottom left corner (our origin).

Looking pretty good! Though now I notice the next thing we’ll need to do: map our drawing calls to the coordinate system. I’m rendering the current ‘player’ an approximate center of the screen using the render buffer width and height, and now that isn’t the center of our map. Now that I think on it, we wont want all of our calls to be this way, just the future calls that will directly correlate to the game. Does that make sense? We want to be able to still draw things outside of the games map if we want to.

For fun, I wanted to test if we could see if any point ‘collides’ with a tile on our map - technically, it’s if a point is inside of a tile. Since our index’s are based on the tile size and the map size, I would think we can actually calculate what the index is based on the point. Here is my first attempt at that.

And here we go, will this work?

Ehhh, not quite. You can see that our test point (Green) is not quite at the blue square (the tile that was returned). So some fiddling here still required.

That’s all for today, next time I’ll get to fixing this GetTileAtPosition code and see if we can get that accomplished. I have a feeling it’s either a round error (because I’m totally ignoring that right now) or an indexing error. Then I’d like to get some drawing/render calls to be correlated with the map/tiles we created today.

Also, I’m getting a weird bug at the moment. The program seems to run fine, but when I try to close it, it just won’t close. When I break in the debugger when I try to close, I’m getting a “Access violation reading location 0xFFFFFFFFFFFFFFF8”. Hopefully by the next post I’ll have that fixed too; I know it has something to do with the code we wrote today, I’ve isolated it that far. So if you can spot the issue, let me know! Actually, you have no way to contact me yet.. Maybe we’ll do something about that. Thanks for reading and see you next time!

Also, I wanted to quickly mention this great article that inspired me to do this type of ‘grid’ for future collision detection (Snippet #5 specifically).

Note to self: You are using the terms ‘Render’ and ‘Draw’ interchangeably, and that’s not good. Sorry about that! I’ll try to get better at that.