Rotate camera and movement
-
So I have the following code for movement:
r = 0 x = 0 z = 0 loop clear() c = controls(0) r += c.rx x += c.lx / 5 z += -c.ly / 5 setObjectPos(player, {x, 0, z}) setCamera({sin(r) * 5 + x, 2, cos(r) * 5 + z}, {x, 1, z}) drawObjects() update() repeat
But when I turn the camera and move, it won't move based of the cameras rotation (I know why this happens) and I don't know how to make it do that. If any of you have a solution, please tell me. :D
-
I’ve done this but it was a while ago. I shared the blog post I copied from here:
@toxibunny said in Good articles found on the web.:Here is a nice simple 3rd person camera for 3d stuff. Again, it’s not Fuze specific, but it’s easy enough to use. I just basically copypasted (with very slight changes) the ’move character relative to camera position’ bit for my project, and it worked!
https://www.braynzarsoft.net/viewtutorial/q16390-32-simple-3rd-person-camera
The relevant bit is actually titled ‘move the character in camera space’. If I understand you correctly, that’s the bit you need :)
-
What you need to do is use the facing direction as the basis vector for the vertical and horizontal movements from the analog stick.
x += (c.lx * cos(r) - c.ly * sin(r)) / 5 z += (-c.lx * sin(r) - c.ly * cos(r)) / 5
You can avoid a big formula like the one above if you represent the position and facing direction as vectors:
r = 0 p = {0, 0, 0} // Position vector loop clear() c = controls(0) r += c.rx f = {sin(r), 0, cos(r)} // facing vector pf = {f.z, 0, -f.x} // Perpendicular to facing p += (f * -c.ly + pf * c.lx) / 5 setObjectPos(player, p) setCamera(f * 5 + p + {0, 2, 0}, p + {0, 1, 0}) drawObjects() update() repeat
-
Woah. So was all that stuff I did just a waste of time?
-
No it wasn't a waste. The tutorial you posted is complimentary; it contains useful hints, techniques for damping, and a discussion of the character displayed in 3rd person view. The tutorial in the Fuze help is only about a first person camera, and my post was just showing the calculations to adapt the provided program without much explanation. All of these together should contribute to a better understanding of the problem and its solution.
-
I noticed that in gothon’s code, there’s
pf = {f.z, 0, -f.x}
Whereas in the blog post I linked, she has
Camright = (-camForward.z, 0.0f, camForward.x)
I’m guessing the difference in which ones are reversed with the - sign is something to do with ‘right-handed’ or ‘left-handed’ coordinate system.
This is a thing which tripped me up/confused me for a while. Just wanted to highlight it.