0 votes

Hello everyone, recently started tinkering around with 3D more while learning Godot, and finding it to be quite a challenge. I've been working on a little spaceship-shooter with controls somewhat similar to games like "Space Pirates And Zombies", where you move your mouse and the ship automatically rotates itself to aim that way.
At first I tried using look_at(), but found I didn't like the total lack of control I had over it, and it was also instant rotation. I need something that I can slow down to an individual ship's speed. (different ships, different turn speeds)
After quite a lot of work and more delving into trigonometry than I ever wanted. (I'm NOT a math-minded person) I worked out a theory about how to implement my own rotation system. Spoiler alert.... it doesn't work, and I'm not entirely sure where I've gone wrong.
There are two problems. #1, the way it's reading rotation is weird. I can move the mouse cursor to the left or right of the screen and make the ship rotate, but it stops if the cursor is in the middle of the screen. It's not rotating towards the cursor per se, but more like I can make it rotate a direction by moving my mouse to the left or right of the ship.
Problem #2 is that as soon as I move the ship anywhere, the rotation goes (even more) haywire. It's as though the rotation point is locked down on the world itself, and not the ship.

If anyone could point me in the right direction on this I would appreciate it. I feel like I'm already drowning trying to comprehend some of this stuff, though I AM trying to learn.

For the setup, I have the ship movement code in another function, that seems to work just fine.
I have this function in my physicsprocess(delta):

func rotate_to_mouse(delta):
var point_a = cursor.global_transform.origin #the point where the mouse cursor is
#point_a = to_local(point_a)
point_a = point_a.normalized() #normalize these or the numbers will get huge and crazy in an instant
var point_c = ship.global_transform.origin #the point where our ship is currently
point_c = point_c.normalized()
var point_b = ship.transform.origin + Vector3(0,0,-5) #Point ahead of the ship a bit 
point_b = to_global(point_b)
point_b = point_b.normalized()
#Get the length of the sides of our triangle
var length_side_a = point_c.distance_to(point_b)
var length_side_b = point_c.distance_to(point_a) 
var length_side_c = point_a.distance_to(point_b)
#Do the cosine math
var angle_cosine = ((length_side_a * length_side_a) + (length_side_b * length_side_b) - (length_side_c * length_side_c)) / 2 * length_side_a * length_side_b

var rotdampen = angle_cosine * delta * turn_speed

ship.rotate_object_local(Vector3(0,1,0),rotdampen)

Basically I have a 3D cursor, and I grab its position, the ships position, and a point a bit out ahead of the ship for a third point to make a triangle, and then I do cosine to get an angle from that to use to rotate towards the cursor point. Where have I got my wires crossed, or am I doing this in completely the wrong way to begin with?

Godot version 3.2.3
in Engine by (14 points)

1 Answer

0 votes

See the answers to the other question below. Don't reinvent the wheel!

https://godotengine.org/qa/79658/rotate-smoothly-with-look_at

by (243 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.