Convert vector to screen/camera space?

I’ve been trying to figure this out.
I was hoping there would be a function that does this in mathutils but I haven’t been able to find it. to_2d() and others are obviously not meant for projecting onto screen/camera space.

Anyone got any hints, tips or solutions?

This works for me (applicable only to blender 2.62):

from mathutils import Matrix
from bge import logic

objs = logic.getCurrentScene().objects
Obj = objs["Cube"]
Cam = objs["Camera"]

M1 = Cam.worldTransform  # camera 4x4 transform matrix
M2 = Obj.worldTransform # object matrix
M = M1.inverted()*M2  # conventions valid for blender 2.62

BlendCoords = M.to_translation()

print('Cube Coordinates in camera reference system (blender units):
',BlendCoords)

bge.types.KX_Camera:

transformation matrix: bge.types.KX_Camera.world_to_camera

object position on the screen: getScreenPosition(object)

To get the coordinates in device units (pixels) multiply the value with the screen dimensions from Rasterizer/bge.render.

Could you help me a bit more here. I haven’t worked with these matrixes and vectors a lot so.

Basically what I want to do is project the objects location into camera space and then use that vector again in an overlay scene.
In that scene I wish to project it back setting the position of a halo/billboard object.

I managed to sort of project the position back into the other scene but the position is offset because I’m just dumping the position onto an object in the other scene without taking into account the overlay scene it’s space.

I think this is where I should be using camera_to_world.

I think it should be possible to re-position the object, in the overlay scene, with the same coordinates relative to the camera (if any is present) of the overlay scene. Is this that you need?

I think so.
Currently it looks like this
http://dl.dropbox.com/u/4618760/debugPos.jpg
But I want the oval shape to be where the cube is.

Hello
Sorry to dig up the topic again I’m having the same problem and I can not practice this step puts rem> transformation matrix: bge.types.KX_Camera.world_to_camera

Monster, you can help me? Thank you in advance.

Do you really need to project to screen space?

I mean, if you need a vector in 3D space, then simply transform from 3D space to 3D space.

I created a “lock on” demo (attached) for someone on nilunder.com, which basically does just that.

You can click on either of the cubes to “lock on”, and then the simple object on the overlay scene should follow.

Hope that helps.

Attachments

lockon.blend (554 KB)

Nice!
My script now.

import bge

XYZCena = bge.logic.getSceneList()[0]
XYCena = bge.logic.getSceneList()[1]
 
print (str("cena 2D: ") + str(XYCena))
print (str("cena 3D: ") + str(XYZCena))

Objeto = XYZCena.objects["Cube"]
Camera = XYZCena.objects["Camera"]
Texto = XYCena.objects["Text"]

##

XYPos = Camera.getScreenPosition(Objeto)
Texto.position[0] = XYPos[0]
Texto.position[1] = -XYPos[1]

No results…