Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  removing objects when off-screen  (Read 765 times)
0 Members and 1 Guest are viewing this topic.
Offline catsaremyreligion

Senior Newbie





« Posted 2013-09-08 06:09:46 »

So I'm making a little side-scrolling platformer in Java right now as some practice. I generate my levels from text files with different numbers signifying different types of blocks and objects in the game world, as I often see people do on the internet. My problem is that when I start the game, all individual walls and other objects are all created at the same time in an arraylist. If my levels were to get much larger than they currently are, I can see having thousands of instances of the same object existing off-screen would be very inefficient. It would be preferable if I could only create blocks and enemies at a certain distance away from the player as it approaches them, and when they exceed that distance, they would be removed from the arraylist. I've been able to remove the objects when they go off screen so far, but not re-draw them again. So this leads me to my question: How would I go about creating only the objects that I need on the fly rather than creating them all at once when the game starts up like I'm currently doing. If anyone needs to see any code, that's cool, I can show, or if you wanna just give me a short explanation, that's fine too. Any help would be greatly appreciated!

Also if any of my Java jargon is incorrect above, please correct me! I'm still quite a novice! Grin
Offline nerb
« Reply #1 - Posted 2013-09-08 06:38:03 »

Perhaps you could break your level up into segments, instead of one large file? Then you only have to load the segment that the player is in, and perhaps immediately in front & behind of the player. As the player moves, reload the required segments. Theoretically you could have limitless level size with this method (notwithstanding hardware limitations!).

nerb.
Offline catsaremyreligion

Senior Newbie





« Reply #2 - Posted 2013-09-08 06:42:14 »

Wow that's a pretty great idea! Much simpler than anything I was thinking of. I could see the amount of files getting out of hand if the game's size (in terms of content) were to increase dramatically, but I definitely will consider this! Thanks!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #3 - Posted 2013-09-08 06:47:04 »

Add this code to every object which can go left the screen in their update code.

1  
2  
3  
4  
5  
public void update()
{
    if (x <= -tileWidth)
        destroy();
}

That is when each object moves out of screen from left, we destroy them.

Offline catsaremyreligion

Senior Newbie





« Reply #4 - Posted 2013-09-08 06:57:25 »

Add this code to every object which can go left the screen in their update code.

1  
2  
3  
4  
5  
public void update()
{
    if (x <= -tileWidth)
        destroy();
}

That is when each object moves out of screen from left, we destroy them.
Yeah, I've actually got this figured out. What I need to know is how to recreate those objects if the player were to go backwards without re-drawing every single object that is already on the screen.  Undecided
Offline SHC
« Reply #5 - Posted 2013-09-08 07:08:52 »

What I've understood is you are trying to draw the objects that are only visible. If so, make a rectangle that fit's the screen. When rendering objects, test whether the object intersects with the screen rectangle and render the objects that lie on the visible part of screen. This way, you can only render objects that are on the screen.

Offline Several Kilo-Bytes

Senior Member


Medals: 11



« Reply #6 - Posted 2013-09-08 19:14:57 »

Load the entire level all at once. It will fit in memory unless your game is much, much bigger than I imagine it is. 1000 blocks should be around the order of several kilobytes. A megabyte is 1000 times larger than that and a gigabyte of RAM is 1000 megabytes of RAM. Collision detection and drawing will be the problem if you try to do it all at once. (Especially since collision detection could be O(n^2).) To narrow down the number of items to visible and nearby items, test if each items AABB overlaps a rectangle slightly larger than the visible area of the level. Items won't need to be discarded until the end of the level. You can leave them wherever they are when they go outside the rectangle or reset their position to where they started. You could sort items by their x position or divide the level into grids/chunks, but it might not matter. (Awesome name btw.)
Offline catsaremyreligion

Senior Newbie





« Reply #7 - Posted 2013-09-09 20:48:26 »

Load the entire level all at once. It will fit in memory unless your game is much, much bigger than I imagine it is. 1000 blocks should be around the order of several kilobytes. A megabyte is 1000 times larger than that and a gigabyte of RAM is 1000 megabytes of RAM.

Alright thanks, I really wasn't sure if this was a bad practice or not, but my game probably will not ever get very large.

Collision detection and drawing will be the problem if you try to do it all at once. (Especially since collision detection could be O(n^2).) To narrow down the number of items to visible and nearby items, test if each items AABB overlaps a rectangle slightly larger than the visible area of the level. Items won't need to be discarded until the end of the level. You can leave them wherever they are when they go outside the rectangle or reset their position to where they started. You could sort items by their x position or divide the level into grids/chunks, but it might not matter.


I'm sorry, but I'm not familiar with the term AABB. Could I get a quick explanation or something?

(Awesome name btw.)

Thanks!
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #8 - Posted 2013-09-09 21:20:37 »

I'm sorry, but I'm not familiar with the term AABB. Could I get a quick explanation or something?
Thanks!

AABB = Axis Aligned Bounding Box

Easy system to implement, because each box just has a x/y and width/height  and you just check those things
x and x+width,  y and y+height  to the others and make sure they aren't overlapping/intersecting.


Basically rectangles that can't be rotated.   Grin


"Experience is what you get when you did not get what you wanted"
Offline catsaremyreligion

Senior Newbie





« Reply #9 - Posted 2013-09-09 21:37:49 »

I'm sorry, but I'm not familiar with the term AABB. Could I get a quick explanation or something?
Thanks!

AABB = Axis Aligned Bounding Box

Easy system to implement, because each box just has a x/y and width/height  and you just check those things
x and x+width,  y and y+height  to the others and make sure they aren't overlapping/intersecting.


Basically rectangles that can't be rotated.   Grin



Ah! Seems simple enough! This is what I was planning on doing anyway, just didn't know it was named that. Thanks!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (247 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!