Python - multiplying a vector's rotation by 2

Hi,
I have a cube that uses applyTorque to align itself to the camera, which rotates freely about the cube by mouse. This setup works okay, but I’d like to cover the cube’s range of motion with less rotation from the camera. Here’s my code so far:

import bge
from mathutils import Vector
cont = bge.logic.getCurrentController()
obj = bge.logic.getCurrentScene().objects
cam = obj["Camera"]
own = cont.owner

ownRot = own.worldOrientation.col[0]
axis = own.getVectTo(cam) 
quat = ownRot.rotation_difference(axis[1]) 
vec = Vector(quat.to_euler()) #Rotation x2????
torque = 100*1
own.applyTorque(torque*vec,0)

For example, if the camera was rotated at 45 degrees from the cube, the cube would rotate to 180 degrees (straight up). if it was rotated -45 degrees from the cube, the cube would rotate to face straight down. Or if 0 degrees, then the cube’s rotation would also be 0.

Basically, I’m trying to get an output of vectors with angles 0-180, with inputs of only vectors with angles 45-135. The only problem is, multiplying the vector by 2 just makes it a longer vector - it doesn’t change the rotation at all.

Is there any way to multiply the rotation of a vector?
Thanks in advance!

The first thing to do here is to learn some basics:

  • Rotation is represented either with Eulers, Matrices, or Quaternions.
  • You often can forget about Euler, and just use a Matrix or Quaternion.
  • To rotate a vector, you can do quaternion * vector, or matrix * vector to get a new rotated vector.

So far you seemed to be on good tracks with the .rotation_difference(...), but I feel like you got confused around that point.

forwardVector = owner.getAxisVect((1, 0, 0)) # make it point to your forward axis
cameraRotation = forwardVector.rotation_difference(own.worldPosition - camera.worldPosition)
rotatedVector = cameraRotation * (cameraRotation * forwardVector)

If not exactly this code, it should be something close.

Not sure if I’m implementing this right? At the moment, the cube just sort of flops around…

import bge
from mathutils import Vector

cont = bge.logic.getCurrentController()
obj = bge.logic.getCurrentScene().objects
cam = obj["Camera"]
own = cont.owner

forwardVector = own.getAxisVect((1, 0, 0))
cameraRotation = forwardVector.rotation_difference(own.worldPosition - cam.worldPosition)
rotatedVector = cameraRotation * (cameraRotation * forwardVector)
torque = 100*1
own.applyTorque(torque*rotatedVector,0)

Isn’t there some way to directly access the vector’s ‘rotation’ variable?

That doesn’t make any sense to me?

But here you are playing with torque, which is working differently from regular vectors.

rotatedVector represents a the forward direction rotated twice, maybe you can use another .rotation_difference to get the effective rotation, convert that to euler and hopefully it works?

But are you really sure about using a torque?

Well, the cube is part of a ragdoll, and I’m using torque to pose his body parts in accordance with an identical, animated set. His arms are supposed to point away from the camera, but I don’t want to have to rotate the camera all the way up or down in order for him to fully raise/lower his arms.
If the vector between the arm-cube and camera had a ‘globalOrientation’ or ‘rotation’ attribute stored, all I’d have to do would be to write,

whateverVector.rotation *= 2