How do you learn to do mathy-algorithms on game engines?

Hey guys, so I made this post just for information, basically.

My question is like, how do you guys learn these mathematical algorithms for different cases, on any game engine? Obviously the way you’d write up the code would look a little different on different engines, I would presume. Could be wrong, however…

An example:

Say I wanted to make it so your character’s head points toward the camera in a smooth fashion.

I wouldn’t even know where to begin with that. I mean, for one thing, there’s formulas that you learn in math. But then you’d have to translate that formula into whatever game engine you’re dealing with. So I mean, how do you even learn this stuff? I’ve seen some very complex formulas or algorithms included in stuff like, I don’t know, joint manipulation or animator scripts.

I mean, I’d definitely consider learning this stuff. Thing is, I question if this stuff is really “to learn,” I mean, you’ll have a bunch of different scenarios or stuff you’re trying to make, and the formulas will be a bit different each time.

Another thing, which formula you need to come up with, and to always know how to use the formula for what you’re doing.

It really complicates me. I mean, never in a math class would I learn a formula for doing a specific task. Any articles on this whole concept, or just general information for how this works for you, would be greatly appreciated. Thanks!

17 Likes

Eh, this is a tough question to answer. To be able to do what you ask, I would say there are some requirements, and the biggest one is: You must be able to learn things by yourself.

A programmer dealing with stuff like this would likely need to become familiar with a number of complex algorithms and formulas and mathematical concepts. Schools don’t really teach that, and if you leave school you won’t be learning anything new after that (meaning you won’t be able to use new ideas).

It’s pretty hard for a lot of people, I think, myself included.

In terms of actually doing stuff, you need to figure out both the concept behind what you want to do, and the doing of it. Learning game-engine specific features is the easy part. The hard part is converting the concept into an algorithm.

The other hard part is of course learning the concept. For that, you need time and dedication. A lot of people look at something hard and assume its hard and just don’t properly try. Properly trying would be completely ignoring the enormity of what you are learning, and instead working to understand each sentence, sentence by sentence, of what a book or article or whatever is saying.

Hopefully this is helpful lol

8 Likes

Well, although I am most definitely not a super “mathy” person, if I ever need to figure out how to create something in particular, I just go to the same resources I used to start learning in the first place - the wiki for basic stuff, and asking fellow developers for more complicated stuff. Eventually, you will get to the point where the knowledge you have learned on how to create certain things can be applied to new things too. It just comes from experience imo.

It can be very helpful to ask developers the concept behind how what it is you want to do works, versus asking how you would do something directly.

khan academy can be an extremely helpful tool for learning any new maths as well, I use it constantly. If you don’t understand a written piece of code, figure out what type of math goes into it and get to learning! ^.^

Just as an example,

I have made “smooth head turning” before, and all that really went into it was some trigonometry and interpolation.

I used trigonometry to find the angle from the npc’s head to the target point (which was the player in my game) .

Then, I used that angle as the “target angle” for the head to turn to. To interpolate it, every frame I would set the head’s actual angle to an angle between the current angle and the target angle. (I used
Angle = (targetAngle-Angle)/2). This makes the turning smooth.

Some additional math went into making the head stop at 90 degrees on each side so as not to look like it had a broken neck, but that was pretty basic stuff.

8 Likes

Oho, good question. I always wondered this myself, like, “where would I begin on making heads turn around? I have a vague idea of the math, but how do I apply it?”. And then other scenarios like throwing a grenade - knowing that you need a parabola but not sure how to fill in the values or what variables to use when calculating it, or even how to make your grenade follow that parabola.

Math is a tough world. Bookmarked to see responses.

2 Likes

Your problem seems to be that you’re looking at the entire daunting concept of math in programming, and panicking.

Math is nothing but a tool, just like how Lua is a tool. You understand how you can use Lua to accomplish something via a series of logical steps. Math is no different in this respect. In general, you need to practice with components or small aspects (ie. vector manipulation), and once you have an understanding of how you can manipulate that aspect, you can start imagining the steps you need to take to make something happen, just as with Lua. Learn tools such as algebra, calculus, trigonometry, etc, and practice applying them with small projects. As with anything, the more you practice, the better you’ll get at seeing what you need to do, and doing it.

Nobody just memorizes a handful of formulas and runs off to do magic. That’s not how it works. They break their problems down, realize they need to do X thing that can perhaps be modeled with an equation (ie. physics simulation), and they either go to Google to find an already well known formula, or use what they know relevant to what they’re working on and build up a solution. Vectors are a very good example for this. A lot of math in Roblox development is vector manipulation.

School teaches you how to apply algorithms and formulas to solve problems, but unfortunately they don’t teach you how to come up with those algorithms and formulas until far into post-secondary. That’s something you eventually learn on your own.

