[Help] Size depending on distance from camera

Hi there!

I made a script to track a air plane (as target)
but if the camera is near the tracker the tracker looks to big and if the camera is far away from the tracker the tracker looks to small.

I tried it in python with getDistanceTo() but that went wrong:(


So maybe the tracker shouldn’t be an actual object attached to the target?
Maybe you could put it on an overlay scene, like the HUD it is (supposedly) meant to represent?

The question is:

  • How is this done?

To be honest, I haven’t done this one in particular, but I do know there are some handy functions for it.
For example, You can check if an object is visible with:

camera = bge.logic.getCurrentScene().active_camera
visible = camera.pointInsideFrustum(object.worldPosition)

Note that this looks at the center of an object. Investigate the boxInsideFrustrum or sphereInsideFrustrum

And one can get the screen-space location of an object with

x,y = camera.getScreenPosition(object) 

Now you’ll have to do something with the X and Y co-ordinates. While the document doesn’t explicitly say so, I’d guess they run from -1 to 1, or maybe 0 to 1. Either way, you should probably multiply x by bge.render.getWindowWidth() and y by bge.render.getWindowHeight() (this will correct aspect ratio).

Finally you should set an object on an overlay scene to this position, with something like:

HUD_MARKER.worldPosition = [x,y,0]

And have a camera looking down at it.

But yeah, that’s the complex way. The script you’re looking for probably goes like:

scale = camera.getDistanceTo(obj)**0.5
obj.worldScale = [scale, scale, scale]

Not quite sure if that should be **0.5 or **2. Too tired to think it through fully right now.

Good Night.

Yes it works :slight_smile:
i had to do

scale = camera.getDistanceTo(obj)**0.2

Thanks sdfGeof

Hmm, interesting that 0.2 was the magic number here, I expected it to be squared or square-root.

The number will depend on the camera frustrum size/field of view. It would be interesting to find the link between view angle and distance/scale falloff.
Someone with a little more knowledge than me could probably figure it out easily enough.

I may have to play with this a little when I have some more time.

The distance from cam to obj is about 1500
0.2 was the best value because 0.5 was HUGE
after playing around I stopped by 0.2:/

There’s also another appoach. The cursor could be a child of the camera.

Do you have any examples for that Raco because i see a child is fixed to a parent but the cursor mus be dynamic following the nearest target

Here’s an example of positioning a mouse pointer object locally on the camera. (Mouse_Pointer_00.blend). Even when the player moves and turns the pointer stays in place. Notice that it’s also adaptable with the lens of the camera.

This example works with the mouse position, but if you replace this with camera.getScreenPosition(object) it should work.

Edit: I’ve added an example to show how it works with getScreenPosition. (Target_tracker_00.blend). Use arrows to fly.


Target_tracker_00.blend (84 KB)Mouse_Pointer_00.blend (80.3 KB)

Thanks Raco I will look at it

Thanks Raco,

That worked out the best :smiley: