I'm trying to convert a length (well, a radius) from world coordinates to screen coordinates using an OrthographicCamera. However, the only method I've figured out so far is to do a projection, with some basic vector math before/after, but this seems...expensive.

Here's what I have so far (apologies, written in Kotlin):

val cam: OrthographicCamera = ...
val behavior: SteeringBehavior<Vector2> = ...

// calculating the center of the circle
val coords = Vector3()
val wander = behavior as Wander<Vector2> // defined elsewhere above
coords.set(wander.wanderCenter, 0F)
cam.project(coords) // coords now has wanderCenter in screen coordinates

// now, the part I'm interested in, calculating the radius
val radiusCoords = Vector3(wander.wanderCenter, 0F).add(wander.wanderRadius, 0F, 0F)
val radius = radiusCoords.x // this is the value I want

The first half of the code snippet is fine, and provided for context. But the second half...I'm basically taking a scalar and plugging it through a Vector3 and a series of Complicated Matrix Math to convert it to another larger scalar. Is there an easier way to do what I'm doing? I've looked around a bit -- like at OrthographicCamera's combined.getScaleX() field, but it doesn't seem to have the value I need.


1 Answer 1


Turns out this was pretty easy. You know the width of the screen, you know the width (and zoom) of the camera's viewport...just math it up:

fun Float.metersToPixels(cam: OrthographicCamera): Float {
    return this * cam.scale
fun Float.pixelsToMeters(cam: OrthographicCamera): Float {
    return this / cam.scale
val OrthographicCamera.scale: Float
    get() = Gdx.graphics.width / viewportWidth / zoom

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .