How would I actually apply these maths to programming?

18 replies to this topic

#1onyxthedog

Senior Member

• Members
• 467 posts

Posted 01 August 2008 - 01:28 PM

I have finally learned the Pythagorean theorem, Sine/Cosine, still working on Tangent/Cotangent(They just don't seem to click, I mean memorizing the formula), and am working on rise/run. I was wondering how do I actually apply these. For instance, with the Pythagorean theorem there seems to be a lot of things that have to be just right to find the distance between two objects. Why would I want to know the angle between to points, how would that be applicable, and why would I want to know the slope of a flat surface?
/* Perfect_day.c */
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........

#2starstutter

Senior Member

• Members
• 1039 posts

Posted 01 August 2008 - 02:44 PM

Let me apply these in a 2d context for how I always used them...

onyxthedog said:

For instance, with the Pythagorean theorem there seems to be a lot of things that have to be just right to find the distance between two objects.

Nope, unless you're talking about finding ways around walls, it measure the exact distance under any circumstance and any angle (it works in 3D too, just add Z)

Quote

Why would I want to know the angle between to points?
I always used this (sin/cos) in platform and overhead games. If your character has a gun (or wide variety of other scenerios), you can point the mouse anywhere and the character will always look at it. Very useful for any shooting game. Sin/Cos is also the way you get things to move easily in a circular motion (in 2D of course, maybe 3D), and it's suprisingly difficult to do without it.

Quote

and why would I want to know the slope of a flat surface?
I never had much reason to use this, but I started building a 2D physics engine (which I failed at) and this topic came up quite a bit. As a warning though using that for physics is a bit hackish.

If you need any of these formulas, I have them sitting here on my hard drive collecting dust. Need to do anything specific in your game? :)
(\__/)
(='.'=)
This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.

#3onyxthedog

Senior Member

• Members
• 467 posts

Posted 01 August 2008 - 02:52 PM

No, I just recently got into the parts of math that teach about the Pythagorean theorem, I already knew how to Sine and Cosine. Just wondering how they were applicable. I need to know the formula for Sine to degrees, so that I don't have to have built in look up table.

Define how you get the two sides needed for the Pythagorean theorem, what I mean is give an example please.

Thanks
/* Perfect_day.c */
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........

#4Reedbeta

DevMaster Staff

• 5340 posts
• LocationSanta Clara, CA

Posted 01 August 2008 - 04:28 PM

From the pythagorean theorem, the distance between any pair of points (x1, y1) and (x2, y2) is sqrt((x2 - x1)^2 + (y2 - y1)^2).

You can see this by drawing a right triangle with its hypotenuse going from (x1, y1) to (x2, y2), and its legs parallel to the x axis and the y axis. You see the length of the x-axis leg is just |x2 - x1| (absolute value because x2 might be < x1 or vice versa) and similarly, the length of the y-axis leg is just |y2 - y1|. So by the pythagorean theorem, you get the above formula for the length of the hypotenuse.

You see you can draw a right triangle this way for *any* pair of points, and so the formula always works for giving the distance between them.

As for the trigonometric functions, they pop up all over the place. Sin and cos are seen more often than tan. The other three (csc, sec, cot) are much less common; I wouldn't worry about memorizing them at this point.
reedbeta.com - developer blog, OpenGL demos, and other projects

#5onyxthedog

Senior Member

• Members
• 467 posts

Posted 01 August 2008 - 05:44 PM

So this would find the exact distance in 2 dimensional space using world space, not global space:
#include <iostream>
using namespace std;
//square root
int sqrt(int digit)
{
return digit * digit;
}

//finds distance between 2 coordinates in 2D space
int find_dist(int xO, int yO, int xT, int yT) //just the abbreviated x1, y1 and x2, y2
{
int xDist, yDist;
xDist = sqrt(xT - xO);
yDist = sqrt(yT-yO);
return xDist + yDist;
}

int main()
{
int x, y, xT, yT, dist;
cout << "Please enter x coordinate 1: ";
cin >> x;
cout << "Please enter y coordinate 1: ";
cin >> y;
cout << "Please enter x coordinate 2: ";
cin >> xT;
cout << "Please enter y coordinate 2: ";
cin >> yT;

dist = find_dist(x, y, xT, yT);
cout << "The distance between the two points is: " << dist <<".\n";
return 0;
}


/* Perfect_day.c */
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........

#6starstutter

Senior Member

• Members
• 1039 posts

Posted 01 August 2008 - 07:14 PM

onyxthedog said:


#include <iostream>

using namespace std;

//square root

int sqrt(int digit)

{

return digit * digit;

}

//finds distance between 2 coordinates in 2D space

int find_dist(int xO, int yO, int xT, int yT) //just the abbreviated x1, y1 and x2, y2

{

int xDist, yDist;

xDist = sqrt(xT - xO);

yDist = sqrt(yT-yO);

return xDist + yDist;

}



Unless I missed something along the way, that function would not work because it's missing the final step: the square root at the very end. Also you're kind of hurting yourself in the sense that the layout for this function is was more complicated than it needs to be.

This is the function quoted from my old engine:

float GetDistance(float X1, float Y1, float X2, float Y2)                   //this function gets the distance between 2 points in space

{

float dis = sqrt(    ((X1-X2)*(X1-X2)) + ((Y1-Y2)*(Y1-Y2))     );

return dis;

}



also to make things easier on yourself, always include this at the beginning of your program.

#include <cmath>

Although I guess you can learn to write the manual functions yourself (really that's the better habbit to get into).

Also, know that sqrt stands for square root, so the function multiplying x1 * x1 is a bit backwards. I guess it doesn't matter, but that's just a terminology issue.

In addition, I need to correct myself from earlier. Finding the angle between 2 points is not sin/cos, it's arc tangent. Sin/cos is used to express the rotation of the object. ie, you use arc tangent to find the angle between the character and the mouse, and then use sin/cos to make the character rotate (or even travel) to that direction.
(\__/)
(='.'=)
This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.

#7onyxthedog

Senior Member

• Members
• 467 posts

Posted 01 August 2008 - 07:22 PM

I knew that sqrt was in the cmath header, but I figured that since I was just writing something quick and dirty to see if what I thought the formula was is what it was. I fixed it by adding this to my find_dist function

return sqrt(xDist + yDist);



/* Perfect_day.c */
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........

#8starstutter

Senior Member

• Members
• 1039 posts

Posted 01 August 2008 - 07:30 PM

onyxthedog said:

I need to know the formula for Sine to degrees, so that I don't have to have built in look up table.
I don't really know what you mean by sine to degrees. Sin is a mathmatic function and not any fixed number (or really number at all for that matter).

I think you're saying is you want to learn to use sin/cos as functions to move an object in a circular motion (which I am guessing so I may be totally wrong :p).

But anyway, this is my function for moving an object in a circular path around a given point using a given angle (0-360):

float SinAngle(float Angle)										            //optimization, can combine the equasions into a variable and then just preform sin, cos in one function

{

return sin((Angle * -57.2957764091904) * (3.1415926259712 / 180))	;   //sin and cosine functions, they make using circles and much of the rotating geometry possible

}

float CosAngle(float Angle)

{

return cos((Angle * -57.2957764091904) * (3.1415926259712 / 180))	;

}


that was the generic function for converting an angle to a location. In context, imagine you have a character with 2 floating balls for hands (like rayman lol). You need to rotate the character any direction and always have the hands remaining to his side. Here would be the formula for the right hand (from a top down view).


float2 handPoint;

handPoint.x = characterLocationX + radius * math->CosAngle(angle);

handPoint.y = characterLocationY + radius * math->SinAngle(angle);



In this case, the radius would mean how far out you want the hand to reach. 1 would be right next to him, 100 would be him reaching across a football field. But in either case, any way he rotated the hand would stay perfectly aligned.

Maybe you don't need all this now but you can keep it for future reference :)
(\__/)
(='.'=)
This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.

#9starstutter

Senior Member

• Members
• 1039 posts

Posted 01 August 2008 - 07:31 PM

onyxthedog said:


return sqrt(xDist + yDist);


yup, that should do it
(\__/)
(='.'=)
This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.

#10onyxthedog

Senior Member

• Members
• 467 posts

Posted 01 August 2008 - 07:35 PM

Okay, say, I have the Sine of Theta, then I have to use a look up table to find the angle of Theta. Such as the Sine of Theta = .5 = 30 degrees
/* Perfect_day.c */
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........

#11starstutter

Senior Member

• Members
• 1039 posts

Posted 01 August 2008 - 07:39 PM

I'm really not sure you ever need a lookup table. I've certainly never had to use one. Maybe I'm not understanding correctly. Is Theta a meaningful number or a generic variable?
(\__/)
(='.'=)
This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.

#12onyxthedog

Senior Member

• Members
• 467 posts

Posted 01 August 2008 - 07:43 PM

It is the generic variable used to represent an angle. Sine^-1, it is used to convert the sine of an angle to degrees.

P.S. I remember its name, inverse sine is what it is called.
/* Perfect_day.c */
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........

#13starstutter

Senior Member

• Members
• 1039 posts

Posted 01 August 2008 - 07:45 PM

OH OH! ok, I get what you're saying.

No you defintiely don't need a lookup table for that unless you want to display a lot of possiblities to the screen. That would waste awhole lot of memory.

To answer your question though, Sine of Theta to degrees will not be a meaningful number. To measure degrees you have to have another variable to compar it to. Not to mention I really wouldn't be sure how to convert something like that.

Sin simply outputs a wave structure that is useful for many things, but one function alone would not find a "degree" that really means anything useful.

EDIT: ok, from the latest post I get what you're fully saying now. But I really don't know how to do that, but at the same time I've never run across a situation where I had to do that.
(\__/)
(='.'=)
This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.

#14Mephs

Member

• Members
• 34 posts

Posted 01 August 2008 - 08:41 PM

I think the pyth theorem can be applied to almost any graphical concept in your game's development, if not everything. Or at least everything becomes a derivative of it.

For example: I created a Camera object that maintains a pivotal location at all times by focusing an invisible node that actually controls the camera's movement (the camera itself isn't controlled). I probably could apply algorithms to direct camera movement but I've found this to be WAY easier for my game outline. Now all I have to do to control camera in any way is throw a short command like Zoom(units) or Pivot(degrees), and likewise it'll increase the distance the camera maintains from this center focal point, or pivot around it by the specified angle in degrees (the function converts to radians, but this is much easier to conceptualize with).

