TGC Codebase Backup



3D mouse, object selection, distance calculation (DBPro / DB) by David T

20th Sep 2003 7:40
Summary

A simple snippet demonstrating 3D mouse control, how to select objects and simple distance calculation. Ideal for somebody starting out with RTS development.



Description

3D mouse code by WhizzRich.

Runs in both DarkBASIC Professional and DarkBASIC.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    remstart
SIMPLE 3DMOUSE DEMO - DAVIDTATTERSALL / DAVID89
remend

rem make mouse
make object cube 1,5
position object 1,0,0,0

rem make other object
make object sphere 2,5
position object 2,0,0,0

rem Setup camera + mouse
position camera 0,30,-30
pitch camera down 30
position mouse 400,400

sync on

rem Main loop
do

rem Here we position the mouse object at the cursor's position (code by WhizzRich)
repeat
   if object screen x(1) < mousex()-3
      position object 1,object position x(1)+1,object position y(1),object position z(1)
   endif
   if object screen x(1) > mousex()+3
      position object 1,object position x(1)-1,object position y(1),object position z(1)
   endif
      if object screen y(1) < mousey()-3
      move object 1,-3
   endif
      if object screen y(1) > mousey()+3
      move object 1,3
   endif
until object screen x(1) =< mousex()+7 and object screen x(1) => mousex()-7 and object screen y(1) =< mousey()+7 and object screen y(1) => mousey()-7
rem -----------------------------------------------------------------------------------

rem Output
set cursor 10,10
print "Distance between objects: ";dist_obj(1,2)

if dist_obj(1,2)<4 then print "Mouse over!"
if dist_obj(1,2)<4 and mouseclick()=1 then print "Clicked On!"

sync

loop

rem Returns distance between o and o2
function dist_obj(o,o2)
ox#=object position x(o2)
oz#=object position z(o2)
oy#=object position y(o2)
dx#=object position x(o)-ox#
dz#=object position z(o)-oz#
dy#=object position y(o)-oy#
dist#=sqrt((dx#*dx#)+(dz#*dz#)+(dy#*dy#))
endfunction dist#