i started working with math89s raycasting code and found it very usefull and merged it with my own code for the laser sight that i had started for my game.
so let me know wath you all think of it is it any usefull or yust crapp.
i do not usually enter snippets.
screenie is included.
and special thanks to math89 (france)
Rem Project: laser sight with raycast
rem by cliff mellangard of 3d energizers
rem snippet built around math89s raycast snippet so thanks and cred to him.:)
Rem Created: 2005-06-25 13:43:14
remstart
i modified this code for my game and wanted to share it if anyone else would find it useful.:)
i have tryed to make the code easy to understand for newbies. :)
a is the player object.
i wanted the code for the laser sight to be simple small and fast and work on as many objects
as possible without slowdowns.:)
it is a bit buggy when colliding with corners.
havent bin able to solve it because iam yust to crappy at it.:)
the ray code itself is small but i filled this snippet with alot of other things like making
the textures internally in the code so yust remove that stuff when using the code.:)
remend
Rem ***** Main Source File *****
sync on
sync rate 60
`create laser sight ray texture
cls
ink rgb(238,84,0),0
box 0,0,16,16
get image 1,0,0,16,16
`create player texture
cls
ink rgb(255,255,255),0
box 0,0,16,16
ink rgb(0,0,0),0
box 1,1,15,15
ink rgb(255,255,255),0
SET TEXT SIZE 8
TEXT 4,0,"a"
get image 2,0,0,16,16
`create floor texture
cls
ink rgb(0,100,0),0
box 0,0,16,16
get image 3,0,0,16,16
`create cubes texture
cls
ink rgb(0,0,255),0
box 0,0,16,16
ink rgb(255,0,0),0
box 1,1,15,15
get image 4,0,0,16,16
`create rotated 90 degrees cube texture
cls
ink rgb(255,0,0),0
box 0,0,16,16
ink rgb(0,0,255),0
box 1,1,15,15
get image 5,0,0,16,16
COLOR BACKDROP rgb(0,0,0)
`setup global variables
global a
global ray_startx#
global ray_starty#
global ray_startz#
global ray_endx#
global ray_endy#
global ray_endz#
global ray_hit_obj_scalx
global ray_hit_obj_scaly
global ray_hit_obj_scalz
global ray_hit_obj_x
global ray_hit_obj_y
global ray_hit_obj_z
`object nr to names
a=260:floor=261:ray=1:cube1=3:cube2=4:cube3=5:cube4=6:cube5=7:cube6=8:cube7=9
`create player object
make object cube a,20
texture object a,2
`initate start ray values
ray_startx#=object position x(a)
ray_starty#=object position y(a)
ray_startz#=object position z(a)
ray_endx#=ray_startx#
ray_endy#=ray_starty#
ray_endz#=ray_startz#+600
`create ray
make object box ray,2,2,100
make object sphere 5000,3
make mesh from object 5000,5000
delete object 5000
add limb 1,1,5000
`texture limb 1,1,1
offset limb 1,1,0,0,50
ghost object on ray
set object light ray,0
texture object ray,1
`create a cubes
make object cube cube1,50
position object cube1,200,0,500
scale object cube1,400,100,100
texture object cube1,4
make object cube cube2,50
position object cube2,400,0,200
texture object cube2,4
make object cube cube3,50
position object cube3,300,0,300
scale object cube3,200,100,100
texture object cube3,4
make object cube cube4,50
position object cube4,100,0,300
texture object cube4,4
make object cube cube5,50
position object cube5,300,0,100
scale object cube5,100,100,300
texture object cube5,4
make object cube cube6,50
position object cube6,300,0,600
scale object cube6,400,100,100
texture object cube6,4
`create cube that is rotated 90 degrees
make object cube cube7,50
position object cube7,600,0,400
scale object cube7,100,100,500
yrotate object cube7,90
texture object cube7,5
`create floor object
make object cube floor,50
position object floor,400,-50,400
scale object floor,1600,100,1600
texture object floor,3
ink rgb(255,255,255),0
cls
`<<<<<<<<<<<<<<<<<<<<<<<<<<<<main loop
do
set cursor 0,0 : print screen fps()
`various player data
yang#=yang#+mousemovex()*0.3
x#=object position x(a)
y#=object position y(a)
z#=object position z(a)
d#=125.0
h#=80.0
s#=6.0
set camera to follow 0,x#,y#,z#,yang#,d#,h#,s#,1
point camera 0,x#,y#,z#
move object a,(upkey()-downkey())*5
yrotate object a,camera angle y(0)
`starting with ray stuff
gosub reset_ray
gosub check_ray_collision
`initate ray calculation
raycast(ray,distance#)
fastsync
loop
`<<<<<<<<<<<<<<<<<<<<<<<<<<<<main loop end
reset_ray:
`get ray start data and end data
ray_startx#=object position x(a)
ray_starty#=object position y(a)
ray_startz#=object position z(a)
ray_endx#=ray_startx#
ray_endy#=ray_starty#
`only give ray full lenght if it aint colliding with more then one object
if colliding_with<1 and collison_ray_multi=0 then ray_endz#=ray_startz#+600
if colliding_with>1 then dec ray_endz#,80:collison_ray_multi=1
distance#=sqrt((ray_endx# - ray_startx#)^2+(ray_endy# - ray_starty#)^2+(ray_endz# - ray_startz#)^2)
return
check_ray_collision:
`reset the counter that counts how many objects the ray hits
colliding_with=0
`get data when ray hits object 1 to object before player obj
for obj=cube1 to a-1
if object exist (obj)
if object collision(obj,1)=0 then collison_ray_multi=0 `bugg fix add this here sorry :)
if object collision(obj,1)=1
`calculate how many objects the ray collides with
colliding_with=colliding_with+1
`if ray collides with less that 2 objects enable full lenght on ray
if colliding_with<2 then collison_ray_multi=0
`Closest object gets hit and grabb its data
ray_hit_obj_x=object position x(obj)
ray_hit_obj_y=object position y(obj)
ray_hit_obj_z=object position z(obj)
if object angle y (obj)=0 then ray_hit_obj_scalx=OBJECT SIZE X(obj)/2
ray_hit_obj_scaly=OBJECT SIZE y(obj)/2
if object angle y (obj)=0 then ray_hit_obj_scalz=OBJECT SIZE z(obj)/2
`if object hit is rotated 90 degrees switch x size to z size instead
if object angle y (obj)=90 then ray_hit_obj_scalx=OBJECT SIZE z(obj)/2
if object angle y (obj)=90 then ray_hit_obj_scalz=OBJECT SIZE x(obj)/2
endif
endif
next obj
return
function raycast(ray_obj,dist)
position object ray_obj,ray_startx#,ray_starty#,ray_startz#
ax#=object angle x(a)
ay#=object angle y(a)
nx=ray_startx#
ny=ray_starty#
nz=ray_startz#
for D=1 to dist
nx=ray_startX# + sin(aY#)*cos(ax#)*D
ny=ray_starty# - sin(aX#)*D
nz=ray_startz# + cos(aY#)*cos(aX#)*D
if nx > ray_hit_obj_x - ray_hit_obj_scalx then if nx < ray_hit_obj_x + ray_hit_obj_scalx
if ny > ray_hit_obj_y - ray_hit_obj_scaly then if ny < ray_hit_obj_y + ray_hit_obj_scaly
if nz > ray_hit_obj_z - ray_hit_obj_scalz then if nz < ray_hit_obj_z + ray_hit_obj_scalz
`if the point is inside the obj, we stop ray
exit
endif
endif
endif
next D
`position the ray
position object ray_obj,(nx + ray_startx#)/2.0,(ny + ray_starty#)/2.0,(nz + ray_startz#)/2.0
dist#=sqrt((nx - ray_startx#)^2+(ny - ray_starty#)^2+(nz - ray_startz#)^2)
`give the ray the accurate length and point in the same direction as the player
scale object ray_obj,100,100,dist#-3.5
yrotate object ray_obj,object angle y(a)
endfunction
fixed an tiny bugg the code is ok now
AMD ATHLON 1.8 XP WITH AN GEFORCE 4 TITANIUM 4600 (128 MB) AND 256 MB DDRAM (266).