Manual Rotation Issue

Godot Version

v4.2.1

Question

I’m trying to revolve a sphere around an origin point in the world, but for some reason my math just isn’t mathing. I’m fairly certain my code is correct, but the sphere just keeps jumping between two points rather than following a circular path.

My code for changing the location of the sphere is as follows:

var time = 0
var deg = 0.1
func _physics_process(delta):
	var sphereMesh = $environ/StaticBody3D/originMesh/sphere
	var origin = $environ/StaticBody3D/originMesh
	
	time = time + 1
	if time == 100:
		print(global_funcs.rotate_point3D2(sphereMesh.global_position, origin.global_position, deg))
		sphereMesh.global_position = global_funcs.rotate_point3D2(sphereMesh.global_position, origin.global_position, deg)
		time = 0

The code for applying the rotation around the origin is:

func rotate_point3D2(point, origin, deg):
	var s = sin(deg_to_rad(deg));
	var c = cos(deg_to_rad(deg));
	
	var point2 = Vector2(point.x, point.z)
	
	point2.x = point2.x - origin.x
	point2.y = point2.y - origin.z
	
	point2.x = point2.x * c - point2.y * s
	point2.y = point2.x * s - point2.y * c
	
	point2.x = point2.x + origin.x
	point2.y = point2.y + origin.z

	return Vector3(point2.x, point.y, point2.y)

I’d really appreciate any help, thanks

You’re updating point2.x before you compute point2.y so it uses the new converted value, you need to do:

func rotate_point3D2(point, origin, deg):
	var s = sin(deg_to_rad(deg));
	var c = cos(deg_to_rad(deg));
	
	var point2 = Vector2(point.x, point.z)
	
	point2.x = point2.x - origin.x
	point2.y = point2.y - origin.z
	
	point2 = Vector2(point2.x * c - point2.y * s, point2.x * s - point2.y * c)
	
	point2.x = point2.x + origin.x
	point2.y = point2.y + origin.z

	return Vector3(point2.x, point.y, point2.y)

Whoops, can’t believe I didn’t catch that earlier. I added the changes but it still seems to be doing the same thing.

Try:

func rotate_point3D2(point, origin, deg):
	var s = sin(deg_to_rad(deg));
	var c = cos(deg_to_rad(deg));
	
	var point2 = Vector2(point.x, point.z)
	
	point2.x = point2.x - origin.x
	point2.y = point2.y - origin.z
	
	point2 = Vector2(point2.x * c - point2.y * s, point2.x * s + point2.y * c)
	
	point2.x = point2.x + origin.x
	point2.y = point2.y + origin.z

	return Vector3(point2.x, point.y, point2.y)

That’s the transform formula for 2D rotations

1 Like

Ah awesome, that seems to work. Thanks for the help

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.