MoveTowardsPoint2D: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
 
Line 1: Line 1:
===== Just the formula for now, no code examples (yet) =====
===== Just the formula for now, no code examples (yet) =====
object = thing to move, target = where to move
x<sub>1</sub>, y<sub>1</sub> = position of object to move<br />
x<sub>2</sub>, y<sub>2</sub> = position to move to<br />


direction = atan2(targetY-objectY, targetX - objectX)*180/PI;


objectx += cos(direction*PI/180)*movingSpeed;<br />
direction = atan2(y<sub>2</sub>-y<sub>1</sub>, x<sub>2</sub> - x<sub>1</sub>)*180/PI;
objecty += sin(direction*PI/180)*movingSpeed;
 
x<sub>1</sub> += cos(direction*PI/180)*movingSpeed;<br />
y<sub>1</sub> += sin(direction*PI/180)*movingSpeed;

Latest revision as of 12:56, 18 October 2011

Just the formula for now, no code examples (yet)

x1, y1 = position of object to move
x2, y2 = position to move to


direction = atan2(y2-y1, x2 - x1)*180/PI;

x1 += cos(direction*PI/180)*movingSpeed;
y1 += sin(direction*PI/180)*movingSpeed;