I just had my 1st go with ray casting and this is what I come up with
sync on : sync rate 100
cls
hide mouse
set display mode 800,600,32
set ambient light 0
color ambient light RGB(0,0,0)
backdrop on
color backdrop rgb(0,0,0)
fog on
fog color rgb(0,0,0)
FOG DISTANCE 250
make light 1
position light 1,0,10,0
color light 1,rgb(255,0,0)
`set spot light 1,45,90
position camera 0,0,-50
autocam off
position camera 0,10,-20
phy start
`load image "lab1_dr4b.BMP",1
`*** uesr array ***
type cube
mass1 as float
mass2 as float
mass3 as float
xpos as float
ypos as float
zpos as float
alive as float
exist as boolean
objXpos# as float
objYpos# as float
objZpos# as float
sqrt1# as float
raycast as float
endtype
`*** make the usea array ***
dim falling_objects(1000) as cube
`*** add mass to all objects, and the total number of objects, and there position ***
for i = 1 to 1000
`*** ***
falling_objects(i).mass1 = 100
falling_objects(i).mass2 = 100
falling_objects(i).mass3 = 100
`*** ***
falling_objects(i).exist = 1
falling_objects(i).alive = 1
next i
`*** make floor ***
make object plain 2000,500,500
xrotate object 2000,-90
position object 2000,0,-40,0
color object 2000,rgb(0,0,0)
phy make rigid body static box 2000
`*** make object to sleect object to pick up useing raycasting***
make object sphere 1001,1
position object 1001,0,0,20
remstart
`*** this is the object that casts the ray ***
make object sphere 1002,1
`*** this is the destantion object for the cube repositioning ***
make object sphere 1003,10
position object 1003,0,60,0
remend
`*** object ID ***
ID as integer
ID = 1
FI = 1001
sqrtraycast# as float
`*** magnet object positions ***
xm# as float
ym# as float
zm# as float
`*** start of loop ***
do
`*** move magnet object ***
if scancode() = 16 then zm# = zm# + 0.75
if scancode() = 17 then ym# = ym# - 0.75
if scancode() = 18 then zm# = zm# - 0.75
if scancode() = 30 then xm# = xm# - 0.75
if scancode() = 31 then ym# = ym# + 0.75
if scancode() = 32 then xm# = xm# + 0.75
set cursor 0,70 :print "keyboard key nomber: ";scancode()
phy set rigid body kinematic position 1001,xm#,-ym#,zm#
`*** camera controls ***
camera_control()
`*** raycater function ***
raycaster()
`*** position etc ***
position_ect()
`*** make and delete objects
gosub falling_object
`gosub shooter
`*** out to the sreen fames per second and object timer for deletion ***
set cursor 0,10 : print "fps: ";screen fps()
set cursor 0,20 : print "object timer: ";timer1
`*** gravity for all physics objects ***
phy set gravity 0,-9.8,0
`*** end loop, update physics, ***
phy update
sync
loop
phy end
`*** raycater function ***
function raycaster()
set cursor 0,40 :print "object intersected ", phy get ray cast object ( )
distcast# = phy get ray cast distance ( )
set cursor 0,50 : print "object distance: ";distcast#
position object 1001,camera position x(),camera position y(),camera position z()
`*** ***
x# = object position x(1001)
y# = object position y(1001)
z# = object position z(1001)
set object to camera orientation 1001
move object 1001, 1
xvec# = object position x(1001) - x#
yvec# = object position y(1001) - y#
zvec# = object position z(1001) - z#
`Now cast your ray
ray = phy ray cast closest shape ( x#,y#,z#,xvec#,yvec#,zvec# )
for i = 1 to 1000
falling_objects(i).raycast = phy get ray cast object ( )
position object 1001, phy get ray cast hit point x ( ) ,phy get ray cast hit point y ( ),phy get ray cast hit point z ( )
`*** this gets the number of the object that the raycast object is pointing at and then colours them repositions them ***
if falling_objects(i).raycast > 1 and falling_objects(i).raycast <= 1000 and spacekey()=1
if falling_objects(i).exist > 0
if distcast# > 0 and distcast# < 100
color object falling_objects(i).raycast,rgb(0,255,0)
phy set rigid body position falling_objects(i).raycast, object position x(1001),object position y(1001),object position z(1001)
endif
endif
endif
next i
endfunction
`*** camera controls ***
function camera_control()
yrotate camera wrapvalue(camera angle y() + mousemovex()*0.3)
xrotate camera wrapvalue(camera angle x() + mousemovey()*0.3)
if mouseclick()=1 then move camera 0.75
if mouseclick()=2 then move camera -0.75
endfunction
function position_ect()
randomize timer()
for i = 1 to 1000
`*** object position / exist / mass etc ***
falling_objects(i).xpos = rnd(80)-rnd(80)
falling_objects(i).ypos = rnd(40)-rnd(20)
falling_objects(i).zpos = rnd(80)-rnd(80)
falling_objects(i).exist = object exist(i)
falling_objects(i).mass1 = rnd(299)+101
falling_objects(i).mass2 = rnd(299)+101
falling_objects(i).mass3 = rnd(299)+101
if falling_objects(i).exist > 0
falling_objects(i).objXpos# = object position x(i)
falling_objects(i).objYpos# = object position y(i)
falling_objects(i).objZpos# = object position z(i)
falling_objects(i).sqrt1# = sqrt((falling_objects(i).objXpos# - camera position x())^2 + (falling_objects(i).objYpos# - camera position y())^2 + (falling_objects(i).objZpos# - camera position z())^2)
endif
next i
endfunction
`*** make falling object ***
falling_object:
`make falling objects
if ID > 0 and ID < 1000
inc ID
`for ID = 1 to 999
`*** make physic object ***
make object cube ID,1
`texture object ID,1
set object specular ID, rgb ( rnd ( 255 ), rnd ( 255 ), rnd ( 255 ) )
set object specular power ID, 255
set object ambient ID, 0
scale object ID,falling_objects(ID).mass1,falling_objects(ID).mass2,falling_objects(ID).mass3
phy make rigid body dynamic box ID
phy set rigid body position ID,falling_objects(ID).xpos,falling_objects(ID).ypos,falling_objects(ID).zpos
`phy sleep rigid body ID
`next ID
endif
set cursor 0,0 : print "number of objects: ";ID
`max number of objects sets off timer
if ID = 1000 then inc timer1
`delete objects and reset
if timer1 >= 1000
for i = 1 to 1000
if falling_objects(i).exist > 0
phy delete rigid body i
delete object i
endif
next i
ID = 1
timer1 = 0
endif
`delete object that gettes to closs ********************************
remstart
for i = 1 to 1000
if falling_objects(i).exist > 0
if falling_objects(i).sqrt1# > 1 and falling_objects(i).sqrt1# < 3
endif
endif
next i
remend
`*************************************************************************
return
ok so tell me what you think, and let me know if you have any way to make it run faster, or just changes that you think I should make.
soul sucking devils, twisted body of the damed, slivering slim drips from ever poor, sin licking at your ears, and the smell stinging your eyes, and if you don't like it, get out of my kitchen!....