3D Moving the cursor towards a point in 3D

Fredrik

Member
I've made it so if you stand nearby a NPC in my 3D game and press F you'll start a dialogue, but how can I make it so when I press F the camera will also move towards a point, let's say the NPC's fade? not pop right to the point but move towards it and then stop.
 

zircher

Member
Given most people have unique variables and ways of setting up their 3D scenes, a specific bit of code won't help (yet). The easiest way is to imagine a camera in your hand and moving it towards the target. You have to move the camera in 3D space and orient it towards the NPC's face. Once you hit F, you should lock the camera from user input while it is being animated. The easiest method is linear. Let's say your room speed is 30 and you want it to take two seconds to move to the new location. You just need to move the camera in 60 small steps. So, when you hit F set a flag to begin movement of the camera. Something like cameraCounter = 60. Determine where you want the camera to go. Let's call that newCamX, newCamY, and newCamZ. Calculate the amount of movement per step as stepX = (currentCamX - newCamX)/60, stepY = (currentCamY - newCamY)/60, and stepZ = (currentCamZ - newCamZ)/60. Now, every step that cameraCounter >0 increase the current camera location by the step amounts (camX += stepX, camY +=stepY, camZ += stepZ) while decreasing the cameraCounter -= 1. Once the cameraCounter hits zero the move is complete.

That's half the battle. Odds are you camera will not be looking where you want it to. You have to do the same thing with the camera's pitch and yaw. Determine how you want to rotate the camera, find the difference, and divide it into 60 steps. There are many styles of camera, so I have not gone into as much detail. (They're all the same camera but their behavior can be a different thing like 'isometric' cameras, trolley cameras, over the shoulder, close ups, 3rd person views, orbiting cameras, etc.)

Hopefully, that will get you pointed in the right direction (heh.)

---
Debugging tip, I write the camera coordinates, pitch, and yaw to the screen. I note the values of the current position and then I manually move the camera to where I want to go when it is animated and write down those values. That might help to visualize what steps you have to apply to get from A to B.

[edited for pesky typos]
 
Last edited:
Top