how do I raycast to owner?

anyone know a way to for the owner/self object to raycast to itself and see if it is hit? I was trying to do this and read in the documentation it ignores the caster…I can cast from another object but I am trying to keep the code for this object self contained so I do not need to jump around and get confused.

side note…or workaround…is it logical to assume if the ray returns nothing hit, that I can assume it hit the caster? I’m just trying to see that a path is clear to an object.

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

object = own

pointInSpace = object.worldPosition + (object.worldOrientation*Vector([ X, Y, Z]) )
pos = object.worldPositon
ray = object.rayCast(pos,pointInSpace,0,'',0,0,0)

if ray[0]:
    print('you hit '+ray[0].name )


I don’t have an issue with raycasting from arbitrary vectors or orientations…but I cannot return the owner from the owners cast…it even states in the documentation that the owner who envokes the cast will be ignored. Am I just an idiot? :wink: I will check again tomorrow…but I think the fact that I hit nothing when casting to the caster is still information that can be used for me.

:expressionless:

You already have a reference to the raycasting object when creating the raycasting?

use a different object to call the raycast function, like a camera?

+1 use other object to raycast

pseudo


other_obj.rayCast(target,own.worldPosition,2)

yes, that is what I ended up doing…I just do not like have to jump around between objects for exchanging code values and information…I do think it is needed but in this case I just wanted to see if an enemy sees the player…without having all the enemies raycast to the player…inneficient…so I used a for loop on the camera since all the enemies share a property(bool sees_player) I can just do one more additional check to see if they are in range…and all is good…thanks for your time.

raycasting isnt that bad. just dont run the raycast code if the enemy is too far from the player, maybe attach it to the lod state to make it even faster.

I do do that…I check the distToObject with a specific property inside a loop in the camera…uh, did that make sense?

 for i in objects
 if 'this property' in i
 check = distToObject i
 raycast

this is clearly not the way the code is written, but that is the basic idea.
I’ve appended this to my nodes(pathfinding) nodes script and I have another question…
I am trying to find out the node that is closest to the player…so I do a stupid if statement and pass a variable that holds the distance to check against all the other ‘i’ (nodes) objects which is closer

 if i['this_distance'] < the distance variable:
     distance variable = i['this_distance']

is there a smarter way to do that? like…

distance variable = min(i[‘this_distance’])??? because I am getting errors when I pass it like that…any advice?
EDIT: still looking for a solution, but I was looking again at the api and did not find ‘min’ as far as I was expecting to use it…

I really just need to loop through the objects and see which is closer to the player. I can do this, but it is messy as I stated above.

Here ya go.

def sort_on_distance(cont):    
    own = cont.owner
    scene = own.scene
    
    objects = [obj for obj in scene.objects if 'PROPERTY_NAME' in obj]
    
    if objects:
                        #       the list  - sorting by distance
        sorted_objects  = sorted(objects, key=lambda obj: own.getDistanceTo(obj))[0]    
        distance        = own.getDistanceTo(sorted_objects) 
        max_distance    = own['distance'] 
        
        if distance <= max_distance:
        
        else:

#edit you can also look in my sig at track to closest.blend it uses the script.
also if you want the sorted_list remove the [0] behind it, with the [0] it will grab only the closest object.

I got it all working…all of it(obstacle avoidance, target transfering and jumping) with the player only having about 2-20 nodes(they also dynamically die off)…finaly ;). Your method is a bit shorter and probably a bit faster…can I do that without importing other modules…I usually just have bge and mathutils imported…

on a side note I want to thank everyone here for answering all my, sometimes extremely simple, questions…it can be hard to immediately start a project on a new platform and language…plans change as you realize the bottlenecks and the time requirements…your code, the way you write it etc etc…

anyway thanks everyone …monster, daedalus, thatimst3r(using the hell out of ssao btw), blueprintrandom…sorry if I miss anyone…when I have some more polish and less generic assets in game I will post some shots or a video…it may be a while…I tend to really get involved in a specific part for a while…like texturing, or animation…but it is better to stay in one gear for me…for at least a week.

can I do that without importing other modules

