# Phyton code to get text remaining at the same size all the time to be readable

I want to get the text with a fix dimensions although there is near or far from the camera.

At the moment I have a text facing the camera all the time with this code:

"from bge import logic

scene = logic.getCurrentScene()

text = scene.objects["Text"]

camera = scene.cameras[0]

text.worldOrientation = camera.worldOrientation"

What I need is that the text remain at the same size all the time to be readable (not too small neither too big).

thanks

• A perfect solution for my problem! it works – merce buyse Nov 23 '16 at 9:41

I think you need to measure he distance to the camera. Be aware this does work for a single point of the object only due to the perspective distortion. Faces nearer to the camera appear larger than faces of the same size more far away from the camera.

Here is a script that calculates the scale dependent on the distance to the camera. The scale when running the script the first time will be preserved. The Python controller needs to be triggered at each single frame, where the distance to the camera changes. If you are unsure us Always Sensor with True Level Triggering.

The controller applies to the object that should be scaled (your text object).

import bge

owner = bge.logic.getCurrentController().owner
camera = owner.scene.active_camera

distance = owner.getDistanceTo(camera)

try:
originalDistance = owner["originalDistance"]
originalScale = owner["scale"]
owner.localScale = originalScale * distance/originalDistance
except KeyError:
owner["scale"] = owner.localScale.copy()
owner["originalDistance"] = distance


here is a short version by reducing the formulas to the minimum. It might be a bit harder to understand. Basically originalScale/originalDistance is constant and calculated just once.

import bge

owner = bge.logic.getCurrentController().owner
camera = owner.scene.active_camera

distance = owner.getDistanceTo(camera)

try:
owner.localScale = owner["normalizedScale"] * distance
except KeyError:
owner["normalizedScale"] = owner.localScale.copy()/distance


Place the text in an overlay scene. This way the distance to the camera (in that scene) is constant and the object size appears to be always the same.