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:

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))

end

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!

## Recent Comments