# Two questions: Orientation and if statements

First question: Is it possible to set an objects orientation to another objects orientation but plus some degrees? I believe the orientation is stored in radians and my basic attempt to add to it was just:
obj.orientation = cam.orientation.z + math.pi (pseudo code)

but that didn’t work. Is there something wrong with my syntax or am I missing some mathy type thing?

My second question is there a way to have an if statement that is activated when two things are true, but then after that initial activation only needs one value to continue working? An example would be for a cube to move the mouse would need to be over the cube and the left mouse button must be down, but after it is initiated, only the mouse must be held down to keep the cube moving. I’m kind of stumped on this one, but I need something like this for my game. Thanks guys!

You can concatenate rotations by multiplying orientation matrices:

``````
A.worldOrientation = B.worldOrientation #  "set an objects orientation to another objects orientation"

A.worldOrientation  *= Matrix.Rotation(math.radians(some_degrees), 3, 'Z')  # "plus some degrees" around Z

``````

Orientation matrices are somewhat explained in the beginning of this video tutorial: http://www.youtube.com/watch?v=hyxS4RyWxpk

for a cube to move the mouse would need to be over the cube and the left mouse button must be down, but after it is initiated, only the mouse must be held down to keep the cube moving.

``````
if left_mouse_down() and mouse_over_cube():
cube_move()

``````

But you want to move the cube only with the mouse, after the initial mouse_over condition, so this structure makes more sense:

``````
if left_mouse_down():
if mouse_over_cube():
cube_move()

``````

So now you can probably intuit that you need to flip a toggle somewhere in that sub-conditional:

``````
if left_mouse_down():
if mouse_over_cube():
toggle = True
if toggle:
cube_move()

``````

Assuming the toggle is a persistent variable, initialized to False, this should satisfy the given requirements.

Hey thanks for the response, for some reason when I implemented the method you suggested for my second question the toggle is always false no matter what. It doesn’t change to True when it should and I don’t know why.
Code:
hit_obj = mouse_over.hitObject

if keys[leftmouse] >= 1:
if hit_obj == own:
toggle = True
else:
toggle = False
if toggle == True:
target_vec = Vector(player.getVectTo(own)[1])
target_vec.magnitude = PULL_SPEED
player.setLinearVelocity(target_vec.xyz)
print(toggle)
EDIT: the indenting is alright it just doesn’t show up int he post

You can get a persistent variable by saving on the owner:

``````
own["toggle"] = False

``````

Also, when posting code to the forums, use code tags.

… and using “if variable == True:” is redundant. “if variable:” is sufficient enough (if you expect a boolean value).

Thanks for the tips and help! I’m still very much in the learning phase

for the rotation of the object, which axis do you want to rotate.
if you just want a full orientation:

``````
camRotation = cam.worldOrientation
the_object_you_want_to_rotate.worldOrientation = camRotation

``````

the rotation matrix looks like this:
[1, 0, 0] X = [0]
[0, 1, 0] Y = [1]
[0, 0, 1] Z = [2]
x y z
[0][1][2]

so if you want to have an rotation of 90 degrees overto the left from the above imagery:
[0,1,0]
[1,0,0]
[0,0,1]

this will give you a rotation over the x and Y axis of 90 degrees left from the previous matrix.

and to access a part of it you need to call it :

``````
matrix[0][0] # the first pair of brackets gives the first row, the second pair of brackets gives you the column

``````

this gives you the first row, and from the first row the first item.

to prevent the object to rotate over the z axis(or an axis you want.)
you need to keep those corresponding values to zero.

So from my understanding, you need to split the matrix into bite pieces X, Y and Z.
and manipulate them as lists(just don’t use append)

for example one that does not rotate on the z axis:

``````
newOrientation = cam.worldOrientation
object_to_orient = object.worldOrientation   # this is the one to dissassemble into x, y and z

object_to_orient[0][2] = 0
object_to_orient[1][2] = 0
object_to_orientz = [0, 0, 1] # the one indicates it is facing into +Z direction(if that one is zero it wil give errors)

object.worldOrientation = [object_to_orient[0], object_to_orient[1], object_to_orientz]

``````

I hope this clears it up for a bit.

Chances are you will actually want to work with radians rather than orientation matrices.

``````
x, y, z = camera.worldOrientation.to_euler() #set variables x, y and z to camera world orientation (in radians)
z += 3.1416 #in python you can write it like this instead of z = z + 3.1416
#you can use the math.pi but I'm not sure if you're going to be doing it a lot and if you really need the precision for it
#note that pi is 180 degrees and 90 degrees would be pi/2 =~1.5708

object.worldOrientation = [x, y, z] #you can write in euler, no need to convert back to matrix

``````

Goran’s post is of course accurate to the point but chances are you will want to work with radians rather than orientation matrices:

``````
x, y, z = camera.worldOrientation.to_euler() #set variables x, y and z to camera world orientation (in radians)
z += 3.1416 #in python you can write it like this instead of z = z + 3.1416
#you can use the math.pi but I'm not sure if you're going to be doing it a lot and if you really need the precision for it

object.worldOrientation = [x, y, z] #you can write in euler, no need to convert back to matrix

``````

true, but I am not sure what kind of consequences it has for the performance.
I myself don’t like it when I need to convert output to change an input when you can actually just use the native data and manipulate that for your own needs.
But it certainly is a lot shorter and easier.

It’s just some people just making the transition from logic bricks to python could get overly complex impression of python if they are presented the best and most accurate methods before something that is clearer and easier to use. They will likely (want to!) learn about matrices and be able to write “one-liner” solutions sooner or later anyway.

Just expressing the flipside in case someone finds it useful