Getting visible screen area bounds with Rendercam

In my 2D games I use Orthographic camera and camera.screen_to_world_bounds(camera_id) to get the screen edges.

In a 3D game I’m playing with, how would I do the same with Rendercam? I’m using an orthographic projection, but the camera is rotated 55 degrees on the x axis. I’ve been playing with rendercam.screen_to_world_plane() but can’t get it working. I am missing something basic no doubt.

Edit: This seems to work, but not quite sure why, because the plane_normal is an arbitrary number:

local camera_pos = go.get_position("camera")
local sw,sh = window.get_size()
local plane_normal = vmath.vector3(1,0,1)
local top_left = rendercam.screen_to_world_plane(0, 0, plane_normal, vmath.vector3())
local bottom_right = rendercam.screen_to_world_plane(sw, sh, plane_normal, vmath.vector3())

Hmm. screen_to_world_plane should work, but it is one of those things that rarely gets used. Is your project small enough to share? You’re trying to get a point on the Z=0 plane, right?

Well, ultimately I’m trying to see if the player game object is outside the visible screen area. This game object can be anywhere really, x,y and z.

Ahh. In that case I would just do the reverse—do world_to_screen with your player pos and check if it’s inside the window.

2 Likes

Sweet, that just worked! I have so much to learn. :partying_face:

The other thing I was trying to do (forgot in all the excitement) was to start a game object just outside the screen view, to animate it into view. I get lost in the 3D stuff here. Would you be able to explain how the screen_to_world_plane() works?

This is the rotation of the camera:
14

I manually track the player only on the z axis.

OK, first, there’s 'screen_to_world_ray', which gives you two points describing a line between the camera’s near plane and its far plane. Both points are directly “underneath” a point in screen-space.

'screen_to_world_plane' just calls 'screen_to_world_ray' and then (supposedly) intersects that ray with an infinite, flat plane (which you specify with the normal vector and any point on the plane), resulting in a single point.

Stealing the diagram from the camera manual and drawing over it, it looks like this:

2 Likes

The general point of 'screen_to_world_plane' is for things like strategy games or top-down shooters with angled cameras, where your actual gameplay is in 2D, but your camera is not.

You could choose whatever plane you want. Personally I would use the X/Y plane like a normal 2D project, and have the camera raised up in the Z axis and angled down. (so planeNormal = (0, 0, 1), pointOnPlane = (0, 0, 0))

With an orthographic camera and only X-axis rotation, the area of the X/Y plane that you see will still be a rectangle in world-space, so you should be able to do 'screen_to_world_plane' with the screen corners to get the bounds, like you had in your first post. But if you’re also moving stuff on the Z axis then that wouldn’t be enough, you would need to get the whole box that the camera shows and check it in all 3 dimensions (so if you’re only doing it for one or two objects it’s easier to reverse it and do world-to-screen).

3 Likes