How to know if an object is visible on screen?

Hello,

I’m doing a platformer game, and there are bonuses in it (balloon).
When the player hit a balloon, this one is released, lift in the air, and soon disappear from the screen.

And that’s it, i’d like to know when the balloon has disappeared, for computing something and applying the result to the game.

The terrain is bigger than the screen resolution, so i’m using a camera (thank you @britzl for this one :wink: ).

I had thought of something like:

if (ballon.y - player.y) > screen_resolution_height then ballon_has_vanished()

but it’s ok only if the player is on the first floor. If he climbs the platforms, then the balloon should take less time to disappear.
For example, if the player is at (100,100), the balloon will take longer to reach the top of the screen than if the player is in the middle of the screen.

May be the engine can tell me if an object is culled ?

Thank you for reading :slight_smile:

The engine doesn’t cull at all. So there isn’t anything like that builtin.

Maybe I misunderstand what you want to do, but don’t you only want to compare ballon.y > screen_resolution_hight? That should tell if it is on screen or not. If the origin of the game object is on the bottom of the sprite. Also maybe you need to do go.get_world_position instead of go.get_position.

1 Like

Thank you for your answer.

Well testing balloon.y is not enough, when the camera is moving. It would work only on a static screen.
But you were right with using go.get_world_position and it led me to the solution :slight_smile:

Reading again the documentation of britzl camera , i’ve found this:

camera.world_to_screen(camera_id, x, y)

Translate world coordinates to screen coordinates, based on the view and projection of the camera. This is useful when manually culling game objects and you need to determine if a world coordinate will be visible or not.

I’ve put in bold world coordinates, because i had done a test with this function, using object coordinates :confused:

So the correct form is:

cam.world_to_screen(cam_id, go.get_world_position("#"))

Thank you :slight_smile:

It doesn’t really matter in this case, but the short-hand “#” means “the current script”. What you really want is “.” which means “the current game object”.

1 Like