# Random Math Questions

This topic is 1219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,

I've been reading some information on cos, sin, and tan. I'm just wondering what the practical uses are. They're not clear on many of the sites I'm reading yet. Just saying that it's hypotenuse/opposite, or what have you. I'm trying to see what the practical use of these are. The number given for an answer is usually a small random decimal, that doesn't seem to be attached to the triangle anywhere.

I'm also wondering about location picking. How do I learn point picking, and moving in odd directions. Say I want a sprite to move from point a to b, but do it in a half circle. Where can I learn to graph/calculate these. I guess two things are keeping me from good game designing beyond event based stuff. Math knowledge, and practical use. I'm not even sure where to start when it come to picking points, getting there in certain ways. I'd be useless in designing anything but a simple platformer, rpg, or a event/menu game...

I don't mind that too much, but really, who doesn't want to get better at what they don't know?

The funny thing is I've been here off and on for like 10 years, lol. Sad to say most my programming efforts have crashed and burned, but now I've learned to accept I'm not that great at it... except I've got a nice web project coming along, and a game in construct (love this engine, great for people like me who suffer the lesser intelligence of the og's around here, lol)

##### Share on other sites

Trigonometry is pretty useful when doing vector math. For starters, it's intrinsically related to moving in circles, and thus rotation. Want to point a turret at a target? Use trigonometry to find the angle of the direction vector. Want to move your point along the circumference of a circle? Trigonometry.

Sine waves get used all over the place: in audio, in procedural generation, in easing curves.

By point picking, I assume you mean when you click on a point on the screen and translate it to a point in the game world? For a 2D game, one way to handle that is to translate it from the screen coordinates to the world coordinates via a camera function that keeps track of where the in-game camera is and translates that to an in-world coordinate, which is basically just addition and subtraction.

Then you check which sprite is at that world location. Either go through the list of sprites comparing bounding boxes or alpha masks, draw an interface frame that paints the sprites by z-order, or whatever else works.

For a more complex 2D world or a fully 3D world this may not be sufficient, but the general idea is the same: translate screen coordinates to a ray, figure out what that ray intersects. Trigonometry by itself may not be the most efficient way to do this: you'll probably end up using translation matrixes.

Now, you don't need to be an expert in trig (or matrixes) to make a game. You can get a lot done with just addition and subtraction. But if you're trying to code anything that has rotation, it's probably going to come up.

##### Share on other sites

Sweet, thanks for the advice. I plan on going through as many of the new Boston's math videos as I can, starting with basic algebra. Math is nowhere near my strong point, but have been progressing because of my interests and getting more serious with life, lol. I'm talking strictly from the point of 2D in this thread. I think I have some questions though...

"Trigonometry is pretty useful when doing vector math. For starters, it's intrinsically related to moving in circles, and thus rotation. Want to point a turret at a target? Use trigonometry to find the angle of the direction vector."

This sounds interesting. I'll have to take notes on angel of the direction vector... I've only peaked in vector math. I'm only using construct 2, so pointing objects at objects is kind of easy, but other math is a little more difficult.

"By point picking, I assume you mean when you click on a point on the screen and translate it to a point in the game world?"

Actually any point... Like I wanted for one situation a random point up to 100px from an enemy. The formula given to me to use was this..

pointX = enemyUnit.X + cos(random(360)) * random(100)

pointY = enemyUnit.X + sin(random(360)) * random(100)

I would have never guessed that. I don't know what cos and sin does to an angle, and how it relates to the current x,y positions, and then why you would choose to randomly multiply the random distance... why multiply over add subtract or divide? lol, see what I mean.

Or say you have a sprite in x,y position... I wouldn't know how to just randomly move it to any point, at all.. lol... I just know how to do mouse movement, by click, and stuff like that, cuz you're gather the new point.. and of course, direction key stuff.... but nothing special..

This limitation keeps me stuck at:

basic tactical turn games

rpg

point click/event games

stuff like that... I would be useless to space shooters, advanced platformers, actions games.... anything more difficult..

Edited by JeremyB

##### Share on other sites

Cos returns the X value associated with a angle, Sin the Y value. You multiply sin and cos by your radius for a circle with a radius other then 1. The code listed takes a random angle (random(0,360), splits it into a x and y value. (The sine and cosine), then multiples it by a random number to make the distance random.

##### Share on other sites

sin and cos express length relations from an angle. Look at the formula and picture at https://en.wikipedia.org/wiki/Trigonometry#Overview

It says

sin A = a / c

Assume you know the angle A (say 45 degrees), and c (straight distance to B from A), you can compute a (height of B relative to A).

What may be throwing you off, is that you should see "sin A" as just another number (with a weird value). Let's add "s" to make this clear.

s = sin A
s = a / c


I only replaced "sin A" by "s" (and added a "s = sin A" so I don't loose the relation). Now if you look at the top line, we know "A", so we can compute "sin A", and we know "s".

Since 45 degrees is a well known angle, I know s is "sqrt(2)/2", ie just a number, only slightly more weird than 1, 4, 6, or 3.18.

Next is the bottom line. We know "s" (from the first line), we know "c" (let's say 100), so only "a" is not known, and this is the length of the line directly below B, ie the height we are looking for! So the line "s = a / c" must be shuffled around algebraically to read "a = ....", where "...." must only contain s and c. Since we know both s and c, we can compute the value of ..., and since "a = ...", we then also know the value of a.

This is where elementary algebra comes in. For this case, it says we should multiply both sides with c. We get "c * s  = c * (a / c)", which is equal to "c * s = a", which is "a = s * c" (reading backwards). Apparently, ... above is "c * s", which is 100 * sqrt(2)/2, or 50 * sqrt(2), or about 50*1.4 (sqrt(2) is about 1.41), which is about 70 (computers can give you a more precise answer ).

As you can see, we are not so worried about the precise value of "sin A". It's just a value that gives the ratio between c and a for a given angle A.

Now try to compute the height of an airplane that you see at 60 degrees at 15km distance.

Another question is horizontal distance, how far do I have to walk such that I am directly under the plane (for simplicity, let's assume it doesn't move while I walk, or just flies tight circles, so it stays at position B). Hint: "sin A" won't work here.

Actually any point... Like I wanted for one situation a random point up to 100px from an enemy. The formula given to me to use was this..

pointX = enemyUnit.X + cos(random(360)) * random(100)
pointY = enemyUnit.X + sin(random(360)) * random(100)

As was pointed out in the other thread already, this won't work. The math is alright, it's just that "random" produces euhm... random results.

Let me rewrite to make it more clear.

angleA = random(360)
angleB = random(360)
distanceA = random(100)
distanceB = random(100)
pointX = enemyUnit.X + cos(angleA) * distanceA
pointY = enemyUnit.X + sin(angleB) * distanceB

As you can see in the first 2 lines, "random(360)" is performed twice. Each call gives a different answer. So, angleA may be "35", and angleB can be "243".

Similarly, distanceA may be 81, and distanceB may be 1.

So your X calculation uses a different angle and distance than your Y calculation.

It is probably not so bad here, as random movement is random movement, no matter how you derive it, but in other cases it may be more problematic.

To make it more consistent, draw one random angle, and one random distance, and use the same angle and distance both for X and Y calculation.

Also, check the documentation of your sin and cos functions. Some languages take degrees (which is what happens above), other languages use radians (in which case, divide the drawn angle by (2 * PI) ).

Degrees and radians both express angles of rotation, they just differ in scale. Degrees use 360 for one full rotation, while radians use about 6.28 (2 * PI). Radians make more sense from a theoretic point of view, for us they are just a little more weird than "nice" degree values

Edited by Alberth

##### Share on other sites

trig functions relate the angles of a right triangle to the lengths of its sides.  so anywhere you can setup a right triangle, you can get the angles from the sides, and vica versa. and the 2d direction vector from any point A to any point B is the hypotenuse of a right triangle.  so trig lets you get the angle to some point given its direction vector, or the direction vector given the angle. and the same concept extends to 3D.

so if i'm at point A, and the badguy is at point B, the vector from point A's (x,y) to point B's (x,y) can be used along with trig to determine the angle - IE the amount to turn to face the badguy.

##### Share on other sites

One useful way to think of those trig functions, is that they and their inverses can be used to transform coordinates between cartesian coordinates and polar coordinates. (different trig functions depending which one is source and which is dest, and which axis you want and so on)

And the reason youd do the "transformation" is that some things are easier in one coordinate system than another (theres a lot of transformations between coord systems in games).

Eg to move in circle at constant velocity, you dont do that with (x,y). You work in "polar coordinates" and thus store the angle. Now the angle is linearly proportional to the velocity, so moving at constant velocity is simple addition! (and then convert back to cartesian x,y coords to render the sprite or whatever)

##### Share on other sites

This is a lot of information, but very helpful. I'm going to have to save some, and come back to it. I've got a game plan for learning math. Sadly I'm a bit behind this. I'm doing per-algebra and got hung up on some binomial stuff... I went back and learned everything I could from various sites about polynomials. Right now my next lesson is multiplying two termed polynomials where the terms aren't the same size. A crash course on that would be great..

like:

(2x^2 + 3x + 5)(x^2 + 6)

##### Share on other sites

for each term x in polynomial a

for each term y in polynomial b

result = result + i*j

Edited by Waterlimon

##### Share on other sites

This is a lot of information, but very helpful. I'm going to have to save some, and come back to it. I've got a game plan for learning math. Sadly I'm a bit behind this. I'm doing per-algebra and got hung up on some binomial stuff... I went back and learned everything I could from various sites about polynomials. Right now my next lesson is multiplying two termed polynomials where the terms aren't the same size. A crash course on that would be great..

like:

(2x^2 + 3x + 5)(x^2 + 6)

Two methods:
(1) Re-learn how you do multiplication of numbers, where you think of "10" as being "x".
            2    3    5
*  1    0    6
--------------
12   18   30
0    0    0
2    3    5
---------------------
2    3   17   18   30

So your polynomial is 2x^4 + 3x^3 + 17x^2 + 18x + 30

(2) Make a table with the terms of one polynomial as headings for the columns and the terms of the other polynomial as headings for the rows:
          |  2x^2    3x     5
----------+-------------------
x^2       |
6         |

Now fill in each entry in the table with the product of the headings of its row and column:
          |  2x^2    3x     5
----------+-------------------
x^2       |  2x^4  3x^3  5x^2
6         | 12x^2   18x    30

Finally, group the resulting terms by power of x: 2x^4 + 3x^3 + 17x^2 + 18x + 30 Edited by Álvaro

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634051
• Total Posts
3015247
×