I’m sure many of you game devs out there have struggled with physics calculations in your game. Today, I’m going to discuss rotating around an object. Personally, I had the same problem and looking at the complex Math explanations didn’t help me. This tutorial is for all of you out there and it applies to any programming language. Here’s the code:

rotations by eis – kino
The sprites, the position (x and y) of both star1 and star2
star1 ={sprite=2, x = 58, y = 58}
star2 ={sprite=3, x = 0, y = 0}
degrees = 0
function _update()
Here we rotate star2 around star1 by giving rotate star1's position(x, y).
rotate(star1.x, star1.y, 10, star2)
function _draw()
spr(star1.sprite, star1.x, star1.y)
spr(star2.sprite, star2.x, star2.y)
rotation logic
function rotate(x, y, radius, star)
degrees += 0.05
We update star's position by incrementing the degrees and using our formula
x + (radius * cosine(degrees)) = a point on the edge of the circle in the x-axis
y + (radius * sine(degrees)) = a point of the edge of the circle in the y-axis
star.x = x + (radius * cos(degrees))
star.y = y + (radius * sin(degrees))
draw debug inforatmion
function drawdebug()
print("star2\nx: "..star2.x.." y: "..star2.y)
view raw Rotations.lua hosted with ❤ by GitHub

Now To understand the basics, you need to understand some Trigonometry and circles. So, here are the functions you’ll need in any language to accomplish this: cosine and sine. Okay, let’s get down to business.

Circles And You

A circle is made up of two parts: it’s center and it’s radius. Now, you can touch any point on the circle by changing the radius and adding that number to the x and y that make up the circle’s center. This is exactly what makes up the Unit Circle. If you think about the circle’s edge as a list of points it all makes sense; draw a line from the center of the circle to the edge using the radius in any direction and you touch a point on the circle. So, let’s go into detail.

The Math

So, if we change the radius we can move around the circle. Now, if we were to multiply the radius by any number between 1 and – 1, we’d get a number within the absolute value of the radius. For example: 7 * 1 = 7; 7 * -1 = -7  The absolute value is still the same. Now, if we could do this infinitely, we could rotate around any object given it’s x and y coordinate in space. This is where sine and cosine come in. Sine and cosine both go from 1 – 1 when given degrees or radians; cosine represents the x-axis in space and sine represents the y-axis. By giving both sine and cosine the same degrees and incrementing, we can rotate around a point. Which, leads to the function in the code above used in our Pico-8 example:

function rotate(x, y, radius, star)
degrees += 0.05
star2.x = x + (radius * cos(degrees))
star2.y = y + (radius * sin(degrees))

We rotate the star around the bunny’s position in by constantly adding to the degrees, multiply the radius by cosine or sine accordingly, then adding it to the x and y, which make up the bunny’s position in space. If we wanted to change the rotation all we have to do is change the degrees; lower the incrementation to slow it down, or make it negative to rotate clockwise.

Hopefully, this post helped you and happy game making!

%d bloggers like this: