# Rotation Issues

4.2.1

### Question

Hi there, I’m having a bit of trouble rotating a cube in 3d space. The rotation does indeed rotate, but the coordinates are slightly off when rotated, almost as if they’re being sheared. I’m using the normal equations x’ = xsin(0)… , so I’m not sure what I’m doing wrong. Here is a picture of the cube rotated in the y-axis 45 degrees:

And here is my code doing the rotating:

``````func get_outer_points(shape: MeshInstance3D):
var points = {} # p1, p2, p3, p4, p5, p6
#var transform = shape.global_transform
var transform = shape.global_transform
var center = transform.origin

var deg = transform.basis.get_euler()

var size_x = (shape.scale.x/2.0)*shape.mesh.size.x
var size_y = (shape.scale.y/2.0)*shape.mesh.size.y
var size_z = (shape.scale.z/2.0)*shape.mesh.size.z

for x in [-size_x, size_x]:
for y in [-size_y, size_y]:
for z in [-size_z, size_z]:
var point = Vector3(x, y, z)
print(point)
point = rotate_point(point, deg, center)
print(point)
points[point] = Color(1.0, .5, .3, 0.0)

return points

func rotate_point(p: Vector3, e: Vector3, t: Vector3):

p[0] = p[0]*cos(e[1]) - sin(e[1])*p[2] # I'm only using this part at the moment to check my y-rotation
p[2] = p[0]*sin(e[1]) + cos(e[1])*p[2]

#p[1] = p[1]*sin(e[0]) + cos(e[0])*p[2]
#p[2] = p[1]*cos(e[0]) - sin(e[0])*p[2]

#p[0] = p[0]*cos(e[2]) - sin(e[2])*p[1]
#p[1] = p[0]*sin(e[2]) + cos(e[2])*p[1]

for i in range(3):
p[i] = p[i] + t[i]

return p
``````

If anyone with more math experience could help, I’d be super thankful.

This should be global_transform.origin

I would also use builtin functions over using sin and cos any day of the week. I would also avoid using euler and learn quaternions.

Finally since you are trying to rotate each point of mesh, why not use a shader.

Oh I didn’t realise I was using local transforms there, thanks for the heads up.

I would use a function to just rotate the mesh, but I’m mainly trying to get the vertices and the coordinates of them when the mesh is rotated, for collision purposes. I couldn’t find anything that does this automatically sadly. Getting the aabb of a mesh does not account for rotation it seems, and I’m not too sure how to use transformations directly on coordinates to rotate them.

I’ll probably have to learn how to use quaternions like you said because the more I read, the less it seems euler angles are useful.

Quaternions just allow for more precise control, while euler will rotate one axis at a time. Which can lead to strange rotations.

AABB is typically used as a bounding box to determine collision detection optimization of collision shapes and the physics engine.

There are polygon collision shapes that I think can be almost anything even the shape of your mesh(with a performance cost), but since it looks like you are just working with cubes there is a box collision shape, that you could align with the mesh.

As far as rotating it seems like you are rotating the same degree on all axis so you could just call

``````transform.rotate_y(deg)
transform.rotate_x(deg)
transform.rotate_z(deg)
``````

Or whichever order euler does it.

If there is a point in space then just you `look_at`

1 Like