Realm Master Member Posts: 1971 From: USA Registered: 05152005 
*really incredibly titanic gimungous bigbird sized sigh* Well friggen nuts. This was supposed to be a really simple 2d physics test, but I can't even do the first part! Simply put I have two ojbects, ones in teh center, the other is wherever the mouse is. When I click, it creates an ojbect taht is supposed to zoom directly to the ojbect in the center, now all I need is an equasion to make that happen, right? right. So we all know (or should know) how to find a 2d slope/angle, right? easy. y1  y2 / x1  x2 = angle/slope (those are supposed to be sub 1's and 2's) Yay!!! That SHOULD work, correct! And then ot get it sos I can goes like thiss: x = x + vectorx and y = y + vectory should send my little object straing towards the center thingy. to get my vectorx and vectory I do the following i take the angle/slope and do this. vectorx = Cos(angle/slope) * ojbectspeed (Even If I got Cos and Sin mixed up there, I tired it both ways in my program and found the correct way but it still didn't work) So... we have.... y1  y2
x = x + vectorx In theory, Perfectomundo, so...
?!?!?! 


HanClinto Administrator Posts: 1828 From: Indiana Registered: 10112004 
What about it is not working? I'm not entirely sure how you're getting your angle, but you need to be careful when dealing with degrees and radians. You may already know this (so if I'm repeating stuff you already know, I apologise), but computers expect sin() and cos() to have radians as inputs. The conversion ratio between degrees and radians is: 180 degrees = 3.14159... radians clint 

Realm Master Member Posts: 1971 From: USA Registered: 05152005 
Please Elaborate, this probelm is driving me nuts. What i do is aI have two ojbects, to find the "slope" between them (Picture 2d graphs, line graphs you know, you know the equastion.... x and y sub1 represent the coordinates of teh first ojbect, and likewise x and y sub2 represents the coordinates of the second ojbect.) What I get from my lidddle equasion is a number very similar to your 3.14159... its just that the things I'm using are behaving very strangly. No matter where I "place" my ojbect that is supposed to use the angle to hit the ojbect in the center, they go off to the right side of the screen, no matter what I do! I'm sorry if I'm so vauge, and I will post source if its that confusing (be warned, I'm a sloppy programmer) 


steveth45 Member Posts: 536 From: Eugene, OR, USA Registered: 08102005 
quote: This is where you went wrong. What you call the "angle" is actually a slope. They are very different things. You can't take a slope and use it like an angle. This problem of yours needs no sin/cos functions at all. To get a vector that goes from point 2 to point 1, this is it: (x1  x2, y1  y2) Now, to scale this vector to a certain speed, you first need to normalize the vector, which makes it's length exactly 1, and then multiply it by the speed you want. Lets call our nonnormalized vector (a, b). length = square root( a^2 + b^2 ) normalized vector = (a / length, b / length) = (c, d) Now, just multiply that by the speed you want, (c * speed, d * speed). That will give you what you want, sort of. You'll need to take into account making the point stop when it reaches the center. Really the trick is to view the vector as a single unit that is capable of addition and subtraction with other vectors (of similar dimension) and multiplication and division with scalars (single values).  

Realm Master Member Posts: 1971 From: USA Registered: 05152005 
...THANK YOU!!! Well, its still Christmas morning, so I have to get back with the family, but thanks and I will look all that there math over after We're done. Thanks a ton... (and I don't need it to stop) 


MeneMene Member Posts: 1398 From: Fort Wayne, IN, USA Registered: 10232006 
What's the angle then?
Function fixed. [This message has been edited by MeneMene (edited December 28, 2006).] 

steveth45 Member Posts: 536 From: Eugene, OR, USA Registered: 08102005 
Well, the angle is not important for this problem, but it could be easily calculated. First of all, angles are relative. Most commonly, you could find the angle relative to the horizontal, or (1, 0) vector. Using the normalized vector (c, d), with the angle "theta" from the horizontal, imagine a right triangle where the hypotenuse (length of 1) is the normalized vector. We know the following: c = cos(theta) So, to find the angle (theta), you could do one of the following: theta = acos(c) Generally, these functions return the angle in radians (0  PI). Multiply by (180 / PI) to get the angle in degrees. The angle doesn't tell you if the slope is positive or negative. A vector with a slope of 1 and a vector with a slope of 1 will both calculate to a 45 degree angle from the horizontal. In games, acos and asin are processor expensive functions, that don't give you much more information than you already have. To find relative angles between vectors, you generally normalize the vectors and take the dot product. This gives you cos(angle). So, if the dot product equals 1, then the vectors have identical angles (0 degrees difference), if the dot product is 0, then the angles are orthogonal (90 degrees difference), and if it is 1, they are pointing in opposite directions (180 degrees difference). 1 to 1 is as easy a range to use as 0 to PI or 0 to 180.  