Get only objects that enter into view

I am trying to create a lock on system that locks in to objects closer to the center of the screen like in the game called armored core

cam = scene.active_camera
target = scene.objects['npc']
x , y = cam.getScreenPosition(target)
score = 1 - Vector([x-0.5,y-0.5]).length

works as long as your npc is visible on screen since i didn’t figure out whats values get out of getScreenPosition when the npc is off screen.

#if in upbge 3x or greater replace * with @ 
local = own.scene.active_camera.worldTransform.inverted() * target.worldPosition
if local.z <0:
    score = abs(x) + abs(y)
    score  = 1/ (score*score)
    if score<0:
       score=0
    elif score>1:
       score = 1
else:
    target_behind   

is this to transform the target.worldPosition into camera’s space ? Nice, it’s more elegant than some dirty ray stuff :+1:

What is abs(x) and abs(y)?

looks like he thinks that x and y are scaled on [-0.5,0.5] and 0 is the middle, but no , for
getScreenPosition , they are both scaled on [0,1] . 0.5 is the middle

So to take the idea of BPR with the camera z axis

cam = scene.active_camera
target = scene.objects['npc']
local = cam.worldTransform.inverted() * target.worldPosition
x , y = cam.getScreenPosition(target)
score = (1 - Vector([x-0.5,y-0.5]).length) * (local.z <0) / 0.7071

the / 0.7071 is to make the score = 0 when being in one of the 4 corners.

abs(local.x) and abs(local.y)

local.x 0 is the center left to right of screen.

local.y 0 is center up and down