# [tip] WorldToScreenPoint function

A topic by Liam Brocklehurst created 1 year ago Views: 205
Viewing posts 1 to 1

I recently needed to get the screen position of a player for checks to move the camera, so I added some functions to allow me to do this - figured it might help someone else! If anyone spots any errors, let me know and I can update. I imagine this'll only work for 2D games!

Firstly, I created a class that could hold top, bottom, left, right bounds (reusable for a few things):

```class Bounds
{
constructor(public top: number, public bottom: number, public left: number, public right: number) { }
}
```

We can then use this within our CameraHelper class:

```class CameraHelper {

static WorldToScreenPoint(x: number, y: number, cameraName: string): Sup.Math.Vector2
{
let cameraActor = Sup.getActor(cameraName);
let cameraPos = cameraActor.getPosition();
let cameraBounds = CameraHelper.CameraBounds(cameraName);

return new Sup.Math.Vector2(
(x - cameraBounds.left) / Sup.Game.getScreenRatio().width,
(y - cameraBounds.bottom) / Sup.Game.getScreenRatio().height
);
}
static CameraBounds(cameraName: string): Bounds
{
let cameraActor = Sup.getActor(cameraName);
let cameraPos = cameraActor.getPosition();
return new Bounds
(
cameraPos.y + (Sup.Game.getScreenRatio().height / 2),
cameraPos.y - (Sup.Game.getScreenRatio().height / 2),
cameraPos.x - (Sup.Game.getScreenRatio().width / 2),
cameraPos.x + (Sup.Game.getScreenRatio().width / 2)
);
}
}```

CameraBounds returns a Bounds type of the world position of the top, bottom, left and right of the camera. WorldToScreenPoint then takes these values and determines what screen point (0-1) the x,y parameters are at.

Sup.Game.getScreenRatio() is possibly not giving us accurate calculations, so if I find a better way (or if someone lets me know) I'll get that changed.

Hope this helps someone!