Messed up polar coordinates
BlitzMax Forums/BlitzMax Programming/Messed up polar coordinates
| ||
Im trying to use polar coordinates to make an object orbit around a central point, but the object keeps drifing in towards the centre...as far as i can see the code is fine and the object should remain static until i have altered T variable. Can anyone see whats wrong? (all values are double) For Local PingObject:TPing=EachIn PingObject_List X=PingObject.getX() Y=PingObject.getY() X1=X-384 Y1=Y-384 R=Sqr((X1*X1)+(Y1*Y1)) T=ATan2(Y1,X1) X1=R*Cos(T) Y1=R*Sin(T) X=X1+384 Y=Y1+384 PingObject.setXY(X, Y) Next |
| ||
Possibly rounding errors. Cos and Sin return numbers at very high decimal places and the computer needs to truncate at a certain place .I believe doubles truncate at 32 places, not 100% sure, but it does truncate the number resulting in small errors that can accumulate over time. What I do when using polar coordinates in a game is I always use polar on that object throughout the game and only convert to x,y for rendering. If you must use X,Y (a.k.a. cartesian coordiantes), then make R a constant, possibly a Distance field in TPing. then you replace R=Sqr((X1*X1)+(Y1*Y1)) with R=PingObject.getDistance() That way the distance never changes until you change it. |
| ||
The issue seems to be that it gets confused when the angle is 90 or -90 |
| ||
I still think you have a rounding issue somewhere. I tried some experimenting and 90 and -90 degrees are converted correctly. I tried incorporating your routine above into a test program, and everything behaves as expected. Are you sure that x and y within the TPing type are double? Here's the test program I made. |