Solved Remove bullets when off the screen

Hey guys, I’ll make this quick as I’m in College but my brain got side tracked into programming :stuck_out_tongue:

I’m wondering what the best way is to remove bullets from my new game when they go off the screen? I have them setup for collisions with entity’s but now I need to solve this.

My first though was something with the camera but now I am thinking about making a line of rectangles (invisible of course) that move at the same speed as the player at each update and will be just above the edge of the screen. If the bullets collide with the rectangles then they are removed. Thoughts?

EDIT: Totally forgot to add this… The game is a vertical scrolling game, so the player is moving up the screen. Just to clear up any confusion :slight_smile:

You can make a rectangle the size of the screen (or slightly bigger). Then, you would move the rectangle at the same speed of the camera/player. If the bullets do not intersect this rectangle, then you can remove them.

So, it’s almost like your method, but with a single rectangle that would also handle bullets flying off in any direction.

If the bullet’s y is greater than -100 (or the screen height depending on if you are going up or down) destroy bullet.

Perfect idea! Simple is best :slight_smile: I’ll implement this when I get home :smiley: thank you

Or you could just test if the bullet left the screen bounds using a simple if statement



if(bullet.x > camera.getViewportWidth)
   bullet.destroy();


I actually tried that yesterday Gibbo but for some reason some pretty weird results happened. I likely put it somewhere where I shouldn’t, I was tired :stuck_out_tongue:

If you are using a coordinate system, say meters then you would have to convert these to screen coordinates.

A simple and cheap way would be to have a field that represents a scale, say 1 px = 32 meters. Then if your taking the bullets position, you would multiply it by that field to get its real coordinates.

Or if using LibGDX you could use camera to unproject to screen coords.

Think I found an easier way.

if(bullet.getY() > player.getY() + 250){
it.remove  // 'it' is my variable for the Iterator for removing bullets.

Simply gets the player cords as he moves up the screen, puts an extra 250 onto the position and if the bullet goes above that, it deletes the bullet :slight_smile:

That is only a half correct solution to the problem. What if the bullet goes out on the left side and not on the right? What if it leaves the screen in the Y dimension but not the X dimension? You’re going to have to make it a bit more complicated than that, although if you think about it, it is really easy to implement.
Something like this would get the job done:


if(bullet.x < 0 || bullet.x > camera.getViewportWidth()
	|| bullet.y < 0 || bullet.y > camera.getViewportHeight())
	bullet.destroy();

Although this code is pretty trivial, there are some things to watch out for when you’re going to try to implement that:
What does your bullet’s X and Y parameters represent? The lower left corner of the bullet? The middle of the bullet? Or something else?
The other thing that you should keep in mind is that this solution is only going to work if you are NOT going to move the camera itself away from (0;0). If you want to move your camera away from (0;0) you’ll have to move the boundaries too in the if statement.

However, even if you implement this feature correctly (so it will always destroy the bullets that are outside of the camera’s viewport) it is not guaranteed to give you the desired effect. What if an enemy is outside of the camera’s viewport but there are no obstacles to block the bullet? The enemy should receive damage from the bullet, but he won’t because you remove the bullet before it could even hit him.
Also, nothing guarantees that your camera will be always around your player. What if you have to move away the camera from the player to somewhere else (for example to an exploding helicopter in a scripted event)? Bullets that are already around won’t hit your player because (similarly to the previous problem where it was the enemy who couldn’t get damaged) the bullets won’t reach him before they get destroyed.

You could go around these things by using a bit bigger boundaries than your camera’s viewport but it could still result in weird effects when opponents are far away from the camera.
What I could suggest is to make boundaries for your entire level and check if the bullet left the level itself (or collided with something, etc.) and then remove it. :wink:

The bullet can’t go below or to the side… it’s a vertical scroll game, see attached video. Thank you for your idea though. This is my first real game using LibGDX and those bullet mechanics have got me thinking about future games :smiley:

kvzh16gTr3w

Oh, in that case

if(bullet.getY() > player.getY() + 250){
it.remove  // 'it' is my variable for the Iterator for removing bullets.

will be good enough.
For more complicated games tough you should follow the guidelines that I wrote earlier. :slight_smile:

Okay :smiley: thank you. Is there a way to like… save a thread? So I can refer to it later or should I just take a screenshot? :slight_smile:

http://www.java-gaming.org/topics/remove-bullets-when-off-the-screen/33465/view.html

Use your browser’s bookmarks? ;D
Other than that AFAIK you can’t save a thread but can always revisit your threads and posts by clicking “Profile” just below the header and then click on the link next to Posts/Topics.

I didn’t know if the forums had a built in function for saving threads :stuck_out_tongue: thanks for every ones help!

I was not giving him a direct solution, just a code snippet. Having an extra argument in an if statement is hardly more complicated.

Sometimes it’s better to give people a vague answer, promote self learning.

General solution is to test bounding volume against camera frustum. In this case bullets could be spheres.

Special solution for 2d is intersection test with rectange and bullets.

For non rotated 2d cameras you can use axis aligned rectangle instersection test with bullets.