21 Likes

Thanks for all the wonderful information guys! I’ll definitely keep this in mind. Any other perspectives on the topic would be appreciated as well.

2 Likes

I’m in the same boat as you. The mathematical part of scripting is the one area I have yet to be proficient at.

6 Likes

The biggest misconception is thinking that people just know how to solve these problems. I’ve often seen maths problems and thought “How did these people even know where to begin let alone solve the whole problem!!” but the truth is no one starts off just knowing how to solve problems.

Your biggest tool for solving a problem is not knowledge, it’s motivation. If you are motivated enough you will find a way to solve almost any problem given enough time (provided the problem is not some obscure mega-hard problem even degree level mathematicians would struggle with).

The second biggest tool you have is connections. Ask other people! Humanity has only gotten as far as it has by everyone putting in a group effort. Even if people don’t know the answer they may be able to give you some pointers on where to look.

Once you push through the struggle and ask others for help you will eventually come to a solution. It may not be perfect and sure it may have been difficult but now you have that knowledge locked in. When you come across the same problem elsewhere you will know how to solve it. Similar problems will now seem much more approachable and you’ll have an intuition on how to go about solving it.

People only know how to solve problems by having a foundation of previous problems they have solved, as well as being able to ask others for help. So get out there and start building your foundation of problem solving skills!

(Also I don’t think you ever feel truly proficient at the mathematical part of programming. I don’t at least. There will always be something you can’t do, you just find harder problems but this is a good thing as it keeps motivation to keep learning new things. This is also kinda like learning a new language. Looking at the entire language and thinking you have to learn it all in one go is unreasonable and scary. You learn it bit by bit. The point at which you start being “fluent” is also very subjective. You may never feel fluent. I’m sure there is lots about a language that a fluent person doesn’t understand too but this doesn’t stop them from being fluent and using it to their advantage.)

9 Likes

Pretty sure most people that can do that are mathematically inclined. They’re just really good at applying the math they do know to the real world (well technically it’s virtual but whatever).

I’m sure they just use the algorithms enough that they eventually become second-nature to them, just like traditional math. They understand when an algorithm might apply to something so they try it out.

I for one almost always need help with this stuff so I consult others on the dev forums or use Google. Maybe one day I’ll be smart enough to do the amazing math others do.

3 Likes

Desmos.com is a good platform for creating your own algorithms

6 Likes

Ok, I’m going to be completely frank here. You need to have mathematical awareness in order to program these kind of stuff. Ie to know how math works and how to use math to do what you want to do.

Main question is:
What does multiplication and addition do? And how do they relate to each other?

After you know the answer to that fundamental question, coming up with equations is actually a piece of cake for most of what you need in programming general stuff in game.

But you don’t just need equations to turn a character’s head. You need to learn about matrices, linear transformations, interpolations, quaternions.


Another thing is there is no;

This is a wrong way to look at math and equations within. There is no equation that you need to fit in order to make a formula. You wouldn’t be making a formula there, you’d be using one. There isn’t a list of formulas that you need to know to come up with one that works for your situation.

Someone on discord asked how he could relate a random number with the price. The higher the price, the lower it should appear so a lower number. He said he wasn’t good with math and that what he was coming up with was complex. This was actually a very easy solution.

math.random()/price

This is the main idea. If you’re relating two things, you don’t need complex math as they can relate easily like this. But let’s say this doesn’t quite give you the curve that you want for your game. You want the randomness to drastically drop as price goes higher. We can use another type of relation, powers. You can take the power of price (^2, ^3 etc.) and the graph in which the area that the randomness has will get smaller at a much higher rate. After this is easy, you have your random number that scales with the price however you want and you can do whatever you want with the new number.


Now I’ll start explaining a bit about the question I asked:
What does multiplication and addition do? And how do they relate to each other?

Think of a formula as a bread recipe. It needs flour, milk and eggs. But does it need all of them? That gives us a cake dough, we don’t want a cake. We want a bread dough. So we add flour and water maybe salt, yeast and oil.

bread_dough = flour + water

But is this all there is to make a dough? What is the unit of the dough? How much flour and water do I add? Now here, you need to know PHYSICS. What is your unit of measurement? Is it grams or milliliters? Is this thing scalar or is it a vector? Is this flour a matrix or an angle? We all know it’s supposed to be in grams it’s mass of something. (screw you imperial users) Ok but can you add a mass and a volume together? No. How would the unit of something like that change? That never happens. In physics and math, you don’t add things with different units together 99% of the time and you most definitely don’t in any practical situation in Roblox. So whatever we add, must have the same unit.
Now how much do we want of these things since it’s in grams? Do we add just one gram of both? No we are not making bread for ants.

bread_dough = x*flour + y*water

