Trigonometry For Children

Devblog # 4

Dair Deary (spelling?),

The last two weeks have been a real challenge. I had finally moved into actually programming this darn game. Before that it was a lot of notes on paper or sketches/experiments in photoshop. Since I know very little about programming and I’m learning as I go I’m relying a lot on guides and tutorials and a million bookmarks of the engine manual. Unfortunately for me there are not very many tutorials, if any, for creating a 2D oblique perspective game. So I’m combining a lot of tutorials for top-down and isometric games.

Right now I’m racing to a point where I can actually show progress in game. Actual video game stuff. I needed to program how the game runs first. It’s a completely 2D game but it will look 3D(ish). You can walk left and right and away and towards. You can walk in front of objects and behind objects. As a quick visual experiment to make sure my chosen perspective works I drew this:

The rabbit you should recognize from some random comics I’ve drawn. He’s almost certainly not going to be in the game. But you can see my intended goal as far as setting up a scene. In order to make the character walk AROUND the objects but not THROUGH the objects I needed to set up collisions for the ground under the cubes. In order to do that I needed to break down the object and tackle each part separately.

The game engine allows you to create a rectangular “collision mask” for sprites that use very little CPU power. It’s only tracking 4 points in space (the corners of a box). Unfortunately this does not include angles.

I needed to program in a new collision area for every object and I wanted it to be very low cost to the CPU. Fortunately I got lucky because I chose a 45 degree angle for the side perspective. Using the very simple formula for perfect right angle triangles I simply took the side dimension of our collision mask and extended outwards from the top right corner and bottom left corners. This created a new area that I can track.

Now to find out if my character is in fact inside of that area I used the “Point in Triangle” formula. It’s a way of finding out if any given point is inside a triangle by measuring the combined areas it makes. If a point is INSIDE a triangle the area stays the same. If the point is OUTSIDE a triangle the areas get larger. (I’m a math junky and it’s so awesome being able to use these formulas in real life now).

Yes you can use the formula of A = ab / 2 for the first triangle but since you can’t for the other three I didn’t put it in the picture. The silly thing is that I was half way through coding these formulas into the game when I found out that I can use a command literally called “rectangle_in_triangle” which did all the math for me. I just needed to set up the original coordinates of the triangle points and player bounding box. Really saved me a lot of time. Well not really. I had done most of the math already…

There was still one small glitch though. As it turns out the “Rectangle” actually doesn’t trigger when it hits the “triangle”. It only does if one of the corners meets the triangle. Effectively it was doing 4 “point in triangle” calculations.  So to compensate for it I use a “point_in_rectangle” using the furthest point of the triangles and the players bounding box. I’d made a graphic for it but I’m tired and can’t be bothered. If anyone out there ever actually does follow what I’m doing they can ask me to clarify if they don’t already get it.

I spent days. Maybe weeks. working on this. I could recreate it now in a matter of minutes but with only a basic grasp of how to program what I’m thinking the smallest mistake can cost me hours while I try to diagnose the problem. In order to really know I had to finish another issue I had. The depth of objects. When objects or characters move around they need to be shown behind or in front of objects depending on the situation.

I followed an awesome guide from Friendly Cosmonaut to create a grid that will load all objects with a lower Y coordinate first and higher numbers after. This puts them in the correct order going down the screen. This works for most RPG type games like Stardew Valley or Final Fantasy because you don’t see any of the sides of the box.

So close. I went searching for a solution but I wasn’t quickly able to find one. People asked similar questions about how to order all the X coordinates before moving to the next Y coordinate and some of the answers they got were complicated and heavy. For mine I just simply added a ZERO to the end of the Y coordinate for each object and added the X coordinate. So now instead of 15 objects having an identical Y coordinate (ex 1). Then they each get their own like 11, 12, 13 -> (all 15 objects) before moving to the next Y coordinate to find new boxes.

Long story short it worked like a dream. For a few minutes before I found ANOTHER problem. This works great for objects that have the same size footprint. And with a very small modification I made it work for longer items. But items that reach forward and back completely break it. Here’s a grid I created in photoshop with a bunch of different sized and shaped 45 degree parallelograms (the shape my boxes make in the end).

I have dozens of pictures like this from experimenting. It’s ridiculous. If all the objects loaded in the way I needed it to this grid would create a scene like this:

But the largest boxes that extend backwards and forwards the most mess it all up. I haven’t yet come up with a formula that will correct that for me. I’ve had a few experiments come close but nothing perfect. For now I’ll have to just cut up large sprites into multiple objects or tiles so I can continue on with my game. My google searches have just about convinced me that a one-line formula is not likely to fix this. A solution in javascript I found was 200 lines of code and I have no idea how to start using it. I’ll have to come back to it.

But now I’m excited because I can finally start producing assets and show off what this game actually is. I’M SO CLOSE!

Love Michael

A Matter Of Perspective


Soooo Diary,

Remember a long time ago (literally the last post… 5 days ago) when I said there are two games I wanted to make but I’ll make the one I’m least excited about because reasons? Ignore all that. I had a lot of people (nearly 4) message me after I posted it and they made me realize that I’m thinking to much. Who cares if the game is harder and will take longer? Life is too short not to be excited about what you’re working on.

Immediately after I decided to change games I had a new problem. What PERSPECTIVE do I make the game? Here’s some important points about the game:

  • The game is 2D.
  • You can explore large areas.
  • You can arrange items/furniture/objects on the ground.
  • I dislike Top-Down video games (with some exceptions).

Most people understand a little bit about perspective. Especially artists. But what a lot of people don’t realize is that Perspective Projection can make the same object look very, yet barely perceptively, different in specific situations. And each has it’s use. Let’s get more specific. I’ve drawn 3 sides of a super modern and sexy TV.

So lets put our TV together.

