# Mathematics in game making

65 replies to this topic

### #21 Marius

Marius

GMC Member

• GMC Member
• 203 posts

Posted 12 July 2011 - 09:53 AM

Well, there are some situations where multiplication is indispensible. For example, if you want to make something that pulsates, you might want to write image_xscale = 1 + 0.2 * sin(current_time / 700). In that case, you need to know about multiplication, and in addition knowing that sine gives you a wave. You also would be limited if you didn't know anything about manipulation graphs. It seems easy, but not everyone would recognize what a, b and c does in the expression a + b * sin(x / c).

Something like that, but I can't think of a more advanced example.
• 0

### #22 @Alex@

@Alex@

Retired GMC Reviewer

• Reviewer
• 3075 posts
• Version:Unknown

Posted 12 July 2011 - 02:08 PM

I'll create a custom banner for you saying "I PWNED IMP" if you can define "arctangent" without using or defining "tangent". (Note to those math-heads here who will no doubt find an obscure answer: I'm only asking Marius public/style_emoticons/default/tongue.gif And Taylor expansions don't count, either!).

Damm, I wanted that banner, wait if I use Maclaurin expansion instead would that count even if it's just really a modified Taylor expansion?

On the topic at hand, I don't think "advanced mathematics" is really all that neccasary but I find things like approximating square/triangle/sawtooth waves quite useful while the ability to manipulate matrices has been invaluable to a little level edittor project I have going. Simspons Rule to approximate an integral has also been quite useful for various applications such as finding the centre of mass. Sure it isn't needed but it is very useful to know how to do some of these things.

• 0

### #23 xshortguy

xshortguy

GMC Member

• Global Moderators
• 4186 posts
• Version:GM:Studio

Posted 12 July 2011 - 02:48 PM

One can easily define arctangent without first defining tangent in the following manner:

$\arctan(x) = \int_{0}^{x} \frac{1}{1 + t^2} \; dt$

This method of defining a function isn't uncommon, as there are many other famous functions defined in terms of integrals. Elliptic integrals come to mind here.
• 0

### #24 Marius

Marius

GMC Member

• GMC Member
• 203 posts

Posted 12 July 2011 - 03:25 PM

