While you're iterating through an arry ( list ) with for..next, you have to decrease your counter when deleting items in the loop, like:
for i=1 to shot.length
if shot[i].x > 100 or shot[i].x < 0 or shot[i].y > 100 or shot[i].y < 0 // shot outside screen?
DeleteSprite ( shot[i].ID ) // yeah, so delete the sprite
shot.remove(i) // and remove the shot from the list
dec i // THIS ONE IS IMPORTANT, BECAUSE WE ARE KILLING LIST ENTRIES WHILE ITERATING THROUGH THE LIST
endif
next i
Also, here's a simple method to do raycasts from from 2D mouse position into a 3D world. Just click on the spheres to delete them:
SetErrorMode ( 2 )
SetWindowSize ( 1024, 768, 0 )
SetVirtualResolution ( 1024, 768 )
SetSyncRate ( 60, 0 )
UseNewDefaultFonts ( 1 )
// CAM stuff
SetCameraRange ( 1, 1, 500 )
SetCameraPosition ( 1, 0, 0, -100 )
SetCameraLookAt ( 1, 0, 0, 0, 0 )
// create some spheres and scatter them in front of the cam
dim sphere[9] as integer
for i = 0 to 9
sphere[i] = CreateObjectSphere ( 10, 10, 10 )
SetObjectPosition ( sphere[i], random ( 0, 100 ) -50, random ( 0, 80 ) -40, random ( 0, 50 ) )
SetObjectColor ( sphere[i], random ( 0, 255 ), random ( 0, 255 ), random ( 0, 255 ), 255 )
next i
do
if GetRawMouseLeftPressed()
mouseX = ScreenToWorldX ( GetPointerX() )
mouseY = ScreenToWorldY ( GetPointerY() )
// check for object hit
object_hit = ShootRay ( 0, mouseX, mouseY )
// if an object has been hit, delete it
if object_hit <> 0
if GetObjectExists ( object_hit )
DeleteObject ( object_hit )
endif
endif
endif
Sync()
loop
function ShootRay ( objectID, mx, my )
distance = 500
unit_x# = Get3DVectorXFromScreen ( mx, my ) // vectors are normalized here, so we use a factor of 500 when calculating our end positions to extend our vectors into the world
unit_y# = Get3DVectorYFromScreen ( mx, my )
unit_z# = Get3DVectorZFromScreen ( mx, my )
start_x# = GetCameraX ( 1 )
start_y# = GetCameraY ( 1 )
start_z# = GetCameraZ ( 1 )
end_x# = distance * unit_x# + start_x#
end_y# = distance * unit_y# + start_y#
end_z# = distance * unit_z# + start_z#
hitID = ObjectRayCast ( objectID, start_x#, start_y#, start_z#, end_x#, end_y#, end_z# )
endfunction hitID
Hope that helps.
PSY