On-screen position script question

*Note: If there is another easier way to do this, such as a 2D getScreenPosition() for an object, please let me know.

I am trying to use DanyAlejand’s script to create a 2D lockon over an object moving through 3D space. The original thread is here:

http://blenderartists.org/forum/showthread.php?t=35841

download script with .blend and tutorial

The tutorial explains to calculate

constant=atan(15.9775/lens)

which I did on the python command line

import math
math.atan(15.9775/35)

My 2D plane does follow the object through the scene, but very loosely. In DanyAlejand’s file, the 2D plane has a tight lock on the object as it moves.

My scene has a different camera lens and constant, so I changed those values to match the demo file exactly, but it still does not track well.

If anyone knows about this script or can look at the blend files, please let me know if you can tell how I can make this work with my scene.

In DanyAlejand’s onscreenpos.blend part of the script is not explained in the tutorial, so I’m not sure how those values are meant to be calculated.

Property: Float: myx = 50
Property: Float: myy = 50
Amover.setDLoc(dif0.0122,-dify0.00922,0,1)

Amover=controlador.getActuator("actmover")
dif=yo.posx-yo.myx
dify=yo.posy-yo.myy
Amover.setDLoc(dif*0.0122,-dify*0.00922,0,1)
yo.myx=yo.posx
yo.myy=yo.posy
GameLogic.addActiveActuator(Amover,1)

I’m also unsure exactly how the 2D plane is supposed to be positioned in front of the camera.

onscreenpos.blend is DanyAlejand’s working sample scene.
2d-tracking-06.blend is my attempt to use this script in my own test scene.

Here is the main script:

from math import tan, cos, acos, sin, sqrt
escena=GameLogic.getCurrentScene()

## INSERT CONSTANTS HERE #############
# View readme to fill these ##########
l=200
constant=0.42824616289707534
######################################

controlador=GameLogic.getCurrentController()
yo=controlador.getOwner()
oBound=escena.getObjectList()[yo.object]
oe1=escena.getObjectList()["OBe1"]
oe2=escena.getObjectList()["OBe2"]
oe3=escena.getObjectList()["OBe3"]
e1p= oe1.getPosition()
e2p= oe2.getPosition()
e3p= oe3.getPosition()
Boundp= oBound.getPosition()
rx=e1p[0]-Boundp[0]
ry=e1p[1]-Boundp[1]
rz=e1p[2]-Boundp[2]
ed1=sqrt((rx*rx)+(ry*ry)+(rz*rz))
rx=e2p[0]-Boundp[0]
ry=e2p[1]-Boundp[1]
rz=e2p[2]-Boundp[2]
ed2=sqrt((rx*rx)+(ry*ry)+(rz*rz))
rx=e3p[0]-Boundp[0]
ry=e3p[1]-Boundp[1]
rz=e3p[2]-Boundp[2]
ed3=sqrt((rx*rx)+(ry*ry)+(rz*rz))
C1=acos((l*l+(ed1*ed1)-(ed2*ed2))/(2*l*ed1))
C2=acos((l*l+(ed1*ed1)-(ed3*ed3))/(2*l*ed1))
C1y=ed1*sin(C1)
C2y=ed1*cos(C2)
C3=acos(C2y/C1y)
dist=C1y*sin(C3)
maxX=dist*tan(constant)
maxY=maxX*0.745
yo.posx=(50/maxX)*(ed1*cos(C1)-l/2)+50
yo.posy=(50/maxY)*(C2y-l/2)+50

Attachments

onscreenpos.blend (115 KB)2d-tracking-06.blend (50.1 KB)