Sometime it would be handy to use the mouse to select or to move a object in 3d space. Imagine writing a checker game or a real time strategy game. One of the problems is that the mouse moves over the screen (a 2d plane) but the objects are located in a 3d space.
DarkBASIC makes it possible to get the position of any given object on the screen, therefore it is possible to write code to determine when the mouse moves over an objects representation on the screen.
here is a example in
DarkBASICclassic:
remstart
--------------------------------------------------------------------------------
[DBC]
(C) 2008 Attla
This code shows the possibility to select and move objects in 3d space
using the mouse.
the code does not need any media (it creates the necessary bitmaps)
You may add more objects and increase the MaxObjects variable
--------------------------------------------------------------------------------
remend
set display mode 800,600,16
sync on
dim Hint_Text$(16)
set text font "Courier New"
` Create some Media to use as textures
gosub CreateExitSign
gosub CreateBoard
set text size 14
set text transparent
set text to bold
autocam off
randomize timer()
set ambient light 50
ink rgb(255,255,255),0
backdrop on
color backdrop rgb(64,64,128)
sync rate 0
cls
`
` We need a board or ground to move the objects around
`
make matrix 1, 400,400,9,9
prepare matrix texture 1,2,2,1
`
` Set the tiles to a checkerboardlike texture
`
set matrix wireframe off 1
for x=0 to 8
for y=0 to 8
if sw=1
set matrix tile 1, x,y, 1
sw=2
else
set matrix tile 1, x,y, 2
sw=1
endif
next y
next x
update matrix 1
sync
`add Light
make light 1
set spot light 1, 15,30
position light 1,0,50,-100
color light 1,255,255,255
` Shadow (no real shadow but objects will look more real
` when one side is darker than the other)
make light 2
set spot light 2, 15,30
position light 2,0,50,-100
color light 2,-255,-255,-255
point light 1, 240,0,240
point light 2, 240,0,240
position camera 200,40,-40
point camera 200,0,200
` We create some different objects to show it is possible
` to use any object for a interface or a game
`
make object plain 1,50,50
texture object 1,1
position object 1, 200,60,200
set object 1, 1,0,0
Hint_Text$(1)="PLAIN [Exit-sign]"
make object cylinder 2,10
color object 2, rgb(255,0,0)
set object 2, 1,0,0
position object 2, 180,5,20
Hint_Text$(2)="CYLINDER"
make object sphere 3,16
color object 3, rgb(255,0,0)
set object 3, 1,0,0
position object 3, 230,8,90
Hint_Text$(3)="SPHERE"
make object cone 4,14
color object 4, rgb(255,0,0)
set object 4, 1,0,0
position object 4, 140,7,80
Hint_Text$(4)="CONE"
make object cube 5,10
color object 5, rgb(255,0,0)
set object 5, 1,0,0
position object 5, 230,5,40
Hint_Text$(5)="CUBE"
make object triangle 6, -10,0,0, 10,0,0, 0,20,0
position object 6, 200,10,60
color object 6, rgb(255,0,0)
yrotate object 6, 45
Hint_Text$(6)="TRIANGLE"
MaxObjects=6
r#=0
Exit_Key=0
while Exit_Key=0
mx = mousex()
my = mousey()
mmy=mousemovey()
` Check for Exit-Sign Mouse-Move-Over
`
` To use another possibility we do not change the objects color on Mouse over
` but we ghost the object, to show that the mouse is over the object
`
ox = object screen x(1)
oy = object screen y(1)
if mx >= (ox-50) and mx <= (ox+50) and my >= (oy-50) and my <= (oy+50)
ghost object on 1
else
ghost object off 1
endif
text 10,10, "Mouse position on screen: "+str$(mx)+"/"+str$(my)+" FPS:"+str$(screen fps())
for obj=1 to MaxObjects
ox = object screen x(obj)
oy = object screen y(obj)
hint$=Hint_Text$(obj)+":"
while len(hint$) < 18 : hint$=hint$+chr$(32) : endwhile
ox$=str$(ox)
oy$=str$(oy)
text 10,20+(obj*15), hint$+" No:"+str$(obj)+" Pos. on Screen x:"+ox$+" y:"+oy$+" Position 3D: x:"+str$(object position x(obj))+" y:"+str$(object position y(obj))+" z:"+str$(object position z(obj))
if mx >= (ox-50) and mx <= (ox+50) and my >= (oy-50) and my <= (oy+50)
if obj=1 and mouseclick() then Exit_Key=1 : ` Was the exit sign selected?
if mouseclick()=1 : ` if the object is selected with a mouse-click we store
if MoveObject=0 : ` the objectsnumber in the variable MoveObject
MoveObject=obj
else
MoveObject=0
endif
while mouseclick():sync:endwhile
endif
if MoveObject=obj : ` if the object the mouse is over is selected we color it blue
if obj <> 1 then color object obj, rgb(8,8,255)
else : ` else we color it yellow to indicate that the mouse is
` over the object
if obj <> 1 then color object obj, rgb(255,255,0)
endif
else
if obj <> 1 then color object obj, rgb(255,2,2)
endif
next obj
` the variable MoveObject indicates there is a object selected if not 0
` and it contains the number of the object
if MoveObject
if mousemovex()
if mx > oldmx
px=object position x(MoveObject)+4
position object MoveObject,px, object position y(MoveObject), object position z(MoveObject)
if px > 390 then px=390
else
if mx < oldmx
px=object position x(MoveObject)-4
if px < 10 then px=10
position object MoveObject,px, object position y(MoveObject), object position z(MoveObject)
endif
endif
endif
if mmy > 0
pz=object position z(MoveObject)-4
if pz > 390 then pz=390
position object MoveObject, object position x(MoveObject), object position y(MoveObject), pz
endif
if mmy < 0
pz=object position z(MoveObject)+4
position object MoveObject,object position x(MoveObject), object position y(MoveObject), pz
endif
` move the mouse with the selected object, but keep it in the screen
mx=object screen x(MoveObject)
my=object screen y(MoveObject)
position mouse mx, my
endif
oldmx=mx
oldmy=my
` lets rotate the EXIT sign as a nice effect
r#=r#+.5
yrotate object 1, wrapvalue(r#)
sync
endwhile
end
`--------------------------------------------------------------------------------
` In the Media-Section the media for the program are generated
` allowing the program to run without any media
`--------------------------------------------------------------------------------
CreateExitSign:
`--------------------------------------------------------------------------------
create bitmap 1,128,128
set current bitmap 1
ink rgb(0,0,0),0
box 1,1,128,128
ink rgb(255,0,0),0
box 128/3,1,2*128/3,5
box 128/3,122,2*128/3,127
box 1,128/3,5,2*128/3
box 122,128/3,127,2*128/3
line 128/3,1,1,128/3
for h=0 to 5
line 128/3,1+h,1,128/3+h
line 1+h,2*128/3,128/3+h,128
line 2*128/3,h,128,128/3+h
line 2*128/3,121+h,121+h,2*128/3
next h
ink rgb(255,255,255),0
set text size 48
set text to bold
text 13,38,"EXIT"
get image 1,0,0,128,128
set current bitmap 0
return
`--------------------------------------------------------------------------------
CreateBoard:
`--------------------------------------------------------------------------------
create bitmap 2,128,128
set current bitmap 2
ink rgb(255,255,255),0
box 1,1,64,127
ink rgb(2,2,2),0
box 64,1,127,127
get image 2,0,0,128,128
set current bitmap 0
return
You can cut and paste the code into your DarkBASIC-Editor and try it out. The code does not need any media (it creates the necessary bitmaps) itself.
- Select the objects with a mouse click and push them around.
- A click on the Exit sign will exit the program.
You can add more objects in the code, but you then must increase the variable
MaxObjects.
The program will work in
DarkBASICpro too, but the "SET OBJECT no,1,0,0" commands (for cull and transparency) must be changed then to the appropriate
DarkBASICpro-commands.
For your convenience the code is although attached as RAR-file (aprox. 2kB), which allows for a comfortable download.
enjoy
Attila