Get angle to camera? [Solved]


Is there a way to get the angle at an object between the horizontal and the camera? This is someting I saw in an OGRE demo. There is this huge forest, but only the trees that are close to the camera are models, the rest are textured planes. Each plane changes the texture for the tree depending on the elevation of the camera.


Edit: I suppose if someone could point me in the right direction to get the height of the camera I could figure it out myself.

controller = GameLogic.getCurrentController()
cam = controller.owner

camHeight = cam.worldPosition[2]

Put that script on your camera, and camHeight will be the z-position of the camera.

If you want the angle to the other object, you could use some trigonometry. I’m not positive about this script, but it would be something like:

dx = abs(cam.worldPosition[0] - trees.worldPosition[0])
dy = abs(cam.worldPosition[1] - trees.worldPosition[1])
dz = abs(cam.worldPosition[2] - trees.worldPosition[2])

hyp = sqrt(dx**2 + dy**2)

theta = atan(dz/hyp)

That leaves you with theta, which is the vertical angle from the trees to the camera, in radians. To convert to degrees, multiply theta by 180/pi.

If you have this script on the trees instead of the camera, remember that controller.owner will be the tree object, and you would need to get the camera from GameLogic.getCurrentScene().objects[“OBCamera”]

Then you can set theta*180/pi as the value of a property, and change the image using interval property sensors.

Ok cool. The trig wont be a problem, I just didnt know how to get the height of the camera.

This is my first real venture into blender python. Hopefully it all goes well.

Edit:Well, its now 1:16 in the morning, and I got it working. I’m gonna work on it a little more later, then I may post it if anyone wants.