Oh great now we have some bread but it doesn’t taste that good. We should add some salt in it. But we have some amount of salt that we have to use no matter what and we have to scale our dough amount with that salt. We know the perfect bread needs 1000 grams of flour, 100 grams of water and 10 grams of salt. Since we have to use all of that salt because it’s what we’re supposed to base it off of. We need to come up with 1000 grams of something somehow:

salt = something
x = salt.mass*100
y = salt.mass*10
z = salt.mass*1
bread_dough = x*flour + y*water + z*salt

Now let’s get back to units. We said that when you multiply two things, the unit of it changes. But what if what we have is unitless? Let’s start by explaining what the unit of flour / water / salt is: We wanted to add grams together right? So what kind of a unit does that flour thing have if x / y / z is already mass? Actually, x, y and z don’t have any unit. Inherently they represent the grams. But what we take is the quantity of mass. Such that it is a relation of how many grams are there in it, not the grams itself.


Here we’ve created a formula for something. So how does this relate to anything in programming? If you know about the mathematical expressions, it all only boils down to how x and y must relate to each other. You have to ask yourself what is the relation between (in your situation with heads and camera) where the head looks and where your camera looks. Well we both know that orientations are defined with CFrames in Roblox which is actually just a matrix. In this case you would need to learn matrices and how to manipulate them. Now most of the boring math is done with the functions Roblox provides so learning what they are used for is enough. You don’t really need to know intricate ways “the CFrame math” works. BUT do learn everything you can about Vectors and what they are useful for, that is very important. Trigonometry is also something quite important. Learn what Tangent, Cosine and Sine functions do and how they relate.

20 Likes

Someone in here said that school doesn’t teach these things, but I feel it is so important that I’m going to mention it first! Mathematics in school is a progression, a story, and changes as time goes on. When you are in elementary school, you learn some of the simple truths about numbers (addition, multiplication, division and fractions, negative numbers, powers) but once you start getting into algebra/geometry they start telling you little white lies. They give you equations to memorize. For the student who asks something truly curious, who would have been able to learn something, “where did the formula for the area of a sphere come from?” they’d get a reply like, “from the book.” When presented throughout middle school and high school, math is a fragmented beast often with only little resemblance of the underlying beauty of its perfect patterns and absolute truth.

In truth, it really isn’t until upper college levels that I’ve found all aspects of mathematics to truly converge. Having taken Calculus 2, I now know how to write a program for sin, cos, tan, or even a decimal power. Calc 1 teaches where most of the formulas you were told to take for truth came from. Also in college you learn about the history of mathematics and problems that stumped the world for years and how they were solved. It is classes like this that as helped me solve the problems that I’ve been presented with.

By the way, the interpolation between angles is a 4th dimensional problem. This discovery, and the resulting solution resulting from it (Quaternions), is a truly fascinating story of Sir William Rowan Hamilton, an Irish professor and mathematician. Looking at the history section of Wikipedia on his solution to this problem, I see it is littered with lots of contributions and discoveries by professors leading up to it.

Universities dedicated to the search for truth have caused professors to publish their knowledge for the common good, often in academic papers. Some of the first papers I read were for A*/Theta* path finding optimizations, the Lua VM, and the Gilbert-Johnson-Keerthi multidimensional intersection and distance algorithm. In addition to reading the good tutorials online, the truly educated person will enjoy a good paper on a topic of interested every once in awhile. :slight_smile:

If I could sum it up, I’d say there is a reason your parent’s wanted you to go to school, and a reason people pay thousands of $$ for it. But yes, I must admit, it is possible to squeeze through school without learning anything, but to the student who pays attention and applies what they learn, school will be a rewarding experience. I’m really grateful I had Roblox when I was growing up, because it actually gave me a place to apply what I was learning in school in my games and scripts!

(I’m sorry, I had to include a couple clips from that history on Wikipedia looking over it again:

And here there dawned on me the notion that we must admit, in some sense,
a fourth dimension of space for the purpose of calculating with triples ...
An electric circuit seemed to close, and a spark flashed forth
Here as he walked by
on the 16th of October 1843
Sir William Rowan Hamilton
in a flash of genius discovered
the fundamental formula for
quaternion multiplication
*i* 2 = *j* 2 = *k* 2 = *ijk* = −1
and cut it on a stone of this bridge
9 Likes

“Math is no different in this respect. In general, you need to practice with components or small aspects (ie. vector manipulation), and once you have an understanding of how you can manipulate that aspect, you can start imagining the steps you need to take to make something happen, just as with Lua.”

I know this was a long time ago but I just wanna bump this even more. It’s crucial to learn new concepts in order to become a better programmer, this also applies to learning math. Start simple, learn easy concepts and work yourself up. It doesn’t require that much math skill to be a good scripter. (You wouldn’t need extremely high math to make good games or assets anyways)