Ursina Module Rotation Help

I’m currently learning the Ursina 3d game engine module for Python. I’m attempting to write a code where you press the ‘s’ key and the cube makes a full 360 degree rotation along the x axis. Right now the code works for the first full rotation, then afterward when you press ‘s’ it only rotates by a couple degrees each time. Here is the code, I can’t figure out what is wrong exactly. Thanks so much.

from ursina import *

class Rotatebox(Button):

    def __init__(self):
        super().__init__()
        self.model = 'cube'
        self.texture = 'PlayerTrans'
        self.scale = (.1,.1,.1)
        self.position = (0,0,0)
        self.color = color.white
        self.rotation_speed = 100  # Adjust the rotation speed as desired
        self.rotation_angle = 0
        self.rotate = False

    def update(self):
        if self.rotate == True:
            self.rotation_angle += self.rotation_speed * time.dt
            self.rotation_x = self.rotation_angle

            if self.rotation_angle >= 360:
                self.rotate = False

        

    def input(self, key):
        if key == 's':
            self.fullRotate()

    def fullRotate(self):
        if self.rotate == False:
            self.rotate = True

app = Ursina()

boxrotate = Rotatebox()

app.run()


I’m guessing that it’s because after a full rotation, self.rotation_angle has reached 360, so it’ll then keep stopping the rotation. You need to also reset self.rotation_angle back to 0.

Incidentally, it’s neater to say if self.rotate: and if not self.rotate:.

1 Like

That fixed it. And you’re definitely right about that being neater, still working on understanding the basics, but also trying to be as clean as possible.

Maybe try:

if self.rotation_angle >= 360:
    self.rotate = False
    self.rotation_angle = self.rotation_angle % 360

As far as a rotating object is concerned, 375 is the same as 15, so there’s no apparent visual difference. And you don’t want to let rotation angles grow infinitely, or someone will do a weeks-long livestream of how they can crash your game.

1 Like

If you remove that part it will be Ok.

if self.rotation_angle >= 360:
                self.rotate = False