I have been trying to make a program to work out how much of the screen a 3d object takes up.
This program is supposed to draw a box around the object, and almost works properly, but from some angles the box is flat (widthwise) instead of touching the edges of the object.
sync rate 60:sync on:autocam off:hide mouse
ob=1
load object "woman1.x",ob: yrotate object ob, 180
set object 1, 1,1,0
position object ob, 0,0,2
sX#=object size x(ob)
sY#=object size y(ob)
sZ#=object size z(ob)
make object plain 255,100,100: xrotate object 255,90
position object 255,0,(sY#/-2)-1,0: color object 255, rgb(0,64,0)
DO
hdist#=sqrt((object position x(ob)-camera position x())^2+(object position z(ob)-camera position z())^2)
tdist#=sqrt(hdist#^2+(object position y(ob)-camera position y())^2)
gosub box
text 1,1,"Distance="+str$(tdist#)
text 1,40,"AngX="+str$(aX#)
text 1,60,"AngY="+str$(aY#)
text 1,80,"AngZ="+str$(aZ#)
gosub mousemove
if rx=1 then pitch object up 1,1
if ry=1 then turn object right 1,1
if rz=1 then roll object right 1,1
if inkey$()="x"
if rx=0 then rx=1 else rx=0
repeat:until scancode()=0
endif
if inkey$()="y"
if ry=0 then ry=1 else ry=0
repeat:until scancode()=0
endif
if inkey$()="z"
if rz=0 then rz=1 else rz=0
repeat:until scancode()=0
endif
SYNC
LOOP
mousemove:
xrotate camera wrapvalue(camera angle x()-mousemovey())
yrotate camera wrapvalue(camera angle y()+mousemovex())
if upkey() then move camera 0.2
if downkey() then move camera -0.2
return
box:
osx=object screen x(ob): osy=object screen y(ob)
`sizeY#/tdist#
aX#=wrapvalue(camera angle x()-object angle x(ob))
aY#=wrapvalue(camera angle y()-object angle y(ob))
aZ#=wrapvalue(camera angle z()-object angle z(ob))
` tidth#=abs(sX#*cos(ang#)+sZ#*sin(ang#))/2
2dwidth#= (abs(sX#*cos(aY#)*cos(aZ#))+abs(sY#*cos(aX#)*sin(aZ#))+abs(sZ#*sin(aX#)*sin(aY#)))/3
2dheight#=(abs(sX#*cos(aY#)*sin(aZ#))+abs(sY#*cos(aX#)*cos(aZ#))+abs(sZ#*sin(aX#)*sin(aY#)))/3
if tdist#>0
left#= osx-670*abs(2dwidth#)/tdist#
right#= osx+500*abs(2dwidth#)/tdist#
top#=osy-470*abs(2dheight#)/tdist#
bottom#=osy+800*abs(2dheight#)/tdist#
else
left#=-1
right#=-1
top#=-1
bottom#=-1
endif
line left#,top#,right#,top#
line left#,bottom#,right#,bottom#
line left#,top#,left#,bottom#
line right#,top#,right#,bottom#
if mouseclick()=1 then move camera 0.1
if mouseclick()=2 then move camera -0.1
if mouseclick()=4 then point camera object position x(ob), object position y(ob), object position z(ob)
return