Hi question about object coordinates XY

Hi a question similar to this was asked earlier but mines slighly different

Is there a was to get the x,y screen coordinates of an object, instead of a 3d coordinate. Similar to Mouseposition but rather with an object instad of the mouse.
I would like to get the x-y cordinates of an object and send it to an overlay scene.

You can use getScreenPosition: http://www.blender.org/documentation/blender_python_api_2_63_11/bge.types.html#bge.types.KX_Camera.getScreenPosition

Hmm ill try that…i dont see any reason why it shouldnt work.

Hmm i guess im not understanding without a set screen position to use on the overlay scene. Using getscreenposition gives me a xy correctly. But it only returns small decimal values even as i move the object all over the screen. Is this corect?. on the otherside the only way I know how to set the position is worldposition or local position. How to i set the screen postion to an object?

Edit: Sorry for my problem. I was just expecting a setScreenPosition to use on the overlay :smiley:

What are you actually trying to do?

I have a main scene. Game, Characters, monsters on a 3d world. HUD on an overlay scene. I want to display the monsters name over their head. I do not want to use text that points to camera in the 3d world scene. This looks funny as you move the camera and gets bigger as you move closer.

I want the monster name text on HUD scene. 2d, So that it never scales and always looks the same regardless of distance etc… However the XY text position on the hud needs to match the monsters XY position in the 3d world scene.

Hope that makes sense.

getScreenPosition returns a normalised value, which means its in the range of 0.0-1.0. [0,0] being the top left corner of the screen and [1, 1] being the bottom right corner (can someone verify that?).

You have to multiply the x component by the screen width and the y component by the screen height to get the pixel values.

getScreenPosition returns a normalised value, which means its in the range of 0.0-1.0. [0,0] being the top left corner of the screen and [1, 1] being the bottom right corner (can someone verify that?).
Yep. That’s how it works.

@superflip

Ok, now I understand what you want.

You don’t really need to do a projection transform to screen coordinates. Just transform the vector from camera to object back into identity (multiply by transpose of current cam orientation), and then you can shoot a ray along that vector on the overlay camera, to hit a “catcher” plane which can give you the appropriate coordinates of your text tag.

Example is attached.

Attachments

text_track.blend (553 KB)

Yep. That’s how it works.

@superflip

Ok, now I understand what you want.

You don’t really need to do a projection transform to screen coordinates. Just transform the vector from camera to object back into identity (multiply by transpose of current cam orientation), and then you can shoot a ray along that vector on the overlay camera, to hit a “catcher” plane which can give you the appropriate coordinates of your text tag.

Example is attached.

The file you have there has exactly the result im looking for, but I dont understand it at all, its over my head . Any thing a little simpler?

You could also place the orthographic camera in the overlay scene over a specific area, and then use a simple equation to transform the 0-1 values to X and Y positions of the tags in the camera (i.e. if the camera viewed from 0,0 to 1,1 in Blender Units).

Here’s my example. The text is purposely 2 blender units above its ‘owner’ object. It’ll probably get out of sync unless you view it normally (i.e. at the aspect ratio shown by your camera and indicated in your player resolution settings).

OrthoLabel.blend (449 KB)