For my code no modules are needed, so you can license it as you like, if that is where you are after :wink:

a bit shorter and probably a bit faster

Indeed, but not only that, you don’t go trough all scene objects every time. (if you have 10 items with the property but you have 500 objects, it needs to go trough those 500 objects every single frame). you now have a list to work with, and you can use the list whenever you like. You can adjust it just to run it on start of game engine, and then update the list when you need to, if it isn’t needed then its best to grab it all and put it into a list on startup.

#edit

Here is an adjusted version to create the lists on startup, now you create 2 lists 1 with the objects and 1 sorted.


def sort_on_distance(cont):  
      
    own = cont.owner
    
    if not 'first_run' in own:
        update_objects(cont)
        own['first_run'] = True
        return
    
    if own['objects']:


        own['sorted_objects']   = sorted(own['objects'], key=lambda obj: own.getDistanceTo(obj))  
        distance                = own.getDistanceTo(own['sorted_objects'][0]) 
        max_distance            = own['distance'] 
        
        if distance <= max_distance:
            # in range stuff
        else:
            #out of range stuff
            
            
def update_objects(cont):   
    
    own = cont.owner
    scene = own.scene
     
    own['objects'] = [obj for obj in scene.objects if 'PROPERTY_NAME' in obj]  

“you can license it as you like” ??? am I to understand that if I do not import the bge api I am able to use a different license? I’m pretty sure just by using the player I am restricted, but thought I would ask.

-in any case thank you for the snippet.

For my code no modules are needed, so you can license it as you like, if that is where you are after

python is not using the GPL license ,they use there own a very permissive one.

note this part.

GPL-compatible doesn’t mean that we’re distributing Python under the GPL. All Python licenses, unlike the GPL, let you distribute a modified version without making your changes open source. The GPL-compatible licenses make it possible to combine Python with other software that is released under the GPL; the others don’t.

:smiley:

this is good news indeed, but what about ‘Import BGE’
does this affect the overall licences of the script?

you could put the code that is not dependent on the BGE in there own modules and simply import them and they wont be effected by the BGE license (just in case)

on the other hand you could release extensions or enhancements that do not use bge python api and sell those…neat…

I doubt I will sell this...as it started out as a project for my son and I an he quickly decided bge was a toy...jokes on him ;) he is young and was turned on by the more flashy things in unity/ue4, but he does not realize bge is just as capable....so now I am driven to finish this and let him pick it apart and help him learn from it....he is admittedly impressed....I tell him it is not the brush, but the painter.

i wonder if the thing you are trying to do is the same i was testing in this.

have a (NPC/Monster) follow breadcrumbs.

http://15b.dk/blendfiles/breadcrumbs.blend

this is good news indeed, but what about ‘Import BGE’
does this affect the overall licences of the script?

Yes, there is a line in the license that says something like this:
if you import a module the script falls under gpl. and you need to add the source.

If I add a module to a GPL-covered program, do I have to use the GPL as the license for my module? (#GPLModuleLicense)The GPL says that the whole combined program has to be released under the GPL. So your module has to be available for use under the GPL.
But you can give additional permission for the use of your code. You can, if you wish, release your module under a license which is more lax than the GPL but compatible with the GPL. The license list page gives a partial list of GPL-compatible licenses.

If a library is released under the GPL (not the LGPL), does that mean that any software which uses it has to be under the GPL or a GPL-compatible license? (#IfLibraryIsGPL)Yes, because the program actually links to the library. As such, the terms of the GPL apply to the entire combination. The software modules that link with the library may be under various GPL compatible licenses, but the work as a whole must be licensed under the GPL. See also: What does it mean to say a license is “compatible with the GPL”?

source:
https://www.gnu.org/licenses/gpl-faq.html#IfLibraryIsGPL

#edit

pretty sure just by using the player I am restricted, but thought I would ask.

Yes and no, there are workarounds for this gpl problem. simply create a blend(launcher) that starts another blend. then convert that blend(launcher) into an .exe, now the blend(launcher) is gpl, but it starts your blend and now your blend does not fall under gpl.