I don't think Maclaurin series would be accepted since it more of a subset of Taylor series... it's a bit like if someone asks you for a number that's not greater or equal to zero and you give them 2 and argue that it's strictly greater than zero...
How about letting it be defined as follows:
For any a,b in R such that a > 0, let arctan(b/a) be the acute angle between the vector (a, and the x-axis. I think that did the trick. Also, the challenge sounded almost a bit cocky; I can easily say that arctan(x) is the inverse function of sin(x)/cos(x). Nothing wrong with that definition and I don't have to define the tangent, I can just use sin/cos everywhere, but somehow, I have a feeling you wouldn't have accepted that...

On topic: Alex, could you tell me more about you using matrices in a level editor? That sounds interesting. How much have you needed Simpson's rule? Most of the time when I do things that seem to require integrals (mostly when dealing with acceleration), I just add some value to the sum each time. Using discrete sums like that can give quite large inaccuracies, but if it just looks smooth anyway and you are aware that there is a deviation, then there is rarely a problem with not using the accurate value.

EDIT: About half of the content in the last three posts have been about defining arctan, so before this leads us further off-topic, maybe you could just give me that banner so we can be done with it?

Edited by Marius, 12 July 2011 - 03:35 PM.

• 0

### #25 xshortguy

xshortguy

GMC Member

• Global Moderators
• 4186 posts
• Version:GM:Studio

Posted 12 July 2011 - 03:27 PM

I can easily say that arctan(x) is the inverse function of sin(x)/cos(x).

You have to be careful about this. tan(x) actually doesn't have an inverse function, since it isn't one-to-one. Same with sin(x) and cos(x). That's why the domains have to be restricted.
• 0

### #26 @Alex@

@Alex@

Retired GMC Reviewer

• Reviewer
• 3075 posts
• Version:Unknown

Posted 12 July 2011 - 04:01 PM

I've used Matrices in particular for rotating sections, letting a user select part of a level then an angle to rotate it by, although I suppose you could do this without knowing that you are using matrices by simplifing the method down to a simple series of sine and cosine functions. That is the most practial application though I've done other things like allowing the user to add to parts of a level together to form a new one from the values stored in Matrices as more of an experiment than anything. As for simpsons rule you only really get incorrect results when dealing with function of a degree greater than 4 which I almost never use. Meaning as long as I don't go above x^4 as an argument I get accurate results and if I could be bothered working out a script to differeniate the same function I couId compute the error. Ihave used it for finding the centre of mass and inertia for an object and for finding work done in compressing a spring ( Since the force is variable). I suppose I should just have easily have chosen values for the equations but then I would have to choose approiate approximate values for each unique object, having a script to calculate it just seems easier in the long run.
• 0

### #27 chaz13

chaz13

GMC Member

• GMC Member
• 3831 posts
• Version:Unknown

Posted 12 July 2011 - 04:16 PM

I would agree that any undergraduate mathematics is all very applicable to game development, but when you get to pure maths at degree level it's so abstract that very little of it has any real use; nevermind in game development.

Edited by chaz13, 12 July 2011 - 04:17 PM.

• 0

### #28 IceMetalPunk

IceMetalPunk

InfiniteIMPerfection

• Retired Staff
• 9260 posts
• Version:Unknown

Posted 12 July 2011 - 04:19 PM

I don't think Maclaurin series would be accepted since it more of a subset of Taylor series... it's a bit like if someone asks you for a number that's not greater or equal to zero and you give them 2 and argue that it's strictly greater than zero...
How about letting it be defined as follows:
For any a,b in R such that a > 0, let arctan(b/a) be the acute angle between the vector (a, and the x-axis. I think that did the trick. Also, the challenge sounded almost a bit cocky; I can easily say that arctan(x) is the inverse function of sin(x)/cos(x). Nothing wrong with that definition and I don't have to define the tangent, I can just use sin/cos everywhere, but somehow, I have a feeling you wouldn't have accepted that...

Such a simple answer, I should have expected it.

Oh, well, I'm a man of my word. Enjoy your banner: You may link it to your pwning post if you like .

-IMP
• 1

### #29 Gamer3D

Gamer3D

Human* me = this;

• GMC Member
• 1590 posts
• Version:GM8.1

Posted 12 July 2011 - 09:43 PM

Alternately, define it as the integral of [ tex ]\frac{1}{1 + x^2}[ /tex ]. NVM. Someone beat me to it.

Anyway, about the original topic of discussion: it depends on how you define mathematics. I'll start at the simplest mathematical property that comes to mind and move up.
• I'd find it hard (very close to impossible) to make any game without utilizing an equivalence relation. Even rock-paper-scissors and "Push a button to win" use this. Technically, you can make any game (Crysis included) using only this. It would require more storage space than anyone could possibly consider reasonable, but it's possible.
• Adding addition simplifies many of the games that were possible in the previous category. Same goes for subtraction, multiplication, division.
• Adding a square root allows me to easily use the quadratic formula. Now it's easy to make my bots aim at linearly moving targets.
• While we're at it, let's add in exponentiation and logarithms. Now it's easy to raise any number to any power (So I can do proper fog, exposure, and gradual multiplicative effects).
• Now let's add trigonometric functions. Sine, cosine, and tangent let me rotate things smoothly. The inverse functions allow me to put my data into a form that humans can understand better.
• Let's make a jump to complex numbers. Now I can find roots for ANY polynomial. I'll use this to make my bots aim perfectly at falling targets.
• Hmm... Turns out that from nearly the beginning, I've been using vectors. Cool.
• But wait! I could rotate groups of vectors more quickly by pre-calculating a rotation matrix. (Same goes for scaling, translation, skewing, etc)
• Hmmm. I have rotations, but I want to be able to combine them. I'll use quaternions.

The list goes on, but my point is that the more "advanced" the mathematics, the simpler and better you can make your game engine if you use it right.

The simplest game engine I've ever made used vectors, matrices, and square roots. I am reasonably sure that I avoided explicit calls to trigonometric functions, although I cannot be sure without checking. Nevermind. I think I used trigonometry for the skeletal animation, although I could safely remove that without affecting the rest of the engine.

So yeah. The more math you can use, the better you can make your game engine. Also, it's practically impossible (if not completely so) to avoid mathematics. Making a game about herding 3 pigeons into 2 birdhouses? The pigeonhole principle says that at least one birdhouse will have 2 pigeons in it. Making a game about petting tribbles? You can't comb a hairy sphere.

Edited by Gamer3D, 13 July 2011 - 06:10 AM.

• 0

### #30 Marius

Marius

GMC Member

• GMC Member
• 203 posts

Posted 14 July 2011 - 01:55 PM

IMP, I don't really need to brag about it in my signature. It's enough that you commend me for being right now let's just take the experience with us and be friends ^^

I would agree that any undergraduate mathematics is all very applicable to game development, but when you get to pure maths at degree level it's so abstract that very little of it has any real use; nevermind in game development.

That reminds me of a quote by Nikolai Lobachevsky. Paraphrased, it goes something like "Every abstract concept in mathematics can somehow be applied to the real world". Though I don't really think he was talking about maths in game development...
• 0

### #31 numbers

numbers

GMC Member

• New Member
• 460 posts

Posted 15 July 2011 - 07:16 AM

Saying you don't need math to make a game is like saying that you don't need to know physics to fly in an airplane. You personally might not need it, but you're kidding yourself if you think that you could be in the situation you're in without it. The only reason you think it's unnecessary is because it's being handled for you. Ever tried designing AI for a heavily puzzle based game? Maths galore most of the time. Physics, graphical rendering, all of it reduces to mathematics. When you're doing things on a machine that stores everything in numbers, math is indispensable. If you're using GameMaker, will you need advanced math for all types of games? Probably not, but that's because GM handles a lot of things for you, but many types of games would still need that math. Are you going to need higher level math for all games? No. But if you really want to push the limit in what you can do as a game developer, then yeah, you'll probably need that higher math fairly often.
• 0

### #32 paul23

paul23

GMC Member

• Global Moderators
• 3367 posts
• Version:GM8

Posted 17 July 2011 - 08:35 PM

I would agree that any undergraduate mathematics is all very applicable to game development, but when you get to pure maths at degree level it's so abstract that very little of it has any real use; nevermind in game development.

There is no "pure math at degree level"..

Mathematics is such a broad field that you'll always use it, yet you can't really name it when you use it. During my study (applied physics, aerospace engineering) I've had my fair share of mathematics. A lot I have never used outside the theoritical scope (2nd order differential equations come to mind), many parts I can't ever remember anymore, but the good thing about mathematics is that it can be considered a toolbox: the more you know about mathematics the broader your toolbox is. And the higher the chance that when you run into a problem you have the perfect tool for the problem.

Apart from the obvious cases like linear transformations (very interesting to see btw, how you can transform images with matrix manipulations), often you'll use things without realizing.

The other day I wanted to write an "xp" system. I wanted it to have a soft level cap (unfeasable high gains needed to grow levels). I made it so that the awarded xp was limited in a continous curve. Because my mathematics knowlenge I knew how to tackle this problem, and knew the steps needed to solve the problem.
• 0

### #33 IceMetalPunk

IceMetalPunk

InfiniteIMPerfection

• Retired Staff
• 9260 posts
• Version:Unknown

Posted 17 July 2011 - 10:09 PM

A lot I have never used outside the theoritical scope (2nd order differential equations come to mind)

You've really never worked with second-order differentials in aerospace engineering? No harmonic motion? Or does that not count since you usually work with the trigonometric solutions anyway?

-IMP
• 0

### #34 coco13

coco13

GMC Member

• GMC Member
• 201 posts

Posted 18 July 2011 - 03:11 AM

Haha everyone at my school says "We dont need to use any of this math so why are we learning it." They just dont make games. I use all the math i use all the time. Yes the main type mathematics that are used are trigonometry. This is pretty much THE math you should learn if you want to make good games. I remember i had always wanted to learn what Sine Cosine and Tangent did because they are used for 3d cameras. Im glad i took trigonometry this summer. =D
• 0

### #35 paul23

paul23

GMC Member

• Global Moderators
• 3367 posts
• Version:GM8

Posted 18 July 2011 - 07:09 AM

A lot I have never used outside the theoritical scope (2nd order differential equations come to mind)

You've really never worked with second-order differentials in aerospace engineering? No harmonic motion? Or does that not count since you usually work with the trigonometric solutions anyway?

-IMP

Ah yes that's when I used it, wondering why I didn't remember.

Well it strengthen my points only further, that mathematics is often used without realising.
• 0

### #36 Marius

Marius

GMC Member

• GMC Member
• 203 posts

Posted 18 July 2011 - 01:03 PM

Another thing to think about; suppose you have made a very complex algorithm for doing something. It seems to work in every case, but you're not sure. How would you make sure it works? Has anyone ever had a situation where they actually did a formal proof to show that something worked?
• 0

### #37 @Alex@

@Alex@

Retired GMC Reviewer

• Reviewer
• 3075 posts
• Version:Unknown

Posted 18 July 2011 - 02:21 PM

I haven't done a formal proof for anything I've created myself... Mainly because I hate doing proofs that aren't really easy.
• 0

### #38 chaz13

chaz13

GMC Member

• GMC Member
• 3831 posts
• Version:Unknown

Posted 18 July 2011 - 05:10 PM

Another thing to think about; suppose you have made a very complex algorithm for doing something. It seems to work in every case, but you're not sure. How would you make sure it works? Has anyone ever had a situation where they actually did a formal proof to show that something worked?

It's usually self evident if it works or not- and it only usually needs to work for what you're applying it to, not necessarily for every single case. You usually only need proofs as evidence to other people a finding you've made works, which is a fairly different realm to applying mathematics to games (especially as most in game mathematics is already well proven maths being applied as opposed to new ideas).
• 0

### #39 xshortguy

xshortguy

GMC Member

• Global Moderators
• 4186 posts
• Version:GM:Studio

Posted 18 July 2011 - 05:26 PM

It's usually self evident if it works or not- and it only usually needs to work for what you're applying it to, not necessarily for every single case. You usually only need proofs as evidence to other people a finding you've made works, which is a fairly different realm to applying mathematics to games (especially as most in game mathematics is already well proven maths being applied as opposed to new ideas).

That's not necessarily true. Just because it spits out a number doesn't mean that the number is right. For example, consider a program that adds up very large numbers; this could be subject to overflow errors and give a wrong result. These types of problems often limit physicists in their calculations and requires them to be careful.
• 0

### #40 Mnementh

Mnementh

15151

• Retired Staff
• 6261 posts
• Version:GM:Studio

Posted 18 July 2011 - 05:53 PM

I've written several semi-formal proofs while developing or modifying algorithms. For example, I use a modified form of Dijkstra's Algorithm to identify the area that a player can move to in turn-based tactics games. For those unfamiliar with the algorithm: Dijkstra's Algorithm is "a graph search algorithm that solves the single-source shortest path problem for a graph with nonnegative edge path costs, producing a shortest path tree". It begins with an initial cell and expands a ring of cells around it in order of distance from the start cell, until a goal condition it met. For my use, the goal condition is met when there are no more cells closer than the maximum distance. The original algorithm may identify an initial shortest path to a particular cell, and then later identify an even shorter path. I realized, through observation of my implementation, that because of the nature of the graph I was working with, my algorithm's initial path would always be the shortest. When I say "nature of the graph", I mean the particular property of the graph in a turn-based tactics game: although units move from one cell to another, the cost of that movement is dependant only on the cell that the unit is moving to, i.e. every edge leading to a particular node has the same weight.

My proof attempted do demonstrate that, given a directed graph in which every edge leading to a particular node has the same weight, Dijkstra's algorithm has found a shortest path to a node when that node is added to the open list. To do that, I assumed the opposite: that for a given graph, there was a node that was visited twice, the second time with a shorter path than the first. Because every edge leading to that node has the same weight, the implication is that the node's new parent has a shorter path than the node's initial parent. However, one property of Dijkstra's Algorithm is that nodes are expanded in non-decreasing order of distance: and thus, if the second parent had a shorter path than the first, it would have been expanded before the first, leading to a contradiction.

var open, s, range;
open = argument0;
s = argument1;
range = argument2;

var i;
for (i = 0; i < ds_list_size(s.neightbors); i += 1) {
var neighbor;
neighbor = ds_list_find_value(s.neighbors, i);

if neighbor.g > s.g + neighbor.cost && s.g + neighbor.cost <= range {
neighbor.g = s.g + neighbor.cost;
neighbor.parent = s;
ds_priority_add_or_update(open_list, neighbor, neighbor.g); // I removed this line, because the algorithm will never update a cell.
}