This is a simple function I developed for a project I’m working on. Funny, didn’t seem simple when I was trying to figure it out. This function will put a selection box around a selected object, regardless of the objects position, rotation or offset. I am working with loaded models in my program. So the biggest hurdle was correcting for the models center position. Anyway I thought this might be useful to others, so I decided to share.
Remstart
===========================================
Project: 3D Selection Box
Created: July 10, 2011
Version: 1.0.0
By: WickedX Software
===========================================
Remend
Rem ***** Start Main Source *****
` declare global variables
global SelBox as integer
global a# as float
global b# as float
global dist# as float
` initialize variables
a# = 180
b# = 45
dist# = 500
` set up display and sync
Sync On
Sync Rate 60
Backdrop On
AutoCam Off
`make object
Make Object Sphere 1, 200
Color Object 1, rgb( 0, 128, 0)
rem - if offsetting an object in code, be sure to recalculate its internal bounding box.
`Offset Limb 1, 0, 200, 0, 0, 1
` main loop
do
text 0,0,"Arrow key - orbit camera"
text 0,16, "W key - move camera forward"
text 0,32, "S key - move camera backward"
ControlCamera()
if mouseclick() = 1
objpicked = pick object(mousex(),mousey(),0,1)
MakeBox(objpicked)
endif
` update display
sync
loop
` end main loop
end
Rem ***** End Main Source *****
function MakeBox(obj)
if SelBox
Delete Object SelBox
endif
if obj
rotx# = Object Angle X(obj)
roty# = Object Angle Y(obj)
rotz# = Object Angle Z(obj)
Rotate Object obj, 0, 0, 0
cntrx# = Object Collision Center X(obj)
cntry# = Object Collision Center Y(obj)
cntrz# = Object Collision Center Z(obj)
Rotate Object obj, rotx#, roty#, rotz#
sizex# = Object Size X(obj)
sizey# = Object Size Y(obj)
sizez# = Object Size Z(obj)
posx# = Object Position X(obj)
posy# = Object Position Y(obj)
posz# = Object Position Z(obj)
SelBox = FreeObj()
` change the 1 in Make Object Box and the the x,y,y formula's in Offset Limb to change the box size.
Make Object Box SelBox, sizex#+1, sizey#+1, sizez#+1
Set Object Wireframe SelBox, 1
Position Object SelBox , posx#, posy#, posz#
Rotate Object SelBox, rotx#, roty#, rotz#
Offset Limb SelBox, 0, (cntrx#/sizex#)*(sizex#-1), (cntry#/sizey#)*(sizey#-1), (cntrz#/sizez#)*(sizez#-1), 1
endif
endfunction
function FreeObj()
repeat
inc obj
until object exist(obj) = 0
endfunction obj
function ControlCamera()
a# = wrapvalue(a# + rightkey())
a# = wrapvalue(a# - leftkey())
b# = wrapvalue(b# + upkey()): if b# > 179 then b# = 179
b# = wrapvalue(b# - downkey()): if b# < 1 then b# = 1
dist# = dist# + keystate(31): if dist# > 1000 then dist# =1000
dist# = dist# - keystate(17): if dist# < 50 then dist# = 50
position camera (sin(a#)*sin(b#)*dist#), (cos(b#)*dist#),(cos(a#)*sin(b#)*dist#)
point camera 0, 0, 0
endfunction
btw - This is my first code snippet. Hope someone may find it useful.
Brian.