# How can I rotate an object smoothly using sin or cos?

I have an object in the center that "points" to objects around it. I wish to be able to click left or right buttons on the keyboard and have the object rotate (visibly, slowly, and smoothly) exactly 36 degrees using python.

How can I do this? I've seen stuff with sine or cosine and elapsed time but nothing that I can understand well enough to adapt to my needs.

I had this working using Action Actuators and since I want to be able to rotate clockwise and counterclockwise to point to 10 different objects, there were 21 different actuators. I would like to learn to do this with python instead.

Thank you, Zach

• What are your needs? All provided utilities (matrix operations, Euler operation, motion actuator, motion API call ...) already do the sin/cos stuff for you. So what is the reason why you think you need to do it by yourself? Sep 16 '16 at 6:36
• You know that one action actuator can play any pose at a single action with property mode? You know you can turn an object by degree with the motion actuator? You know you can let an object turn slowly (but not at constant speed) to another object with the trackTo actuator? Sep 16 '16 at 6:38
• What do you mean "All provided utilities"? Where are those? The motion actuator rotates n degrees at a constant rate, right? I will look at the property mode of the action actuator. The reason i want to do this "by myself" is so I can learn. Sep 16 '16 at 14:53
• I mean there are quite a lot of options to achieve the effect you are looking for. Typically there is no need to deal with sin/cos by yourself. Sep 19 '16 at 4:43

Following solution might work for you (if I understand the requirement correctly) ...

1. Create a maxRot (int) property on the object.

2. Create an always sensor on the object that points to following python code

import bge
from bge import logic

cont = logic.getCurrentController()
own = cont.owner

if (own['maxRot']>0):
own.applyRotation ((0, 0, 0.0174533), True)
own['maxRot'] -= 1

1. Create an keyboard sensor (right arrow) on the object that points to following python code ...
import bge
from bge import logic

cont = logic.getCurrentController()
own = cont.owner
own['maxRot'] += 36


Run play and click the right arrow.

[0.0174533 = 1 deg in radians]

• I used this to make my "pointer" rotate where I can see it rotate. However it is a constant velocity and I do not know how to incorporate a sine or cosine wave into your example so that it slowly speeds up to a maximum speed then back down to zero while still only moving 36 degrees total. Sep 15 '16 at 21:10

After experimenting for a while and working it out on paper, I was able to figure out the answer.

Blend File Here

Just hit "P" in the viewport and use the left and right arrow keys to make the "pointer" rotate. You can switch between "constant" and "interpolated" rotation by activating the appropriate controller and deactivating the other one (the checkbox on the controller).

I hope this helps others that want to know how to do this from scratch.

Zach