@kaisertal
I experimented somewhat further with the ray cast tutorial.
The following adaptation shows how you can pass a ray trough two
points. A valid direction vector is calculated.
As such the program does not make much sense.
The ray is always going trough the box on the right.
` this is a simple demonstration of ray casting
` set up the program
phy start
autocam off
sync on
sync rate 60
color backdrop 0
position camera 0, 20, -70
rem stripe textures
load image "stripe5.png",8
make object sphere 8,400,48,48
texture object 8,8
scale object texture 8,6,6
set object cull 8,0
load image "stripe6.png",1
` create some objects
make object cube 1, 5
position object 1, 0, 0, 0
phy make rigid body static box 1
make object cube 2, 5
position object 2, -15, 0, 0
phy make rigid body static box 2
make object cube 3, 5
position object 3, 15, 0, 0
phy make rigid body static box 3
make object cube 4, 5
position object 4, 0, 20, 0
make object box 5, 1, 15, 1
position object 5, 0, 15, 0
texture object 4,1
texture object 5,1
` show Dark Physics logo
load image "logo.png", 100000
sprite 1, 0, 600 - 60, 100000
` main program loop
do
` display information
set cursor 0, 0
print "This program demonstrates ray casting in action"
print "The ray casting is taking place from the object at the top of the screen"
print "When it hits an object that object turns blue"
print "Use the arrow keys to move the position of the ray cast left and right"
` set all objects to black
for i = 1 to 3
color object i, 0
next i
` get the position of object 4
x# = object position x ( 4 )
y# = object position y ( 4 )
z# = object position z ( 4 )
rem px#,py#,pz# a second point you want your ray to go trough. In this case the box to the right position.
rem change this value if you want
px# = 15
py# = 0
pz# = 0
rem x#,y#,z# the coördinates of the object you can move with the arrowkeys. From here the ray starts.
rem calculate the difference.
rem You get a vector from one point to another.
rx# = px# - x#
ry# = py# - y#
rz# = pz# - z#
rem calculate the vector length
l# = vectorlength( rx#,ry#,rz#)
rem normalise the vector
rx# = (px# - x#)/l#
ry# = (py# - y#)/l#
rz# = (pz# - z#)/l#
` cast a ray down
rem value = phy ray cast all shapes ( x#, y#, z#, 0, -1, 0 )
value = phy ray cast all shapes ( x#, y#, z#, rx#, ry#, rz# )
` if the ray hit something then colour that object blue
if phy get ray cast hit( )= 1
color object phy get ray cast object ( ), rgb ( 0, 0, 255 )
endif
`move the ray cast left
if leftkey ( )
move object left 4, 0.4
move object left 5, 0.4
endif
` mvoe the ray cast right
if rightkey ( )
move object right 4, 0.4
move object right 5, 0.4
endif
` update the simulation and screen
phy update
text 0,100,str$(rx#) + " " + str$(ry#) + " " +str$(rz#) + " "
sync
loop
function vectorlength( rx#,ry#,rz#)
l# = sqrt(rx# ^ 2 + ry# ^ 2 + rz ^ 2)
endfunction l#