Sign in to follow this  

Random Math Questions

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 smile.png ).

 

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.

 

 

 

About

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 smile.png

Edited by Alberth

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

I finally figured out the polynomials. I'm quitting at binomials though. I don't feel like it's all that relevant for simple games, and it's really hard, lol...

 

I got into trig. I finally know about the functions, and inverse functions, and basically how they work. You have the regular functions for finding a side, and inverse functions for finding an angle. Now I'm have trouble visualizing how to apply trig to a game..

 

I don't know how to get any information besides the distance between sprites in Construct 2, and that's quite simple, as there's functions for it..

 

The problem is that's only the hypotenuse. I don't know what to do to get any other point in the triangle, or other information.

 

Lets take the example below. To keep it simple everything is in degrees...

 

Things I don't know: How to get the angles in relation to the game (in construct sprites have an Angle member attached, collectible through sprite.Angle. Is this the angle for the corner that sprite is in?)

 

How do you get the x,y for the dot halfway between the man and the banana?

 

I really don't understand getting x,y for unknown points... anywhere in the canvas. The only points I can collect are where the sprites are, through member data.. Spire.X or Sprite.Y...

 

Can anyone teach me this?

 

nnpt01.jpg

Edited by JeremyB

Share this post


Link to post
Share on other sites

You don't need trigonometry for this. Lines are linear, so you can interpolate between the positions.

// Banana B at the lower left, human H at the top right, point M (middle) on the line in-between.
fract = 0.5
M.x = B.x + frac * (H.x - B.x)
M.y = B.y + frac * (H.y - B.y)

'frac' runs from 0.0 (position at B) to 1.0 (position at H). By picking a value in-between both, the M point moves along the line.

What happens, is that B is the base position. (H.x - B.x) is the horizontal distance, and (H.y - B.y) is the vertical distance. Both directions grow with increasing 'frac', such that their ratio stays the same. In that way, both lines "arrive" at H, at the same frac=1.0 value.

 

If you want it with angles, you may want to read post #5 above (http://www.gamedev.net/topic/671525-random-math-questions/?view=findpost&p=5250665), as that does essentially exactly that, except B and H are swapped.

 

Share this post


Link to post
Share on other sites
Google 2D vector maths. Best thing I ever studied. Extends to 3D easily as well.

Once you learn to start thinking in terms of vectors and their associated operations, problems like your man and banana become trivial.

Share this post


Link to post
Share on other sites

I'm gonna check out the vector 2d math. This stuff is kill'n me.. I would never have guessed that formula, and don't understand the explanation. I just don't think I'm smart enough for this game stuff. I keep trying to get better at it, but I just don't have the intelligence... I guess the only thing I can do is learn math, and hope it clicks in.. not sure... just learned a bunch of algebra, some trig, and it was no use... nothing I learned in the past 4 days applied here..

Share this post


Link to post
Share on other sites

I hope you will understand my explanation at some point in the (near) future.

Inventing these things is a few steps further, but it's like programming. Once you have seen and understood a solution, you store it in your mind, and can use it in another context.

 

If you want we can dig further down into more detail what it is that you are blocked on.

Share this post


Link to post
Share on other sites

Well, I'm just curious about the building blocks. I've spent hours on per-algebra, and some on algebra, and a quite a few on trigonometry learning the basics. Still, none of it had anything to do with this... What's going to make understand what you've said here. Have I skipped a math subject? Maybe I don't know enough about graphing in general. Where are the building blocks I need to climb to this understanding? I only know what adding to x and y does, and subtracting... Maybe that's it... I don't have enough knowledge in what the four arthritic operations do to the grid, or any one point. That would likely help.

 

When you add to x it goes right, subtract it goes left... So what about dividing and multiplying, I suppose that entirely depends on the integer answer... Where do I learn this crucial graphing knowledge? Is it linear algebra that is graphing?

Share this post


Link to post
Share on other sites

Linear algebra looks like general vector space transformations (a vector space is for example 2D or 3D), where you use matrices to do the transformations. Rotation is an example of a transformation.

Geometry http://en.wikipedia.org/wiki/Geometry is also close, in particular Euclidean geometry. It's mostly concerned with angles and triangles, at first sigh, so close, but not the right thing perhaps.

I think it's closest to elementary algebra, perhaps with a bit of geometry.

 

Note that pretty much everything on those wikipedia pages is about more complicated things than lines. A line is a linear equation (no x^2, or x^3, etc).

2D vector math as Aardvajk suggested should get you quite far.

 

 

 

I thought about your problem, trying to figure out how to explain my solution better.

Maybe you are thrown off by 2D ?

 

In 1D, the problem is as follows: You have a value u. How much do you need to add to u to get a value v? How much do you need to add to u to get halfway inbetween values u and v ?

The 2D problem is just a 1D problem in X direction, and a 1D problem in Y direction combined.

 

Another possibility is that you are thrown of by thinking in variables rather than values. In that case, the question would be better expressed as you have 1, how much do you need to add, to get 6? You much do you need to add to 1 to get 3.5? You may only use the numbers 1 and 6 in your answer (and "/ 2" in the second question).

 

Share this post


Link to post
Share on other sites

"I thought about your problem, trying to figure out how to explain my solution better.

Maybe you are thrown off by 2D ?

 

In 1D, the problem is as follows: You have a value u. How much do you need to add to u to get a value v? How much do you need to add to u to get halfway inbetween values u and v ?

The 2D problem is just a 1D problem in X direction, and a 1D problem in Y direction combined."

 

- What you said there is the right direction. I don't know enough about these mathematical computations. I understand this to simplest degree. You add to x you go right, subtract go left. I construct adding to y goes down, subtracting from y goes up... I can see how to mix these... add little to x, add a little to y, end up somewhere over there.. lol...  but I'm not sure about harder scenarios.. when you add in multiplications, and different equations.... Then there's doing this when you don't know the positions, and the sprites are in free space...

Share this post


Link to post
Share on other sites
> I don't know enough about these mathematical computations. I understand this to simplest degree.

There are three steps here. First step is translating a practical problem into a math problem. Second step is applying math to give a way to solve the converted problem. Third step is converting the solution back to the practical problem. Knowing math gives you a handle for the second step. The first step is practicing I think. Third step is usually quite easy, but maybe that's also practice.

You could consider trying to solve some line problems that people here post. Once you played a little with the problem look at how it gets solved. Not sure if it would work though, it may be hard to decide which problem are solvable for you.

> You add to x you go right, subtract go left. I construct adding to y goes down, subtracting from y goes up... I can see how to mix these... add little to x, add a little to y, end up somewhere over there.. lol...

Basically, I start from one position, and relative to that position I compute movement. It's vector math.


> but I'm not sure about harder scenarios.. when you add in multiplications,

I already added multiplications, / 2 is a multiplication of * 0.5. Multiplication works without problem, as long as you keep the ratios the same.

In the 1D case, when you go from u to v, you traveled on a part of it. You also know the total length. If you divide the former by the latter, you get a fraction of traveled distance. That fraction has to be the same in both X and Y direction. If you do that, you'll stay on the line.

It doesn't have to stay a fraction though. What happens if I travel twice as far in the same direction? What happens if I travel in the opposite direction? (in math, multiply distance by 2, or multiply by -1).
In all cases, you'll stay on the line that runs (with infinite length into both directions) through the B and H point.


> and different equations....

Trying to write a math MMORPG eh? ;)
One puzzle at a time :D


> Then there's doing this when you don't know the positions, and the sprites are in free space...

Euhm, did I ever tell you the values of u and v ?
Sprites typically don't float in free space, as your program knows exactly where they are.

But maybe you have something else in mind?

Share this post


Link to post
Share on other sites


Well, I'm just curious about the building blocks. I've spent hours on per-algebra, and some on algebra, and a quite a few on trigonometry learning the basics. Still, none of it had anything to do with this... What's going to make understand what you've said here. Have I skipped a math subject?

 

I don't think you've really skipped anything. Seems to me one of the problems is that the math books and things you've been reading aren't helping you understand how to apply the info to do what you want. It's a learning process.

 


I'm gonna check out the vector 2d math. This stuff is kill'n me.. I would never have guessed that formula, and don't understand the explanation. I just don't think I'm smart enough for this game stuff. I keep trying to get better at it, but I just don't have the intelligence... I guess the only thing I can do is learn math, and hope it clicks in.. not sure... just learned a bunch of algebra, some trig, and it was no use... nothing I learned in the past 4 days applied here..

 

Another potential problem is that you might be overwhelming yourself with trying to understand all the math at once. It took me several years in secondary school to wrap my head around these concepts. Like any practiced skill, it's only "easy" once you've done it a thousand times. Plus, if you're really trying and still don't understand it, it's not your fault. The job of any teacher or learning tool is to break down the concept. My personal experience of learning new things from the Internet is that it takes a lot more time since it isn't always broken down nicely and there's no one to really ask (except on forums). Even then, it's hard to explain it over text.

 

 

One last thing, have you checked out BSVino's Youtube videos? At the very least, they'll help you see how the math is used.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this