world coords into camera local coords

BlitzMax Forums/BlitzMax Beginners Area/world coords into camera local coords

KronosUK(Posted 2006) [#1]
I've been trying to write some code to convert an objects position from world coordinates into the cameras local coordinates. the camera can rotate around x and y axes.

This would then allow me to calculate pan and depth values for a sound coming from said object by just taking the x and z lengths.

In pseudo code I have been doing this.

newpos = object.pos - camera.pos

object.rotatexyz(newpos,-camanglex,-camangley,-camanglez)

However this doesn't work. What am I doing wrong?


Dreamora(Posted 2006) [#2]
What you need to do is doing matrix operations:


lposx = cos(angle)*posx + sin(angle)*posy - camx
lposy = -sin(angle)*posx + cos(angle)*posy - camy

This should be the correct equation when I'm not totally wrong.

The first part is a matrix multiplication of world pos with rotation matrix of the camera and substraction of the cam position repositions the world origin into the one of the camera


KronosUK(Posted 2006) [#3]
thats essentially what im doing and it ain't working..


Dreamora(Posted 2006) [#4]
What does your rotate method look like?
With 3 dimensional transformations, it is essential to combine the matrices correctly (ie in the correct order)


KronosUK(Posted 2006) [#5]
ah I think I have cracked it. By putting my opengl camera rotation instructions in the order z,y,x (they were in xyz before)and my "unrotations" in order x,y,z everything seems to work.

Well thats 2 days i'll never see again. I hate these problems where one is basically looking in totally the wrong place for the problem.grrr

Thanks Dreamora for your assistance anyway.