I've recently seen a few requests for a function that converts any X/Y/Z worldspace position into its equivalent X/Y camera screen position.
Phaelax and DmitryK have previously provided two excellent solutions for this problem. I thought for completeness I'd post mine as well.
In the case of the code below x vector3(1) and y vector3(1) contain the resulting X/Y camera screen coordinates. If they are larger than the size of the screen or are minus numbers then the X/Y/Z worldspace position is outside the camera's screen. If they are smaller than the size of the screen then the X/Y/Z worldspace position is either within the camera screen or behind it.
The code below also has some added functionality in that product# tells you whether the X/Y/Z worldspace position is in front of or behind the camera screen. Product# varies between 1.0 and -1.0. If product# is positive then the position is somewhere in front of the camera screen. The closer that product# gets to being 1.0 the closer the X/Y/Z worldspace position is to being bang in the centre of the camera screen. Vice versa is also true. In other words, if product# is negative then the X/Y/Z position is somewhere behind the camera screen. The closer product# gets to being -0.1 the closer the X/Y/Z position gets to being directly behind the very centre of the camera screen.
Cheers
Philip
sync on
sync rate 60
make object sphere 1, 30
x_pos = rnd(1000)
y_pos = rnd(1000)
z_pos = 0
position object 1, x_pos, y_pos, z_pos
color object 1, rgb(0, 255, 0)
position camera 0, 0, 2000
point camera 0, 0, 0
do
if leftkey()> 0
turn camera left 0, 1
endif
if rightkey() > 0
turn camera right 0, 1
endif
if spacekey() > 0
x_pos = rnd(1000)
y_pos = rnd(1000)
z_pos = rnd(10)
position object 1, x_pos, y_pos, z_pos
endif
3d_point_to_2d_screen(x_pos, y_pos, z_pos)
text 0, 0, "Object pos (X/Y/Z): " + str$(x_pos) + " " + str$(y_pos) + " " + str$(z_pos)
sync
loop
FUNCTION 3d_point_to_2d_screen(x_pos AS integer, y_pos AS integer, z_pos AS integer)
null = make vector3(1)
null = make vector3(2)
null = make matrix4(3)
null = make matrix4(4)
null = make matrix4(5)
null = make vector3(6)
projection matrix4 3
view matrix4 4
world matrix4 5
set vector3 2, x_pos, y_pos, z_pos
project vector3 1, 2, 3, 4, 5
text 0, 25, "X pos result of function: " + str$(x vector3(1)) + " OR result of object screen x(): " + str$(object screen x(1))
text 0, 40, "Y pos result of function: " + str$(y vector3(1)) + " OR result of object screen y(): " + str$(object screen y(1))
pick screen 512, 384, 1
set vector3 6, get pick vector x(), get pick vector y(), get pick vector z()
set vector3 2, x_pos - camera position x(0), y_pos - camera position y(0), z_pos - camera position z(0)
normalize vector3 2, 2
product# = dot product vector3(6, 2)
text 0, 55, "Product: " + str$(product#)
null = delete vector3(1)
null = delete vector3(2)
null = delete matrix4(3)
null = delete matrix4(4)
null = delete matrix4(5)
null = delete vector3(6)
ENDFUNCTION
What do you mean, bears aren't supposed to wear hats and a tie? P3.2ghz / 1 gig / GeForce FX 5900 128meg / WinXP home