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):
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
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?