Hiho! Me again with my annoying questions…
It’s python again. So, I want to calculate a relative position of a mouse pointer to the camera. For this first I need is the orientation of the camera and the orientation of a ray (for the getting the hit position in 3d space). Well, I fetch the orientation of both with getOrientation and this should return me a 3x3 rotation matrix, right? So, if no rotation is applied to the objects then the directional vector should be (0,1,0) which means that this would be my basic vector. If now I change the orientation of the cam or the ray I should get the new directional vector if I apply the matrix getOrientation returned on that vector.
Would be something like that:
import GameLogic
def mult(mat, vec):
nx = mat[0][0] * vec[0] + mat[0][1] * vec[0] + mat[0][2] * vec[0]
ny = mat[1][0] * vec[1] + mat[1][1] * vec[1] + mat[1][2] * vec[1]
nz = mat[2][0] * vec[2] + mat[2][1] * vec[2] + mat[2][2] * vec[2]
return [nx,ny,nz]
def orient2vec(mat):
return mult(mat, [0,1,0])
cont = GameLogic.getCurrentController()
owner = cont.getOwner()
orient = owner.getOrientation()
GameLogic.orient_cam = orient2vec(orient)
GameLogic.cam_pos = owner.getPosition()
Or am I wrong?
Then I calculate the position of the pointer (on a (imaginary) plane at a certain distance to the camera - here 3 units).
For that purpose I did the following:
#
# Maus-Skript / Pointer setzen
#
import GameLogic
import Rasterizer
from math import *
cont = GameLogic.getCurrentController()
sensor = cont.getSensor("always")
owner = cont.getOwner()
cam_pos = GameLogic.cam_pos
posx,posy,posz = cam_pos
orient_cam = GameLogic.orient_cam
cx,cy,cz = orient_cam
orient_ray = GameLogic.orient_ray
rx,ry,rz = orient_ray
r_vector_len = sqrt(rx*rx + ry*ry + rz*rz) / cx*rx*3 + cy*ry*3 + cz*rz*3
rx = r_vector_len * rx
ry = r_vector_len * ry
rz = r_vector_len * rz
# |r| = sqrt( bx² + by² +bz² ) / ax * bx + ay * by + az * bz
#
# Pointerposition setzen
#
owner.setPosition([posx + rx, posy + ry, posz + rz])
I can’t say for sure if the math in here is fully correct. It should be a triangulation of the desired point. But I think that it should work.
Yes, the globals are defined as emtpy arrays at startup…
Anyone an idea what’s wrong?