2D Circular Motion
Just a quick post today about implementing circular motion in a 2D environment.
centerX = <center X coordinate>;
centerY = <center Y coordinate>;
radius = <radius of circle>;
speed = <time to complete full circle in seconds>
speedScale = (0.001 * 2 * Math.PI) / speed;
angle = <game time in milliseconds> * speedScale;
xCoord = centerX + sin(angle) * radius;
yCoord = centerY + cos(angle) * radius;
position = new Vector2(xCoord, yCoord);
- Set centerX and centerY to the center of the circle (coordinates that you want your entity to move around)
- Set the radius of the circle
- Set the speed at which the entity should complete a full circle in seconds
- Set the position of the entity to the results of the speedscale, x-translation, and y-translation
As time goes from 0 to 2*Pi and back to 0, the motion will complete a full circle.
August 27, 2014 @ 4:20 am
Good sample. Thanks