I'm at the beginning stages of building my first 2D game engine, and I'm not sure if I should setup my base 2D vector class as a vec3(x, y, z) or a vec2(x, y)

I might want to use a vec3(x, y, z) for adding z values, for ordering which objects would overlap other objects and still be visible. I'm also not sure if it's just easier to have a vec3 for working with the transformation matrices and graphics libraries like OpenGL. This is my first game engine project, and I don't really know what is required for the long haul, in a 2D game.

How should I setup a vector class for a 2D game engine?

  • 1
    \$\begingroup\$ Use a pre-built library, and an engine if you can. Doing your first game without an engine is hard \$\endgroup\$ – Bálint Feb 18 '17 at 21:06
  • 1
    \$\begingroup\$ While I agree with you, I'm using this project as a learning exercise as well. I'm not looking to create the next call of duty or anything, just a simple game that I created with a simple engine I created. Already learning a lot doing so! \$\endgroup\$ – Jason Feb 18 '17 at 21:11
  • \$\begingroup\$ Note that you'll learn a lot about engines by using an already available engine. Designing stuff out of thin air without being familiar with said stuff is hard, and you may learn the wrong things... \$\endgroup\$ – Vaillancourt Feb 18 '17 at 22:21
  • \$\begingroup\$ It is definitely hard, I'll give you that! However, by doing things this way I am also learning tons about how to structure a software architecture appropriately. This means learning practical ways to think and go about building an application. I recommend it to anyone with enough patience. It can be very rewarding \$\endgroup\$ – Jason Feb 21 '17 at 2:21

It would take less space in memory to use a Vec2 for things like game object position or scale (2 floats, instead of 3) which could be a win, but since this is your first 2D game engine I would just go with what feels best to you until you absolutely need to optimize. As far as rendering goes, it isn't that much of a hassle to move your x and y components into your graphics calls. As you mentioned, layering can become an issue. For a 2D engine I would probably have a separate field on my sprites for which render layer to use, and then draw each layer separately from back to front with a z value associated to that layer. Ex:(background z=-3, world z=-2, foreground z=-1, etc..)

Best of luck. Cheers :)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.