This function will return the object colliding with the mouse as an integer. This function is great for selecting objects in your model maker, or selecting actually anything in 3 dimensions!
(Edit) Don`t copy the code from the code snippet! The TGC server somehow doubled the enters (as you can see) and it will take up 2 times the space. Download the zip file, the code "get_object_screen.dba.dba" is in it.
remstart
*****************************************************
* This function will return the object colliding *
* with the mouse as an integer value *
*****************************************************
* Author : TheComet Date : ? *
*****************************************************
remend
rem setup screen
sync on
sync rate 30
backdrop on
color backdrop 0
set camera range 1,1000000
#include "objects.dba"
rem make pointing object
make object cube 1,100000
position object 1,0,0,50000
scale object 1,0.4,0.4,0.4
set object 1,1,1,0
make object box 2,0.1,0.1,10000
set object 2,1,1,0
hide object 1
hide object 2
make object collision box 2,-0.05,-0.05,-5000,0.05,0.05,5000,1
rem make all 9 objects in a row
make object cube 3,10
make object sphere 4,10
make object cylinder 5,10
make object plain 6,10,10
make object cone 7,10
create object pyramid(8,10,4,1)
create object prism(9,10,1)
create object disk(10,10,24,1)
create object diamond(11,10,0,1)
position object 3,-30,10,rnd(50)+50
position object 4,-15,10,rnd(50)+50
position object 5,0,10,rnd(50)+50
position object 6,15,10,rnd(50)+50
position object 7,30,10,rnd(50)+50
position object 8,-30,-10,rnd(50)+50
position object 9,-15,-10,rnd(50)+50
position object 10,0,-10,rnd(50)+50
position object 11,15,-10,rnd(50)+50
rem make a collision box for all
for t=3 to 11:make object collision box t,-5,-5,-5,5,5,5,1:next t
rem main loop
do
rem info
center text 320,20,"Please click on and object"
rem position camera
position camera 0,0,0
rotate camera 0,0,0
rem get positions
x#=object position x(1)
y#=object position y(1)
cz#=camera position z()
cx#=camera position x()
cy#=camera position y()
rem position object at mouse
if object screen x(1)<>mousex()
if object screen x(1)>mousex() then position object 1,x#-2000,y#,50000
if object screen x(1)<mousex() then position object 1,x#+2000,y#,50000
endif
x#=object position x(1)
if object screen y(1)<>mousey()
if object screen y(1)>mousey() then position object 1,x#,y#+2000,50000
if object screen y(1)<mousey() then position object 1,x#,y#-2000,50000
endif
rem control pointer object
position object 2,0,0,0
point object 2,object position x(1),object position y(1),object position z(1)
rem collision
if mouseclick()=1
if object collision(2,3)=1 then center text 320,20,"Cube"
if object collision(2,4)=1 then center text 320,20,"Sphere"
if object collision(2,5)=1 then center text 320,20,"Cylnder"
if object collision(2,6)=1 then center text 320,20,"Plain"
if object collision(2,7)=1 then center text 320,20,"Cone"
if object collision(2,8)=1 then center text 320,20,"Pyramid"
if object collision(2,9)=1 then center text 320,20,"Prism"
if object collision(2,10)=1 then center text 320,20,"Disk"
if object collision(2,11)=1 then center text 320,20,"Diamond"
endif
sync
loop
rem this function will return the number of an object that the mouse is on
function get object screen()
rem reset obj
obj=0
rem make pointing object(if not exist)
if object exist(62065)=0
make object cube 62065,100000
position object 62065,0,0,50000
scale object 62065,0.4,0.4,0.4
hide object 62065
endif
if object exist(62066)=0
make object box 62066,0.1,0.1,10000
set object 62066,1,1,0
hide object 62066
make object collision box 62066,-0.05,-0.05,-5000,0.05,0.05,5000,1
endif
rem loop
time=0
repeat
inc time
rem get positions
x#=object position x(62065)
y#=object position y(62065)
cz#=camera position z()
cx#=camera position x()
cy#=camera position y()
rem position object at mouse
if object screen x(62065)<>mousex()
if object screen x(62065)>mousex() then position object 62065,x#-2000,y#,50000
if object screen x(62065)<mousex() then position object 62065,x#+2000,y#,50000
endif
x#=object position x(62065)
if object screen y(62065)<>mousey()
if object screen y(62065)>mousey() then position object 62065,x#,y#+2000,50000
if object screen y(62065)<mousey() then position object 62065,x#,y#-2000,50000
endif
rem control pointer object
position object 62066,cx#,cy#,cz#
point object 62066,object position x(62065),object position y(62065),object position z(62065)
rem collision
obj=object collision(62066,0)
rem boundary
if obj>0
if object exist(obj)=1 then position object 64000,object position x(obj),object position y(obj),object position z(obj)
endif
until time>10
endfunction obj
It`s pretty simple to use: All you have to be aware of, is to put a rotating collision box around that object. Thats all!
First include the file:
#include "get_object_screen.dba"
note that the file "get_object_screen.dba" has to be in the same directory as your program.
make object collision box (object),0-(sizex/2),0-(sizey/2),0-(sizez/2),sizex/2,sizey/2,sizez/2,1
Example:
if mouseclick()=1 then object_selected=get object screen()
File attached
TheComet
Oooooops!!! I accidentally formated drive c.