Have rayCast return a list of all objects hit?

I’m currently trying to polish up my top-down view game, and have added a system of replacing world objects (trees, boulders, walls, etc) with a ‘cut-away’ mesh when those objects are obscuring the player’s view from the camera.

To do so, I use:


blocked = own.rayCast(own.parent, own, 0.0, 'WORLD', 0, 1, 0)[0]

Which is run from the camera, who is a child of the player. This casts a ray from the camera to the player, and returns the first object with a property ‘WORLD’. No problem there.

But what if there are multiple WORLD objects in the firing line of the ray? Can I have the rayCast return a list of all WORLD objects between the camera and the player?

I also proposed this same thing, and bullet supports it apparently, but we need someone to code it,

I don’t know efficient it would be but for every first object hit by the rayCast, you could make them rayCast in the same direction (From the point they were hit at) to get the rest of the “WORLD” objects.

I frequently recast through my actor for mouse over that works though the actor.

This may be viable. I don’t imagine there ever being much more than 2-3 objects being affected at any given time.

After a little tinkering, I got it working like a charm!
Here is the script:


from bge import logic
        
cont = logic.getCurrentController()
own = cont.owner

player = own.parent
blocked = []

object = own.rayCast(player, own, 0.0, 'WORLD',0,1,0)

while object[0] != None:
    blocked.append(object[0])
    object = object[0].rayCast(player,object[1], 0.0, 'WORLD',0,1,0)

if blocked:
    for obj in blocked:
        obj.children[0].replaceMesh(obj.children[0]['Ref']+'_ca',1,0)
        obj.children[0]['CUT'] = True

for obj in own['world'].props:
    ob = obj.children[0]
    if 'WORLD' in obj and 'CUT' in ob and obj not in blocked and ob['CUT'] == True:
        ob['CUT'] = False
        ob.replaceMesh(ob['Ref'],1,0)
    else:
        pass

One more roadblock out of the way! Once I get some kind of paper-doll system working, I will post a demo of this in the WIP section.
I’ve been busy the last couple days trying to make trees and rocks and grass and dirt and all that fun stuff. Finally I will have something with some real graphics in it :wink: