What math and how?

Hi everyone,

I’m not the worst at math but I’m more of an algebra and discrete mathematics guy. I never learned much matrix math, haven’t had reason to until now. At the same time, I strongly dislike just copying and pasting stuff I don’t understand.

So far it looks lile I need to learn linear algebra. But I’m only interested in making 2d games, so it seems that the linear algebra needed is mostly for implementing a camera.

…my question is–are there things I need to focus on? Are there things i should skip over? Is there anything you wish someone told you about linear algebra before you learned it?

1 Like

All code for 2D games is online, just learn what you aim to achieve, search for that, and get onto the next challenge.

EDIT

Hi!

1 Like

There was this article from a few years ago that was pretty good: Linear algebra for game developers ~ part 1 - Wolfire Games Blog

Otherwise Freya has a lot of good videos too: Freya Holmér - YouTube

2 Likes

I will interject my experience, maybe it will be helpful. I also make 2D games, and never used matrix :slight_smile: I mean, I used, but not at the level you are thinking about. I used the basic ones: Matrix.CreateTranslation(...) and .CreateScale(...) and it was enough. The best approach is to learn on the fly what you need. For example, I recently had a problem with animating on a path on a circle, so I had to learn some trigonometry, law of cosines, finding point on a circle, etc.

1 Like

I mostly stick to 2D myself and the only matrix stuff I use is the 2D rotation matrix from time to time and that one time I derived the formula for line intersection, then just copy/pasted it around for the rest of my life :stuck_out_tongue:

I agree with the others though, let what you need dictate what you learn and go from there :slight_smile:

1 Like

Make your life easy, if you don’t rotate that many elements, just cache the rotations. Write a class that gives you the correctly rotates sprite while under the hood it simply selects it from an atlas.
You will be using zero matrix, have less CPU consumption at a slightly more RAM taken. But nowadays, we have loads of RAM…

This is what is being sold to customers today…

This was on page 3

In short, despite how much I agree, we have plenty of RAM, the truth is, no, we do not…

So I didn’t think it would be a easy as just multiplying Matrix.CreateScale by Matrix.CreateTranslation and then pass it imto spritebatch, but it was. Going to mark veme as solution since that post put me on to it.

The only question then is how to make sure the place the user clicks on the screen is correctly registered as being the entity they meant, since my game has game oordinates asidw from whatever the screen is doing… is there a thing where i can just divide the mouse click position by matrix or something?

That said linear algebra seems useful so I’m going to learn some anyway. :slight_smile:

See, now it’s the hard part :sweat_smile: If you want to simple translate/rotate camera - that’s not a problem, but if you want to calculate coordinates according to those transformations, you need to apply those transformations to the coordinates as well, cause camera moves only the graphics, not the logic. Mouse is positioned according to screen, not camera. So now you need math :laughing:

Personally, I do not use camera matrix, but nesting. You’ve got parent container, set the origin of rotation and angle to it, and all of its children rotates as well. It was quite complicated for me at first, but basically you operate on “points on circle”, maybe it will help with matrix :man_shrugging: I bet simple rotation/translation can be achieved with some trigonometry.

You can check my engine that I work on, on GitHub. Maybe it will help you a little

Otherwise you need to wait for some Matrix specialist that will help or figure it out on your own

If you use standartized world space to screen space transformation then transforming from screen space to world space or even local space is trivial, as long as you are not losing depth information in your screen space you can simply use inverse matrix to move between all space. Usually Local space, World Space, View Space, Normalized Screen Space with linear depth.

1 Like