The question is exactly the same as the title: How can I analyze whether my camera is looking up/down? :)

A bit of explanation: I am writing a script that handles both 3rd and 1st person view, and now got stuck at the point where I want setting a cap to prevent the view from turning around vertically.

For this I made an "if... elif... elif..." statement. The first branch tests, whether the sight is between the caps, and if so, then lets the user move the view. So after that, I need to check which direction the user wants to move the view, plus which direction is it stuck to. These do for the 2 other branches.

So this is why I'd need your help.

Awaiting your answers :)


When the camera's X rotation (in euler rotation) is at 90 degrees it is looking parallel to the ground. Less than 90 it will be pointing down at the ground, greater than 90 is pointing up.

if camera.rotation_euler.x > 90 and camera.rotation_euler.x < 180:
    # looking up
elif camera.rotation_euler.x < 90 and camera.rotation_euler.x > 0:
    # looking down
  • $\begingroup$ This simply stops my script from working. :( $\endgroup$ – user1733 Dec 3 '13 at 17:08
  • $\begingroup$ BTW, am using Blender 2.69 $\endgroup$ – user1733 Dec 3 '13 at 17:09
  • $\begingroup$ If you just copy and pasted then you will also need to add something like camera = bge.logic.getCurrentScene().active_camera so it knows what object camera is meant to be referring to. $\endgroup$ – sambler Dec 3 '13 at 19:14
  • $\begingroup$ scene = bge.logic.getCurrentScene() ... empty = scene.objects["empty"] ->this is what I want to rotate, so this is not the problem $\endgroup$ – user1733 Dec 3 '13 at 19:38
  • 1
    $\begingroup$ Note, using rotation_euler is incorrect, it wont take constraints or parenting into account. use matrix_world.to_euler() instead if you need the worldspace euler. $\endgroup$ – ideasman42 Dec 3 '13 at 23:40

This simple script gets the angle from the up axis to the cameras view-axis.

import bpy
from mathutils import Vector 

camera = bpy.data.objects["Camera"]
z_up = Vector((0.0, 0.0, 1.0))
z_camera  = z_up * camera.matrix_world.to_3x3()
angle = z_camera.angle(z_up)

is_up = angle < math.degrees(90.0)

Note that matrix_world and not rotation_euler, this is important since rotation_euler is the rotation input from fcurves and drivers, and not the final transformation of the object with constraints and parents.

  • $\begingroup$ erm, where does "z_camera" come from? $\endgroup$ – user1733 Dec 5 '13 at 1:38
  • $\begingroup$ @user1733 oops - corrected. $\endgroup$ – ideasman42 Dec 5 '13 at 4:16

Got it! :)

aroundX = logic.getCurrentController().owner.worldOrientation.to_euler().x      #_getting rotation direction: + on Up & - on Down
capped = False                                                                  #_capped is "False" as default
if (aroundX > 1.3 and y > 0) or (aroundX < -1.1 and y < 0) == True:     #_capped becomes "True" when  it has to
    capped = True

y shows the mouse movement's 2nd coordinate


I have another idea, you could calculate the angle between the z-axis which points upward and convert the camera rotation to a transformation matrix to create another vector pointing to the camera. After that you can determine the angle of the camera to the z-axis with the dot product which returns the cosine of the angle. @samblers suggestion doesn't seem to take rotations on the cameres y and z-axis into account.

With this method you could limit (if..else) the angle from 0..70 (max 90)

import bpy
import math
from mathutils import Vector 

camera = bpy.data.objects["Camera"]
origin_z = Vector((0,0,1))
cam_rot  = camera.rotation_euler.to_matrix().to_4x4()
z_cam  = origin_z * cam_rot
angle = math.degrees(math.acos( origin_z.dot( z_cam )))
print( angle )

I verified this with the Math Vis addon enabled: enter image description here

You might find this a great tool when coping with rotations and vectors. Simply enter

origin_z = Vector((0,0,1))
cam_rot  = camera.rotation_euler.to_matrix().to_4x4() 

into the console and see how it looks like.

Some examples:

enter image description here

Prints 0.0

enter image description here

Prints 39.00000357556496

enter image description here

Prints 39.00001985547789

enter image description here

Prints 53.46396688981824

  • $\begingroup$ Thanks for the idea, but I just cannot find any way to implement it into my script to get any use of it. :( BTW, does it give back numbers like °s? or radians? or something else? $\endgroup$ – user1733 Dec 3 '13 at 20:42
  • $\begingroup$ It prints degrees 'angle = math.degrees' if you need radians simply omit 'math.degrees' in the last line. $\endgroup$ – stacker Dec 3 '13 at 21:59
  • $\begingroup$ How about adding your script to the question? $\endgroup$ – stacker Dec 3 '13 at 22:00
  • $\begingroup$ coming in parts: (but this is a reworked type): import bge; from bge import render; import mathutils; scene = bge.logic.getCurrentScene(); def main():; #_LOOK PROPERTIES:; rotSpeedX = 0.005 ; #_setting mouse movement speed (right & left); rotSpeedY = 0.005 ; #_setting mouse movement speed (up & down); cap = 82.00 ; #_setting rotation cap (up-/downwards); $\endgroup$ – user1733 Dec 4 '13 at 0:56
  • $\begingroup$ #_SENSOR AND OBJECTS:; mouse = bge.logic.getCurrentController().sensors["mouse"]; #_sensor; ketrec = scene.objects["ketrec"] ; #_object; empty = scene.objects["empty"] ; #_object; karakter = scene.objects["karakter3rdP"] ; #_object; #_MOUSE MOVE PROPERTIES:; x = (render.getWindowWidth() / 2 - mouse.position[0]) #_getting mouse move; y = (render.getWindowHeight() / 2 - mouse.position[1]); #_getting mouse move; vector1 = mathutils.Vector([0.0, 0.0, 1.0]); camOri = empty.localOrientation $\endgroup$ – user1733 Dec 4 '13 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.