Basis in Godot – Complete Guide

Welcome to this comprehensive guide on the Basis class in Godot 4. If you’re venturing into the realm of 3D game development using Godot, understanding the Basis class is a cornerstone. This tutorial will introduce you to this essential component, demonstrate its use in game mechanics, and uncover why learning about the Basis class can amplify your game development skills. Whether you’re at the beginning of your coding journey or you’re an experienced coder looking to refresh your understanding of 3D transformations, this article aims to be as beginner-friendly as it is informative. So gear up to dive deep into the Godot 4 engine’s Basis class, and learn to manipulate the digital space with precision and creativity.

What is the Basis Class?

The Basis class in Godot is a powerful tool. It’s a 3×3 matrix primarily used to represent 3D rotation and scale. When delving into the 3D space, you need to manipulate objects not just by moving them around but by rotating them and altering their size. The Basis class makes this possible through its matrix configuration which consists of 3 vectors – X, Y, and Z, representing the local axes of an object’s transformation.

The Role of Basis in Godot

Each vector in the Basis matrix can be manipulated to affect the corresponding axis of an object in Godot. This means you have control over how an object is rotated and scaled within the 3D environment. Often, Basis is used in conjunction with the Transform3D class to position objects in 3D space, making it an indispensable asset for 3D game development in Godot.

Why Should I Learn About Basis?

Understanding the Basis class opens up a plethora of possibilities in 3D game development. It’s the key to:

– Creating intricate rotations and allowing objects to interact with each other realistically.
– Building complex game mechanics like physics-based puzzles or character movement systems.
– The precise scaling of game assets, which is crucial for creating visually balanced scenes.

By mastering Basis, you gain more granular control over your game’s 3D environment, allowing you to create more dynamic and engaging gameplay experiences. Let’s move forward and explore how to use the Basis class through practical examples.

CTA Small Image

Initializing a Basis Instance

Before we start manipulating 3D objects, we must know how to create a Basis instance in Godot 4. One way to initialize a Basis is by directly using its constructor. Here’s how you can create a default Basis, which corresponds to no rotation and no scaling:

var my_basis = Basis()

Additionally, you can initialize a Basis using three Vector3 objects, each representing the X, Y, and Z axis of the matrix:

var x_axis = Vector3(1, 0, 0)
var y_axis = Vector3(0, 1, 0)
var z_axis = Vector3(0, 0, 1)
var my_basis_from_axes = Basis(x_axis, y_axis, z_axis)

Rotating with Basis

Rotation using the Basis class involves creating a rotation matrix and then applying it to your objects. For example, to rotate around the Y-axis by 90 degrees:

var rotate_y_90 = Basis(Vector3.UP, deg2rad(90))

What if you need to rotate around a custom axis? You can achieve this by creating a Basis and then rotating it:

var my_custom_basis = Basis()
var custom_axis = Vector3(1, 0, 1).normalized()
my_custom_basis = my_custom_basis.rotated(custom_axis, deg2rad(45))

Scaling with Basis

To scale an object in Godot, you modify the Basis vectors directly. Here’s an example where we scale an object to have twice the width, maintaining its height and depth:

var my_basis = Basis()
my_basis.x = my_basis.x * 2

You can uniformly scale an object on all axes like so:

var my_basis = Basis()
var scale_factor = 2.0
my_basis = my_basis.scaled(Vector3(scale_factor, scale_factor, scale_factor))

Decomposing a Basis

Sometimes, you may want to decompose a Basis to understand the rotation and scaling it represents. Here’s how to extract the scale from a Basis matrix:

var my_basis = Basis() # Assume this Basis is already scaled/rotated
var scale = my_basis.get_scale()

To extract rotation in the form of a Euler angles (which are more human-readable), you can do:

var my_basis = Basis() # Assume this Basis is already scaled/rotated
var euler_angles = my_basis.get_euler()

