Thanks for weighing in, Torben.
I didn’t really think about that… As you say, it yields the same result, but according to the documentation using ‘Any’ is faster so I guess I might as well change it. actually, as stated in the docs,
unsorted : Return all results, in no particular order.
Any : Return one result, not necessarily the closest one. – “returns *one result” this would not work because the one result I’m checking for is the first hit.
Yeah, for my project I only need raycasting for my enemy tanks - as a form of environmental awareness sensing whether or not an object is in the enemy’s “firing path”, if you will.
As far as “masking” the results go, how would I do that?
As seen above, I use this:
EDray = physics.rayCast( self.x, self.y, user\_tank.x, user\_tank.y, "any" ) local hitFirst = EDray[1] local hitX, hitY = hitFirst.position.x, hitFirst.position.y if EDray then -- There's at least one hit. print( "Hit count: " .. tostring( #EDray ) ) -- Output all the results. for i,v in ipairs(EDray) do print( "Hit: ", i, v.object.class, " Position: ", v.position.x, v.position.y, " Surface normal: ", v.normal.x, v.normal.y ) end print( "The first object hit is: ", EDray[1].object.class, " at position: ", EDray[1].position.x, EDray[1].position.y, " where the surface normal is: ", EDray[1].normal.x, EDray[1].normal.y ) else -- There's no hit. end if ( EDray[1].object.class == "PlayerTank" or EDray[1].object.class == "Uturret" or EDray[1].object.class == "bullet") then print("PlayerTank in range! Fire!") timer.performWithDelay( 40, ShootEnemyBullet )
Every object that is hit, I get its information printed in the terminal.
So here’s an example of a condition where I need the ray to ignore certain objects -
I have metal-barriers/obstacles that the tank can’t drive over, but it can still shoot over.
Now when I place an object inside LevelHelper, I can assign it a custom tag and using the following code I can access every object that shares the defined tag:
-- Every object that has the tag 'WALLS' is rotated 90º -- local walls = loader:spritesWithTag(LevelHelper\_TAG.WALLS) for i = 1, #walls do local curSpr = walls[i]; curSpr.rotation = 90 print( curSpr.rotation ) end
It grabs every object that has the tag ‘WALLS’ assigned to it and rotates each one 90º.
(If I were to show you in the simulator, every wall would appear to have been rotated 90º)
Now my question would be, how could I tell the ray to ignore ‘curSpr’?
Yes, your suggestion was helpful. I’m always open to opinions.
Edit: Sorry if the code came out weird, it wasn’t rendering quite right in the preview window. :mellow:
Edit: As far as tables/creating objects etc…
Since my objects are already created and positioned via LevelHelper, wouldn’t it be possible to simply use code similar to what I just showed, for adding them to a table?
Another example of how I use ‘tags’, is for my IGmines (in -ground) which can only be destroyed if a tank runs over one:
lol sorry for the long post.