Pyth and trig has been the most useful way to figure out how anything can move in 3d space so far, because it's the most simple algorithm to use or derive from:

Here's the diagram I drew up during development of the PivotCam code. It's really simple, and a good idea how trig applies to a concept like the above. One thing I had not considered originally was that the X/Z shifting of the camera (which actually doesn't move the camera at all, it moves the focal point and then maintains the camera's relationship to it!). The X/Z shifting was based off a tangent line to the cirlce, since I hadn't taken into mind that to shift the view it would need to move sideways in relation to it's current position, and at different rotation angles that didn't automatically resolve itself.

Anyway I hope this gives you a better idea of why Pyth and trig in general is really useful for game applications. Physics is a whole nother can of worms, thank god there's already engines that do that stuff for you ;p

http://www.sjfweb.net/Iso3.avi

The PivotCam in action in this short video, completely mouse controlled (except shift+drag tilts, everything else is based off left click/right click dragging or zooming with the mouse wheel).

The main point of explaining it this way just to show that by drawing a picture of what you want done can often help solve the problem if you use that visualization to apply your math :)

#15Kenneth Gorking

Senior Member

• Members
• 939 posts

Posted 02 August 2008 - 10:49 AM

onyxthedog said:

I knew that sqrt was in the cmath header, but I figured that since I was just writing something quick and dirty to see if what I thought the formula was is what it was. I fixed it by adding this to my find_dist function

return sqrt(xDist + yDist);


It's not as simple as just doubling some value. Have a look at http://mathworld.wol...SquareRoot.html

onyxthedog said:

Okay, say, I have the Sine of Theta, then I have to use a look up table to find the angle of Theta. Such as the Sine of Theta = .5 = 30 degrees
You would use asin to do that. Also, note that all the geometric functions work in radians, not degrees. They are easy to convert between though:



{

return degree * (3.14159265359f / 180.0f);

}

{

return radian * (180.0f / 3.14159265359f);

}



"Stupid bug! You go squish now!!" - Homer Simpson

#16onyxthedog

Senior Member

• Members
• 467 posts

Posted 03 August 2008 - 01:17 AM

I felt stupid when I was explaining my code to my dad yesterday (he always wants me to explain it, even though he knew just by looking at it what it did. I think he wants to make sure I understand it.) and realized that I had squared and squared root mixed up. I didn't realize that it worked in radians, thanks.I need to change my current sqrt to squared. Actually, why reinvent the wheel, I could just stop being stubborn and use cmath.
/* Perfect_day.c */
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........

#17monjardin

Senior Member

• Members
• 1033 posts

Posted 04 August 2008 - 07:45 PM

The standard C math header has a pow function. So, you get x squared with pow(x, 2). It's not really necessary in this case since x * x works fine ;), but it works with non-integer exponents though (e.g. pow(3, 0.5)).
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

#18onyxthedog

Senior Member

• Members
• 467 posts

Posted 04 August 2008 - 08:06 PM

Seriously, because x^0.5 is the formula for square roots. Sweet thanks.

P.S. I couldn't figure out how to do a square root function, or a square function that handles floats <= 1 && floats >= -1. Any help?
/* Perfect_day.c */
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........

#19Reedbeta

DevMaster Staff

• 5340 posts
• LocationSanta Clara, CA

Posted 04 August 2008 - 08:24 PM

What do you mean by "couldn't figure out how to do a square root function"? If you mean you couldn't figure out how to express the square root in terms of adds, multiplies, divides, etc. don't worry about it - you can't. There is no formula for square root except for taking the limit of an infinite series. That is exactly how the sqrt() function works - it calculates the first few terms of an infinite series, which is good enough within the limits of floating-point precision.

As for squaring floats in the -1 to 1 range, the square of x is always x*x, no matter what x is. There is nothing different about the -1 to 1 range. (You might have noticed that numbers outside that range get bigger when squared, while numbers inside the range get smaller. That's okay and it's just how squaring is.)
reedbeta.com - developer blog, OpenGL demos, and other projects

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users