Deleting Object

Started by Belgrado, August 17, 2014, 17:34:09

Previous topic - Next topic

Belgrado

Hello Guys i'm new to LWJGL, but i know some Java.
So I'm trying to make something like BreakOut Game, but i do not know how to delete object when it collides with ball or whatever.
So I made Physics and everything but i am just interested how to delete some object is there some OpenGL function or any other method? I can post source code as well if needed.

Thanks in advance guys!
:)

Cornix

You can not delete objects in java. Deletion is done by a garbage collector.
If this is not what you mean you should clarify your question.

imber

If you're talking about game mechanics:
Assuming you have collision handled, removing them from the game would be a matter of how you have the "blocks"(or whatever you call them) rendered in your game.

A common way people do this is having game objects stored in a collection such as an ArrayList. Every frame, the collection is iterated through to render each game object it holds. So to delete the object, you would have to remove it from the collection which could be done by calling the collection's remove method (care concurrent modification) or by flagging the object to be removed during the next iteration (you would have to code this system yourself).

If this is sort of what you're on about, then a question like this would probably be best posted in a game-development forum where stuff like this is discussed. However if you have a question about the lwjgl library (or OpenGL), then you'll have to elaborate on your question (as Cronix said).

abcdef

Deleting objects is just a matter of not drawing them? Opengl only does what you tell it, if you draw two objects one frame and you only want to draw one the next, then you only run the code to draw the one object in that frame.

Neoptolemus

Like imber said, I have an arraylist which contains a list of "relevant" objects. If a game object isn't in the list, it doesn't get rendered, updated or have collision checks performed on it. My destroy method permanently  removes the object from that list and also cleans up any references to it held internally. The only thing you have to be careful of is keeping rogue references to it in other classes.

To get around the concurrent modification issues my destroy method initially flags an object as pending deletion, then at the appropriate time during the loop the engine removes it, rather than having a free for all. Same goes for spawning new objects.