What happened? There’s 3 of them already? Yup. The same TV (cube) drawn in 1, 2 and 3 point perspective.

For the people that don’t know it’s related to where the vanishing points are:

Technically the way we see in real life is 3 point perspective. But 1 and 2 point perspective are used more often in art. I’m partial to 2 point, myself. So if I like 2 point so much why don’t I just make my game using that? The issue is because objects appear smaller the closer they get to the vanishing point.

That’s fine for all the level assets but not for the character sprite. Here’s a screenshot from Minecraft by artist Kerian-Halcyon that is in nearly perfect 1 point perspective. I put a 2D sprite of Sonic the Hedgehog to show you how he would look standing at the front. Everything seems to be ok.

Now what if Sonic decides to walk to the back of the temple (closer to the vanishing point)? The temple blocks get smaller but Sonic, because he’s a 2-Dimensional sprite, stays the same size. This makes him appear huge!

Those two are the same exact sprite and same 420px tall. For a 3D character in a 3D world we would see him get smaller but for my purposes that just doesn’t work. And yes, technically I could program the game to scale the sprite as he moves backwards but the cons outweigh the pros doing that. At least for my game.

And here is another ‘challenge’. If I make an object like, say, a sexy Ultra High Def TV and I want to put 9 of them in a line using the same sprite here’s how it will look:

That is one fugly lineup (of gorgeous TV’s). Every TV is pointing towards different vanishing points and that’s just not right. See?

This is how it ‘should’ actually look in 1 point perspective:

If I wanted to add this TV to my game I would need to redraw it from every different angle depending on where in the room it’s located. Neither 1, 2 or 3 point perspective will work. Now we need to start getting into Parallel Projection perspectives!

(I can’t believe this post is this long already and I haven’t even started Parallel Projection yet. I thought I’d make like 3 pictures total for this post and I’m already up to (counting 1,2….3……) 9 pictures!)

Since I’m tired of using the Sexiest TV Ever let’s try something a little more normal. Like this car!

No YOU’RE the stupidest drawing you’ve ever seen. Ha. Burned. (I should mention that I’ve done everything so far with just my mouse because my tablet pen is all the way over there). And this is how they look drawn in Orthographic Trimetric, Dimetric and Isometric perspective. Collectively these are called Axonometric projections.

I know I know. They look the same as the first TV picture in 1st, 2nd and 3rd perspective. I told you the changes are subtle. But the impact is hoooooge (at least for what I’m doing). The difference in these pictures is that there are no vanishing points. All lines run parallel:

“If none of the parallel perspectives have vanishing points then what makes them different?”
Excellent question, imaginary student. It’s the angles that the lines are drawn on. Trimetric has 3 different angles, dimetric has 2 and isometric has 1.

Lots of 2D artists (especially pixel artists) will be familiar with Isometric. It’s the most common axonometric because it’s easy to understand just one angle and because it doesn’t favour any particular dimension. This is a screenshot of Shadowrun Returns done really impressively in isometric.

Lots of theory so far. Let’s see why axonometric perspectives are effective. Look what happens when I put them in a line.

SUPER STACKABLE! By choosing one orthographic projection I can make assets in any order and stack them together anywhere in a room and they will look ok. Which one should I choose? My favourite is Dimetric because it favours one side over the others. This would let me put more details where it matters like the front of buildings, etc. So I’m done? It’s settled?

NOPE! I’m not choosing any of these! Let’s dig deeper. (I can hear you all groaning and I don’t care. My devblog my rules. Or lord I’m up to 15 pictures already).

Hey the TV is back. This is a very common perspective used in classic 2D RPG’s. All lines are parallel at 90 degree angles and you don’t see any of the sides. This effect is used to great effect in Stardew Valley.

Stardew Valley is an important game in the Social Simulation and Farming RPG genres which my game will be taking quite a bit of inspiration from. And the 3/4 perspective is effective but it’s just not what I want for my game. I feel like you are looking at the word through a telescope. Being able to survey your entire farm at a glance is nice but the trade off is small details.

Now finally here is the perspective I’ve decided on! (Only took me two days to write this post plus 2 months of thinking about perspectives to narrow it down). Oblique Projection.

Also called Cabinet Perspective because it’s often used in cabinet schematic drawings. Here are the benefits.

  • You see 3 sides (more details)
  • No vanishing point means they can be stacked and moved anywhere.
  • You see 100% of the front side without any distortion. The front of the car and TV is facing us completely as a perfect square.

The whole point of making my own game is really to flex my artistic muscles. Learning programing/design/writing/publishing/PR/etc is just a side effect. I’m all about the art. Upcoming Beat-Em-Up Raging Justice is done in this perspective and just look at these graphics. So many graphics.

It’s a common perspective used in Brawler games and I thought about trying it for my game a few days ago. But I wasn’t completely sure it would be viable. Brawlers let you walk left, right, up and down but you really don’t go that far into the background. But I found a game that actually used oblique projection really well. A really old game. Earthbound!

You don’t even know how excited I was to find out such a beloved game used this perspective so successfully. Just imagine that game with the graphics of Raging Justice. That’s my goal. is it doable? Hahahaha. HAHAHAHA. I don’t know. Maybe?

So there you have it. But before you go let me show you an easy photoshop trick to fake your way into drawing something in 3D using flat 2D pictures and the Transform > Distort tool.

I know this whole post was completely unnecessary for me to say “Hey guys I’m gonna make my game in oblique perspective”. That isn’t even a whole tweet. And I don’t know who I’m trying to teach with all this. But one day I could be an art teacher or do a GDC talk so it’s good practice. Maybe it’ll actually help somebody??

Also there are other perspective projections I didn’t cover. You can read these two articles if you want to know more about game perspectives. Here and Here.

Love Michael