How would I actually apply these maths to programming?
#1
Posted 01 August 2008 - 01:28 PM
#include <arcade>
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........
#2
Posted 01 August 2008 - 02:44 PM
onyxthedog said:
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
Quote
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.
bunny also wants to fight spam: Click Here Bots!
#3
Posted 01 August 2008 - 02:52 PM
Define how you get the two sides needed for the Pythagorean theorem, what I mean is give an example please.
Thanks
#include <arcade>
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........
#4
Posted 01 August 2008 - 04:28 PM
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.
#5
Posted 01 August 2008 - 05:44 PM
#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;
}
#include <arcade>
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........
#6
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.
bunny also wants to fight spam: Click Here Bots!
#7
Posted 01 August 2008 - 07:22 PM
return sqrt(xDist + yDist);
#include <arcade>
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........
#8
Posted 01 August 2008 - 07:30 PM
onyxthedog said:
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.
bunny also wants to fight spam: Click Here Bots!
#9
Posted 01 August 2008 - 07:31 PM
onyxthedog said:
return sqrt(xDist + yDist);
(='.'=) This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.
bunny also wants to fight spam: Click Here Bots!
#10
Posted 01 August 2008 - 07:35 PM
#include <arcade>
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........
#11
Posted 01 August 2008 - 07:39 PM
(='.'=) This is Bunny. Copy and paste bunny into
(")_(") your signature to help him gain world domination.
bunny also wants to fight spam: Click Here Bots!
#12
Posted 01 August 2008 - 07:43 PM
P.S. I remember its name, inverse sine is what it is called.
#include <arcade>
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........
#13
Posted 01 August 2008 - 07:45 PM
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.
bunny also wants to fight spam: Click Here Bots!
#14
Posted 01 August 2008 - 08:41 PM
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 :)
#15
Posted 02 August 2008 - 10:49 AM
onyxthedog said:
return sqrt(xDist + yDist);
onyxthedog said:
float ToRadian(float degree)
{
return degree * (3.14159265359f / 180.0f);
}
float ToDegree(float radian)
{
return radian * (180.0f / 3.14159265359f);
}
#16
Posted 03 August 2008 - 01:17 AM
#include <arcade>
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........
#18
Posted 04 August 2008 - 08:06 PM
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?
#include <arcade>
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........
#19
Posted 04 August 2008 - 08:24 PM
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.)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users