The above examples cover the basics of creating, rotating, and scaling with the Basis class. Practice these concepts to build a strong foundation before we move on to more complex manipulations and combining Basis with other classes to control 3D objects with precision. In the next part, we’ll look at how Basis interacts with other classes and dive into practical scenarios where Basis proves to be indispensable in Godot 4.Continuing with our exploration of the Basis class in Godot 4, we will further examine its utility by delving into more complex manipulations and scenarios.

Applying Basis to Transformations

When it comes to positioning objects in 3D space, the Transform3D class is used together with Basis. Transform3D consists of a Basis and an origin vector. Here’s how you can set the rotation part of a Transform3D with a custom Basis:

var my_transform = Transform3D()
var my_basis = Basis() # This Basis has some rotation
my_transform.basis = my_basis

If you need to update the position of an object while retaining its custom rotation, you can modify the origin component of Transform3D:

my_transform.origin = Vector3(1, 2, 3)

The resulting Transform3D can then be used to position a Spatial node in the scene.

Converting Between Basis and Quaternions

Sometimes, it’s convenient to use quaternions for rotations, as they can avoid issues like gimbal lock. Godot makes it simple to convert back and forth between Basis and Quaternions:

var my_basis = Basis() # Assume this Basis has some rotation
var my_quat = my_basis.get_quat()

# Convert quaternion back to Basis
my_basis = Basis(my_quat)

Quaternions are particularly useful when you need to perform smooth interpolations between rotations, known as “slerping”:

var from_quat = my_basis.get_quat()
var to_quat = another_basis.get_quat()
var slerped_quat = from_quat.slerp(to_quat, 0.5)
var slerped_basis = Basis(slerped_quat)

Looking At a Point in Space

If you want an object to rotate so it’s “looking” at a certain point in space, you would use the `looking_at` function provided by Basis:

var my_basis = Basis()
var target_point = Vector3(5, 0, 5)
var up_direction = Vector3.UP # Typically, the world's up direction
my_basis = my_basis.looking_at(target_point, up_direction)

This method automatically calculates the necessary rotation for an object to face the `target_point`.

Adjusting Field of View Using Basis

In camera systems, Basis can be used to manipulate the field of view (FOV). Although the FOV is typically controlled through camera settings, Basis adjustments allow you to create effects like zooming:

var camera =
var fov_degrees = 70.0

# Modifying the camera's Basis to simulate a change in FOV
camera.projection_matrix = camera.projection_matrix.fovy(fov_degrees, camera.get_aspect())

Inverting Basis

There may be instances where you need to invert a Basis, perhaps to undo a transformation. Here’s how:

var my_basis = Basis() # Assume this Basis has some transforms
var inverted_basis = my_basis.inverted()

Keep in mind, if the Basis has been scaled to zero on any axis, it cannot be inverted, and the function will return an identity Basis instead.

As you continue to practice these examples, you will gain a deeper understanding of how Basis can be used to manipulate 3D space in Godot 4. Beyond game mechanics, these techniques can extend into areas such as procedural generation, animations, and complex camera controls. Experiment with Basis to unlock the full potential of your 3D games and experiences.Navigating through 3D space in Godot 4 with the Basis class becomes even more powerful when armed with the right examples and applications. Let’s continue our exploration with additional code snippets that showcase the versatility and utility of the Basis class in different scenarios.

Advanced Rotations with Basis

An essential skill in game development is combining multiple rotations. Here’s how to apply consecutive rotations to a Basis:

var my_basis = Basis()
var rotation_x = Basis(Vector3.RIGHT, deg2rad(30))
var rotation_y = Basis(Vector3.UP, deg2rad(45))

# Apply rotations sequentially
my_basis *= rotation_x
my_basis *= rotation_y

Building upon rotation, let’s also account for rotation around an arbitrary axis at a specific point, not just the object’s origin:

var my_basis = Basis()
var point_of_rotation = Vector3(1, 0, 0)
var arbitrary_axis = Vector3(1, 1, 0).normalized()
var rotation_angle = deg2rad(45)

