Ursina vector rotation. The right and easy way to deal with this 3d Python module!

Whirling Vectors in Ursina: Spin Your Way to 3D Awesomeness!

Ursina Python 3d rotation

Ursina object manipulation. Entity and vector rotations made easy

Ursina is a Python module that provides a lightweight game development framework. It is built on top of the popular game engine, Panda3D, and aims to simplify the process of creating 2D and 3D games using Python.

Let’s get straight to the point. Although there are several ways to do it, we’re showing a simple but robust approach. Let’s take a look at one that generally can be useful for us. We start rotating a simple vector using auxiliary functions. In the second part, we’re exploring how to rotate a proper Ursina Entity. We’re going to make our little cube do a mind-blowing pirouette.


Ursina vector rotation. How to rotate a Vec3 object

The Vec3 object in Ursina is a fundamental tool for working with three-dimensional coordinates. It represents a point or a vector in 3D space, consisting of three components: X, Y, and Z.

import numpy as np
import matplotlib.pyplot as plt
import logging
logging.getLogger("ursina").setLevel(logging.WARNING)
from ursina import *
## package_folder: C:\Users\Carlos\AppData\Local\Programs\Python\PYTHON~1\lib\site-packages\ursina
## asset_folder: C:\Users\Carlos\AppData\Local\Programs\Python\Python310
from ursina import Vec3


def unit_vector(vector):
    """ Returns the unit vector of the vector."""
    return vector / np.linalg.norm(vector)

def angle_between(v1, v2):
    """Finds angle between two vectors"""
    v1_u = unit_vector(v1)
    v2_u = unit_vector(v2)
    return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))

def x_rotation(vector,theta):
    """Rotates 3-D vector around x-axis"""
    R = np.array([[1,0,0],[0,np.cos(theta),-np.sin(theta)],[0, np.sin(theta), np.cos(theta)]])
    return np.dot(R,vector)

def y_rotation(vector,theta):
    """Rotates 3-D vector around y-axis"""
    R = np.array([[np.cos(theta),0,np.sin(theta)],[0,1,0],[-np.sin(theta), 0, np.cos(theta)]])
    return np.dot(R,vector)

def z_rotation(vector,theta):
    """Rotates 3-D vector around z-axis"""
    R = np.array([[np.cos(theta), -np.sin(theta),0],[np.sin(theta), np.cos(theta),0],[0,0,1]])
    return np.dot(R,vector)


original_vector = Vec3(1, 0, 0)
angle_between(original_vector, original_vector)
## 0.0
rotated_vector = y_rotation(original_vector, 120)
angle_between(original_vector, rotated_vector)
## 0.6194791635878569
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.plot(np.linspace(0,original_vector[0]),np.linspace(0,original_vector[1]),np.linspace(0,original_vector[2]), 'b')
ax.plot(np.linspace(0,rotated_vector[0]),np.linspace(0,rotated_vector[1]),np.linspace(0,rotated_vector[2]), 'r')

plt.title("Python Ursina 3D vector rotation")
plt.show()


Entity 3d object rotation in Ursina

As annotated here, the predefined rotation axes in Ursina are as described below:

x: Perform a clockwise rotation around the x-axis, observed from the external viewpoint.

y: Perform a clockwise rotation around the y-axis, observed from the external viewpoint.

z: Perform a counterclockwise rotation around the z-axis, observed from the external viewpoint. This direction is reversed due to the nature of 2D games.

app = Ursina()

rotation_resetter = Entity()
cube = Entity(parent=rotation_resetter, model='cube', texture='white_cube')


def update():
    rotation_resetter.rotation_x += 100 * (held_keys['a'] - held_keys['d']) * time.dt
    rotation_resetter.rotation_z += 100 * (held_keys['w'] - held_keys['s']) * time.dt

    cube.rotation = cube.world_rotation
    rotation_resetter.rotation = (0,0,0)

EditorCamera()

#app.run()
## EditorCamera(name='editor_camera', color=color.white, )

Run this code, make use of ‘a’, ‘d’, ‘w’ and ‘s’ and witness the transformation for yourself. Remember that rotation in x axis

**Disclaimer: No cubes were harmed during the making of this blog post. Dancing moves are for entertainment purposes only.


Stay updated on Python tips

Happy coding, and may your pixels dance to the beat of your imagination!

If you want to stay updated…

Carlos Vecina
Carlos Vecina
Senior Data Scientist at Jobandtalent

Senior Data Scientist at Jobandtalent | AI & Data Science for Business

Related