Quote: "Replacing every 40 with smaller numbers will give you more accurate detection when there are multiple objects on the screen at the cost of a larger detection area so IF you have many objects on the screen and expect some to overlap,shrink,grow , ect, you may want to decrease every instance to say 25 or even as low as 15. "
I made a 3d tic tac toe game the other day and I noticed the overlap problem, but to solve it I still used reasonably sized boxes but if the mouse was closer to the center of one box than the other I used the one it was closest to... I'm not very good at explaining it but this was my code:
set display mode 800,600,16
sync on
sync rate 0
cls
backdrop on
color backdrop rgb(155,100,100)
`set up player data
dim colarr(2)
colarr(1)=rgb(50,30,20)
colarr(2)=rgb(230,220,200)
player=1
dim pscore(2)
`prepare scoreboard
create bitmap 1,100,480
cls rgb(155,100,100)
make object plain 100,1,4.8
`player 1 screen
MakeRoundedBox(30,50,70,150,5,1,200,200,200,1,50,30,20,1,2,1)
ink rgb(200,200,200),0
set current bitmap 1
set text font "arial"
set text size 7
center text 50,50,"player 1"
line 30,65,70,65
`player 2 screen
MakeRoundedBox(30,250,70,350,5,1,50,30,20,1,230,220,200,1,2,1)
ink rgb(50,30,20),0
set current bitmap 1
center text 50,250,"player 2"
line 30,265,70,265
create bitmap 2,100,480
position camera 0,0,0
point camera 0,0,50
position object 100,2.8,0.003,4
lock object on 100
`update scoreboard
copy bitmap 1,2
set text size 9
ink rgb(200,200,200),0
center text 50,100,STR$(pscore(1))
ink rgb(50,30,20),0
center text 50,300,STR$(pscore(2))
get image 1,0,0,100,480
texture object 100,1
set current bitmap 0
`winner plane
create bitmap 4,330,180
create bitmap 3,330,180
ink rgb(150,0,0),0
box 0,0,329,179
ink rgb(200,170,0),0
line 10,10,319,10
line 10,169,319,169
line 10,10,10,169
line 319,10,319,169
set text font "arial"
set text size 10
center text 165,130,"[any key to continue]"
set text size 30
copy bitmap 3,4
set current bitmap 3
center text 165,60,"Player 1 Wins"
get image 3,0,0,329,179
set current bitmap 4
center text 165,60,"Player 2 Wins"
get image 4,0,0,329,179
make object plain 101,3.3,1.8
position object 101,0,0,3
texture object 101,3
hide object 101
lock object on 101
`base
make object box 13,3,0.2,3
color object 13,rgb(85,30,30)
`pieces
dim piecearr(3,3,3)
for x=1 to 3
for y=1 to 3
for z=1 to 3
t=((x-1)*9)+((y-1)*3)+z+13
make object sphere t,0.4
position object t,(x-2),y*0.4-0.1,(z-2)
scale object t,180,100,180
t#=t
if t#/2=int(t#/2)
color object t,colarr(1)
else
color object t,colarr(2)
endif
hide object t
next z
next y
next x
`piece placement detection boxes
dim objscrpos(59,1)
for x=1 to 3
for z=1 to 3
t=((x-1)*3)+z+50
make object cube t,0.6
scale object t,100,40,100
position object t,(x-2),1.5,(z-2)
color object t,rgb(255,255,100)
ghost object on t
hide object t
next z
next x
`poles
for x=1 to 3
for z=1 to 3
t=(x*3)+z
make object cylinder t,0.5
scale object t,45,300,45
position object t,(x-2),0.75,(z-2)
color object t,rgb(85,40,30)
next z
next x
`rotation object
make object cube 1,0.3
hide object 1
position camera -5,5,-5
point camera 0,0,0
do
gosub camerarotation
gosub checkmousepos
gosub placepiece
gosub showpieces
gosub checkwin
if winner>0 then gosub resetgameboard
sync
loop
placepiece:
`check that an action can be performed
if currobj>0
if mouseclick()=1
t=((objx-1)*9)+objz+13
objy=1
for y=1 to 3
if piecearr(objx,y,objz)>0
if y<3
objy=y+1
endif
endif
next y
piecearr(objx,objy,objz)=player
if player=1 : player=2 : else : player=1 : endif
repeat : until mouseclick()=0
endif
endif
return
showpieces:
for x=1 to 3
for y=1 to 3
for z=1 to 3
t=((x-1)*9)+((y-1)*3)+z+13
if piecearr(x,y,z)>0 then color object t,colarr(piecearr(x,y,z)) : show object t
next z
next y
next x
return
checkmousepos:
currobj=0
`piece placement detection boxes
for x=1 to 3
for z=1 to 3
t=((x-1)*3)+z+50
objscrpos(t,0)=object screen x(t)
objscrpos(t,1)=object screen y(t)
if SQRT(((mousex()-objscrpos(t,0))^2)+((mousey()-objscrpos(t,1))^2))<10
if currobj=0
currobj=t
objx=x
objz=z
else
if SQRT(((mousex()-objscrpos(t,0))^2)+((mousey()-objscrpos(t,1))^2))<SQRT(((mousex()-objscrpos(currobj,0))^2)+((mousey()-objscrpos(currobj,1))^2)) then currobj=t
endif
else
endif
`hide all boxes
hide object t
next z
next x
`show the currently selected box in the player colour
if currobj>0 then show object currobj : color object currobj,colarr(player)
return
camerarotation:
`ZOOM
`get the camera x angle and y position. These are used for zooming in and out
anglex#=camera angle x()
camy#=camera position y()
`detect up/down key presses and zoom accordingly
if upkey()=1 then camy#=newyvalue(camy#,anglex#,0.25)
if downkey()=1 then camy#=newyvalue(camy#,anglex#,-0.25)
`set limits for zoom
if camy#<2 then camy#=2
if camy#>13.5 then camy#=13.5
`ROTATION
`get the hidden rotation object's y position
angley#=object angle y(1)
`change this depending on keypress
if rightkey()=1 then yrotate object 1,wrapvalue(angley#+1.5)
if leftkey()=1 then yrotate object 1,wrapvalue(angley#-1.5)
`Position the camera 5 units in front of the rotate cube. This ensures that the camera is always
`the same distance away from the cube.
camx#=newxvalue(0,angley#,5) : camz#=newzvalue(0,angley#,5)
position camera camx#,camy#,camz#
`Make sure the camera remains pointed at the game board.
point camera 0,0,0
return
checkwin:
winner=0
`along z axis
for x=1 to 3
for y=1 to 3
if piecearr(x,y,1)>0 : if piecearr(x,y,1)=piecearr(x,y,2) and piecearr(x,y,2)=piecearr(x,y,3) then winner=piecearr(x,y,3) : endif
next y
next x
`along y axis
for x=1 to 3
for z=1 to 3
if piecearr(x,1,z)>0 : if piecearr(x,1,z)=piecearr(x,2,z) and piecearr(x,2,z)=piecearr(x,3,z) then winner=piecearr(x,1,z) : endif
next z
next x
`along x axis
for z=1 to 3
for y=1 to 3
if piecearr(1,y,z)>0 : if piecearr(1,y,z)=piecearr(2,y,z) and piecearr(2,y,z)=piecearr(3,y,z) then winner=piecearr(1,y,z) : endif
next y
next z
`diagonal x-z
for y=1 to 3
if piecearr(2,y,2)>0
if piecearr(1,y,1)=piecearr(2,y,2) and piecearr(2,y,2)=piecearr(3,y,3)
winner=piecearr(2,y,2)
endif
if piecearr(3,y,1)=piecearr(2,y,2) and piecearr(2,y,2)=piecearr(1,y,3)
winner=piecearr(2,y,2)
endif
endif
next y
`diagonal x-y
for z=1 to 3
if piecearr(2,2,z)>0
if piecearr(1,1,z)=piecearr(2,2,z) and piecearr(2,2,z)=piecearr(3,3,z)
winner=piecearr(2,2,z)
endif
if piecearr(3,1,z)=piecearr(2,2,z) and piecearr(2,2,z)=piecearr(1,3,z)
winner=piecearr(2,2,z)
endif
endif
next z
`diagonal y-z
for x=1 to 3
if piecearr(x,2,2)>0
if piecearr(x,1,1)=piecearr(x,2,2) and piecearr(x,2,2)=piecearr(x,3,3)
winner=piecearr(x,2,2)
endif
if piecearr(x,1,3)=piecearr(x,2,2) and piecearr(x,2,2)=piecearr(x,3,1)
winner=piecearr(x,2,2)
endif
endif
next x
`diagonal x-y-z
if piecearr(2,2,2)>0
if piecearr(1,1,1)=piecearr(2,2,2) and piecearr(2,2,2)=piecearr(3,3,3) then winner=piecearr(2,2,2)
if piecearr(3,1,3)=piecearr(2,2,2) and piecearr(2,2,2)=piecearr(1,3,1) then winner=piecearr(2,2,2)
if piecearr(1,1,3)=piecearr(2,2,2) and piecearr(2,2,2)=piecearr(3,3,1) then winner=piecearr(2,2,2)
if piecearr(3,1,1)=piecearr(2,2,2) and piecearr(2,2,2)=piecearr(1,3,3) then winner=piecearr(2,2,2)
endif
return
resetgameboard:
pscore(winner)=pscore(winner)+1
if winner=1 then texture object 101,3
if winner=2 then texture object 101,4
show object 101
suspend for key
hide object 101
for x=1 to 3
for y=1 to 3
for z=1 to 3
piecearr(x,y,z)=0
t=((x-1)*9)+((y-1)*3)+z+13
hide object t
next z
next y
next x
copy bitmap 1,2
set current bitmap 2
set text size 9
ink rgb(200,200,200),0
center text 50,100,STR$(pscore(1))
ink rgb(50,30,20),0
center text 50,300,STR$(pscore(2))
get image 1,0,0,100,480
texture object 100,1
set current bitmap 0
return
function MakeRoundedBox(x1,y1,x2,y2,cornrad,lthick,fr,fg,fb,boolbg,br,bg,bb,fbmp,nbmp,tbmp)
lthick#=lthick
bmpwidth=BITMAP WIDTH(fbmp)
bmpheight=BITMAP HEIGHT(fbmp)
create bitmap nbmp,bmpwidth,bmpheight
copy bitmap fbmp,nbmp
x1a=x1+cornrad
y1a=y1+cornrad
x2a=x2-cornrad
y2a=y2-cornrad
`background
if boolbg=1
ink rgb(br,bg,bb),0
box x1,y1a,x2,y2a
box x1a,y1,x2a,y2
for x=0 to cornrad
for y=0 to cornrad
if SQRT((x^2)+(y^2))<cornrad
dot x2a+x,y2a+y
dot x1a-x,y1a-y
dot x1a-x,y2a+y
dot x2a+x,y1a-y
endif
next y
next x
endif
`foreground
ink rgb(fr,fg,fb),0
for t=0 to lthick#-1
line x1a,y1+t,x2a,y1+t
line x1+t,y1a,x1+t,y2a
line x1a,y2-t,x2a,y2-t
line x2-t,y1a,x2-t,y2a
next t
for x=0 to cornrad
for y=0 to cornrad
if SQRT((x^2)+(y^2))>cornrad-(lthick#/2) and SQRT((x^2)+(y^2))<cornrad+(lthick#/2)
dot x2a+x,y2a+y
dot x1a-x,y1a-y
dot x1a-x,y2a+y
dot x2a+x,y1a-y
endif
next y
next x
copy bitmap nbmp,tbmp
endfunction
It was this part of the code that checked the mouse position:
checkmousepos:
currobj=0
`piece placement detection boxes
for x=1 to 3
for z=1 to 3
t=((x-1)*3)+z+50
objscrpos(t,0)=object screen x(t)
objscrpos(t,1)=object screen y(t)
if SQRT(((mousex()-objscrpos(t,0))^2)+((mousey()-objscrpos(t,1))^2))<10
if currobj=0
currobj=t
objx=x
objz=z
else
if SQRT(((mousex()-objscrpos(t,0))^2)+((mousey()-objscrpos(t,1))^2))<SQRT(((mousex()-objscrpos(currobj,0))^2)+((mousey()-objscrpos(currobj,1))^2)) then currobj=t
endif
else
endif
`hide all boxes
hide object t
next z
next x
`show the currently selected box in the player colour
if currobj>0 then show object currobj : color object currobj,colarr(player)
return
Here it checks the mouse position by cycling through all the boxes. If no box is currently selected and the mouse is within the detection area, then it uses that box as the currently selected one, but if a box is already selected it checks if the new box is closer to the mouse position, and changes the selected box if this is true.
Still not explaining this very well, and I'm not sure how helpful it'll be in your context, but just in case it helps...