Ray-Casting Between An Entity And Another Group Of Entities

← All Topics
You are here:

Ray-Casting Between An Entity And Another Group Of Entities

Ray casting is a technique for finding (or “picking”) the closest entity from a group of entities on the screen by sending a linear “Ray” from some entity (3D model or mouse pointer) towards that group of entities for example:

m1 is a dragon having pos=(3,-2,0)
m1 belongs to the enemies group
m2 is a sinbad having pos=(-3,0,0)
m2 belongs to the enemies group

s1 is a sight sprite

function rotate_picked_entity(m) = do
  m.turn left 30 in 1 second
end do

when key left is pressed do
  s1.move left 1 in 0.1 seconds
end do

when key right is pressed do
  s1.move right 1 in 0.1 seconds
end do

// perform ray casting here
when key space is pressed once do
  if enemies.ray check from (s1) then do
    run rotate_picked_entity(enemies.hit)
  end do 
end do

Notice that the enemies.hit object is the closest entity which collides with the ray sent from the s1 entity

Previous Printing Text On The Screen
Next Responding To User Interaction