-
atan2 help needed; confusing results
Hey, I've got 2 apps that I'm working on, both that use atan2 as a base. The first one seemed to be having a very slight problem, but it was very slight and seemed a very small "rounding issue", since the issue was marginal. However, in my second app, the problem is very clear.
When using atan2, I find the angle between two points, and use this angle to find the adjacent and opposite of the angle (the hypotenuse is the given "moveSpeed", which I have defined as 10). Here is the code.
Code:
var adjside = pointX-clip._x;
var oppside = pointY-clip._y;
var angle = Math.atan2(oppside, adjside)/Math.PI;
var moveX = Math.cos(angle)*moveSpeed;
var moveY = Math.sin(angle)*moveSpeed;
trace("x " +moveX)
trace("y " +moveY)
moveX += decX;
moveY += decY;
var moveX1 = Math.floor(moveX);
var moveY1 = Math.floor(moveY);
decX = moveX-moveX1;
decY = moveY-moveY1;
moveX = moveX1;
moveY = moveY1;
if(pointX > clip._x) {
clip._x += moveX;
}
if(pointX < clip._x) {
clip._x -= moveX;
}
if(pointY > clip._y) {
clip._y += moveY;
}
if(pointY < clip._y) {
clip._y += moveY;
}
After tracing moveX and moveY, I used these to solve, and found that they do in fact form a right triangle with movespeed. The x coordinate seems to move just fine, but the y coordinate, blast it, goes wack. I'll upload the swf in a zip.
If your suggestion is to make this change:
Code:
var adjside = pointX-clip._x;
var oppside = pointY-clip._y;
var angle = Math.atan2(oppside, adjside)/Math.PI;
var moveX = Math.cos(angle)*moveSpeed;
var moveY = Math.sin(angle)*moveSpeed;
trace("x " +moveX)
trace("y " +moveY)
moveX += decX;
moveY += decY;
var moveX1 = Math.floor(moveX);
var moveY1 = Math.floor(moveY);
decX = moveX-moveX1;
decY = moveY-moveY1;
moveX = moveX1;
moveY = moveY1;
clip._x += moveX;
clip._y += moveY;
Don't bother, because this doesn't work.
I don't fully understand what the atan2 function is supposed to do, I was just told to use it in this manner to find the angle. Any help is appreciated.
Nathan Gibson
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|