using localized position to define vision cones/inside angle calc


import bge
from mathutils import Vector


def main():


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

    #if target not set get target from scene objects
    if type(own['Target']) is str:
        own['Target']=bge.logic.getCurrentScene().objects[own['Target']]


    # if you have a target object localize it
    else:
        
        # Localize the target point
        local = own['Target'].worldPosition-own.worldPosition
        local = own.worldOrientation.inverted()*local
        
        own['Read']=str(local)
        
        
        
        if local.z>0:
            #it's in front
            # because local +Z is front :D
            #Next is a cone :D
            
            
            if (abs(local.y)-local.z)<.1:
                #inside cone

                bge.render.drawLine(own.worldPosition+(own.worldOr  ientation*Vector([0,1,1])),own.worldPosition+(own.worldOrientation*Vector([0,10,10])),(0,1,0))
                bge.render.drawLine(own.worldPosition+(own.worldOr  ientation*Vector([0,-1,1])),own.worldPosition+(own.worldOrientation*Vector([0,-10,10])),(0,1,0))



            else:
                #outside cone
                bge.render.drawLine(own.worldPosition+(own.worldOr  ientation*Vector([0,1,1])),own.worldPosition+(own.worldOrientation*Vector([0,10,10])),(1,0,0))
                bge.render.drawLine(own.worldPosition+(own.worldOr  ientation*Vector([0,-1,1])),own.worldPosition+(own.worldOrientation*Vector([0,-10,10])),(1,0,0))
                    
            
            
main()





Attachments

LocalCone.blend (479 KB)

This is really great, thanks again BPR.

There are heaps of other applications for this as well, such as checking for what the player can and can’t see.

i use this code to find the angle.
less math :stuck_out_tongue:


import bge

cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()

own = cont.owner 
player = scene.objects["player"]

targetVec = player.worldPosition - own.worldPosition

facingVec = own.worldOrientation.col[1]
angle = facingVec.angle(targetVec)


...


Thanks :smiley:

I figured out the other way on my own,

also localizing a point to another object is very very handy for all sorts of things.
(like the component jacks in wrectified)