# Translate to the point, rotate, and then translate back
my_basis = my_basis.translated(-point_of_rotation)
my_basis = my_basis.rotated(arbitrary_axis, rotation_angle)
my_basis = my_basis.translated(point_of_rotation)

Combining Scale and Rotation

Sometimes we want to apply both scaling and rotation to an object. Here’s how you can apply a non-uniform scale and then a rotation:

var my_basis = Basis()
var scale_vector = Vector3(2, 1, 1) # Double width scale
var rotation_angle = deg2rad(90)

# Scale first then rotate
my_basis = my_basis.scaled(scale_vector)
my_basis = my_basis.rotated(Vector3.UP, rotation_angle)

Combining transformations in different orders produces different results, an important aspect to experiment with for achieving the desired effect.

Interpolating Between Two Basis Instances

Animation and smooth transitions often require interpolating between two states. Here’s how you can interpolate between two Basis matrices, which can be used for smooth rotations:

var start_basis = Basis()
var end_basis = Basis(Vector3.UP, deg2rad(180))
var alpha = 0.5 # Midway interpolation

# Lerping the interpolation
var interpolated_basis = start_basis.linear_interpolate(end_basis, alpha)

Resetting an Object’s Rotation

To reset an object’s rotation back to its default state, you can set it to the identity matrix, which is equivalent to no rotation:

var my_basis = Basis() # Assume some transformations are applied
my_basis = Basis.IDENTITY

Mirroring Objects Using Basis

To mirror an object across an axis, we can scale by -1 along that axis. Here’s an example of mirroring an object across the X axis:

var my_basis = Basis()
my_basis.x = my_basis.x * -1

Constructing Basis from Euler Angles

While Godot often uses quaternions internally for rotations, it can be easier to think in terms of Euler angles. To create a Basis from Euler angles:

var euler_angles = Vector3(deg2rad(30), deg2rad(45), deg2rad(60))
var my_basis = Basis(euler_angles)

By incorporating these examples into your workflow, you’ll be well on your way to masterfully controlling the 3D environment in Godot 4. The Basis class is a tool of precision and flexibility, enabling endless creativity in how you design and implement your game’s mechanics and aesthetics. Continuously practice with Basis to seamlessly translate your vision into the virtual space, bringing your game world to life.

Continuing Your Godot Game Development Journey

Your exploration into Godot’s Basis class has just begun. There’s a whole world of game development techniques and best practices waiting to be discovered. To continue growing your skills and confidence in game creation, we invite you to check out our comprehensive Godot Game Development Mini-Degree. With project-based courses tailored for both beginners and experienced developers, our mini-degree can guide you through the nuances of both 2D and 3D game development using the powerful Godot 4 engine.

The demand for skilled game developers is constantly on the rise, and by expanding your knowledge with our courses, you’ll be equipping yourself to meet that demand. Our learning paths are designed to be flexible and accessible, allowing you to advance through the material at your own pace and on your own schedule. Whether you’re looking to deepen your understanding of gameplay control flow, player dynamics, or expansive game mechanics, our Godot courses serve as a launchpad for your ambitions.

Don’t just stop here; keep building, keep creating, and take your game development journey to new heights with the support of Zenva’s rich learning resources. With our Godot Game Development Mini-Degree, you’re not just learning how to code; you’re learning how to bring your gaming visions to life. Let’s keep learning and turn your passion into a reality together.


As we wrap up this exploration of the Basis class, remember that the paths you can take in game development with Godot are as limitless as your creativity. With the knowledge of 3D transformations and the power of the Basis class, you’re now better equipped to forge immersive experiences and dynamic gameplay. But this is just a single piece of the vast puzzle that is game creation—an exciting journey that continues as you learn and apply new concepts.

Don’t let this be the end of your learning journey. Dive deeper, challenge yourself with new projects, and explore advanced topics with our Godot Game Development Mini-Degree. The combination of your passion for game development and our educational resources can sculpt your path to becoming a successful developer. So, stay curious, stay motivated, and let us guide you through every step of your game development journey. Together, we can transform your game ideas into interactive realities that captivate and amaze.

Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.