Collision between different maps (2d lwjgl)

Ok just to start off, I don’t know if this is the right place to post what I’m asking, but oh well.

So, I have started learning lwjgl (working on 2d game), and I found an issue with collision. I’m using a layout as follows:

GameMain (Driver)

MenuScreen(Play Button)

FirstLevel (the first level of the game)

and then the various Players and Enemies, which all inherit an abstract Entity class.

In the FirstLevel, I create an arraylist to store every object. Then, for collision I just directly access it through each player/enemy (public arraylist).

The issue I’m having is, since I can’t pass the arraylist by reference in java… but only by value (I think), I have to hardcode FirstLevel.arraylistname in my player class and all the enemy classes. This creates an issue for multiple levels, because I would then have to rehardcode all of my other objects to use the SecondLevel.arraylistname.

So my question is, is there a better way to get every object on the screen, or check collision? I just run a check whenever an object tries moving.

So like, if my player tries moving 10 pixels forward, it checks if that spot collides with any other object (using rectangles and looping through the arraylist).

I’m just learning how games really should work in lwjgl, so if I’m doing something the wrong way please let me know. I just don’t know what to do for checking every object, since the examples I’ve found have said to make layers in each level, but that brings me to the same issue.

Also I could provide code, but I wouldn’t know exactly what file to post as I use like 10+ files atm. If you want to see something, please just let me know.

Nope. Java is pass by value but the values it passes are references to objects not the objects themselves. So it is essentially pass by reference. But study this example carefully and understand why what happens happens. It is somewhat important.


public class Foo {
    public int x;

    public Foo(int x) {
        this.x = x;
    }

    public static void bar1(Foo foo, int x) {
        System.out.println(foo.x);
        foo.x = x;
        System.out.println(foo.x);
    }

    public static void bar2(Foo foo, int x) {
        System.out.println(foo.x);
        foo = new Foo(x);
        System.out.println(foo.x);
    }

    public static void main(String[] args) {
        System.out.println("Example 1: Accessing Reference");
        Foo f1 = new Foo(10);
        System.out.println(f1.x);
        bar1(f1, 20);
        System.out.println(f1.x);

        System.out.println("Example 2: Reassigning Reference");
        Foo f2 = new Foo(10);
        System.out.println(f2.x);
        bar2(f2, 20);
        System.out.println(f2.x);
    }
}

The output of this code would be:

[quote]Example 1: Accessing Reference
10
10
20
20
Example 2: Reassigning Reference
10
10
20
10
[/quote]

Sorry if I made it sound like I didn’t understand this. I know the difference between by reference and by value, which is what that code explains. My issue really is how to store/save each objects location over and over so I can check collision without hardcoding the arraylist into each player/enemy.

The reason why I can’t do what I’m doing is because java is by value, not by reference. I already knew this, but thanks for the example. I always get the 2 things confused for some reason.

Sorry, I appreciated by mistake :slight_smile: I meant to reply… It really sounds like you don’t understand by-ref versus by-val.

You can simply have a World object with a list of Entity objects. The you can call world.getEntities() wherever you need to access the entities.

Lol I understand by-ref vs by-val. What your saying still doesn’t fix my issue. Doing what you said will still require me to hardcode the object, which has me run into the same issue. Remember, I’m trying to do this so my player/enemies can be placed into multiple levels, and handle collision by themselves. If I’m thinking about the whole aspect of the game wrong… please let me know. But really, I understand by-ref and by-val just fine.

I’m not sure I fully understand the problem, but I’ll offer a couple comments. First, the issue of game application architecture is more or less orthogonal to using LWJGL. By that I mean that using LWJGL doesn’t mean your game has to be architected in any particular way. LWJGL is a low-level framework that provides access to basic services such as video and input; as such, using LWJGL doesn’t really put many constraints on the architecture of your own code.

Also, I would say that if you have classes with names like FirstLevel and SecondLevel, that may be a red flag. Typically different levels in a game would be defined by data rather than code (although technically ‘data’ here might also include code in the form of scripts).

Again, I don’t fully understand the problem you’re having with the lists, but maybe rethinking your architecture a bit will help resolve it. Also, keep in mind that you can pass lists as arguments wherever they’re needed, so there shouldn’t be a problem (I don’t think) getting the lists where you need them.

Ok, so I see where you’re confused. The reason why I decided to use seperate classes for my levels was to separate everything being drawn on the screen.

The reason why I can’t just pass the list as an argument is because I need to check collision for lets say when something gets shot. It works fine for checking all the data… but if I want to change the data, it doesn’t work. Again, I’m just starting to learn how games work with lwjgl, so I may be structuring my game out wrong.

If it helps at all, my game is a side scroller (move right and left), and i hope to have multiple levels/a story mode/survival mode of sorts/a shop for weapons. I understand how to clear the screen and draw new stuff (with different classes), but I don’t know if this is the corrrect way to use lwjgl for a game.

If you would please explain the correct way to create a 2d game, that would be nice. Like what I mean is the structure, so like different screens, different levels, and collision between objects based on the level. So I can put objects in a level and they will be able to check collision without needing that screen hard coded. Idk if I can like use pastebin to show you my code for my GameMain, FirstLevel, Player, and an enemy… but Idk. I just feel like I’m doing this wrong, and I need to headway on what to change.

Perhaps I am :-\ By ‘level’, do you mean something like successive levels in a game (level 1, level 2, etc.)? Or do you mean something like layers in a map, like the levels of a dungeon or something?

[quote]The reason why I can’t just pass the list as an argument is because I need to check collision for lets say when something gets shot. It works fine for checking all the data… but if I want to change the data, it doesn’t work. Again, I’m just starting to learn how games work with lwjgl, so I may be structuring my game out wrong.

If it helps at all, my game is a side scroller (move right and left), and i hope to have multiple levels/a story mode/survival mode of sorts/a shop for weapons. I understand how to clear the screen and draw new stuff (with different classes), but I don’t know if this is the corrrect way to use lwjgl for a game.

If you would please explain the correct way to create a 2d game, that would be nice. Like what I mean is the structure, so like different screens, different levels, and collision between objects based on the level. So I can put objects in a level and they will be able to check collision without needing that screen hard coded. Idk if I can like use pastebin to show you my code for my GameMain, FirstLevel, Player, and an enemy… but Idk. I just feel like I’m doing this wrong, and I need to headway on what to change.
[/quote]
I’ll just reiterate that there’s really no ‘correct’ way to use LWJGL for a game. In fact, there’s really no ‘correct’ way to make a game, period. There are certainly things that are recommended and things that are discouraged (both in game development and software development in general), but there are no ‘rules’ per se beyond the actual constraints of the development environment.

Everything you’ve mentioned so far seem orthogonal to LWJGL. That is, it really doesn’t matter that you’re using LWJGL - the design issues would be the same or similar with any other similar framework. So, I’d recommend not worrying about the LWJGL aspect too much - just focus on the architecture.

As for posting code, sure, you can always post stuff, and if there’s not too much of it, it might help clarify what you’re asking. In general though I’d say that if you find yourself hard-coding anything like a level, menu, screen, or whatever, that may be (not necessarily is, but may be) an indication that a more data-driven approach might be in order.

Well I only made 1 level so far, but ya, like let’s say you have a story with multiple missions. So you could choose mission 1, or mission 2. Completely separate “maps” per say. The one I made was mainly to test how things worked, and everything does work as I want it too. It’s mainly the issue of getting every object in the map, as I just access the arraylist from the FirstLevel class directly since it’s public. Since we’re using java, in my player, enemy, and any other creature class I have to use FirstLevel.objectList to access all of the objects in the map. I saw an example game used layers for each type of object, but still it creates the same issue. The code part though is pretty simple.

GameMain - http://pastebin.com/XPJq0dka

FirstLevel - http://pastebin.com/uxETtJbu

Player - http://pastebin.com/rfVyjsG1

Basic Enemy - http://pastebin.com/arFpRrHu

StructureBlock (Obstacles and what not) - http://pastebin.com/BbHD5MbA

Generic Block - http://pastebin.com/bSYUikAc

Generic Creature - http://pastebin.com/Mn6NnbQy

Bullet - http://pastebin.com/QWvQW3DS

I know that’s a lot of code, but the FirstLevel creates a bunch of objects, and puts them into an arraylist. Then everything is drawn if health > 0 and stuff. You can see in Player/Enemy where I check collision at, and then in Bullet I check the type of object and remove its health that way.

I understand there’s always multiple ways to make games, but I’m just not doing something right when dealing with collision. Any ideas?

You could have each FirstLevel, SecondLevel, etc inherit a common class (call it Level if you want) and the Level object has an ArrayList to hold entities. Then in your entities, make them accept a Level object in their constructor (or a setter). Store that Level object in the entity.

Now in your FirstLevel, SecondLevel etc, when you’re making all the entities you give the entities the Level object (so when FirstLevel makes the entities it gives itself to the entities to use).

Now this isn’t the be-all-end-all solution. Like Jesse said, you should really just store all your levels in data rather than code, that way it’ll be a lot less work in the future to implement new levels without implementing new classes. Yes, your mission scheme have totally separate maps but the internal structure is the same: list of entities, rendering (although you could put that in its own little class), basic logic like score, etc.

In short, put levels in a common class with a list and give that level object to entities, and in the future (or do it now ::)) use data structures for levels.

I hope this helps!

What do you mean use a data structure for levels? I’m a scrub I know, but I’m trying to understand this xD.

Here’s a simple example. First, come up with a simple text format for your levels, something like this:

WWWWWWWW
W..W...W
W.WW.T.W
W......W
W.E..W.W
W...WW.W
W.P.WT.W
WWWWWWWW

You can use whatever ‘coding system’ you want. In the above, W = wall, T = treasure, E = enemy, P = player, and ‘.’ is an empty map cell.

Each of your levels can then just be a text file with something like the above in it. To load a level, you read the text file, iterate over the characters, and for each character, take the appropriate action (place a wall block, create a treasure item, etc.) based on what the text character is. This way, the Java code is the same for every level; only the text files differ. This makes it much easier to develop content, for a variety of reasons, such as: you can see the level visually and don’t have to type coordinates in manually; you don’t have to recompile (which admittedly is usually a quick process in Java) every time you make a change; if you implement dynamic loading, you may not even have to relaunch the app to make a change (which can be very convenient); you have much less code to write and maintain; and so on.

That’s just a simple example, but maybe it’ll help you get started (and depending on the game, you may not even need anything more complex than that).

But that’s just for like the initial drawing of the map. The issue that I’m still stuck at is data storage for like each enemy. Is that something I would do in data also? I’m still really confused on this. Like I said before, the isssue is if i want something to take damage, updating the “objectlist” seems very difficult even with this new way I’m being told. Unless… I can like import the “Level” object and jut assume the player is always on a level? I have no clue if that works, but other than that I don’t see how using data will help this specific issue. I will think about doing the data idea for my levels though, that makes perfect sense.

I’m just gonna ask this cause I play it, but I know minecraft saves worlds as a folder with files in it. Since minecraft uses java/opengl also, they must do something to fix the issue that I’m asking. Are all the mob’s data stored in a file also? That would make sense, but I just don’t know the right way to do it. I know already how to read/write to text files in code, but I feel like I’m not thinking of it the right way still.

Sure, switching to a data-driven approach may not automatically solve the architectural problems you’re running into. But, besides being a good idea in general, it might also put you in a better position to solve those kinds of problems.

[quote]The issue that I’m still stuck at is data storage for like each enemy. Is that something I would do in data also?
[/quote]
You might very well define your enemy characteristics in your game data, but the enemies themselves would still be represented and manipulated in your Java code, as you’re doing now (that might not be the case if you used an embedded scripting engine, but that’s a different story). The only difference is that the parameters used to create them are coming from data rather than being hard-coded.

Actually, reading back over your posts, I think the data-driven level approach could actually be a first step in solving your problem. The problem as you described it seems to be that, depending on what level the player is on, you may want to access FirstLevel.objectList, or SecondLevel.objectList, and so on. You can’t just write ‘FirstLevel.objectList’ in your code, because then when the player is on the second level, it’ll be wrong (I think that’s what you mean). But if levels are defined in data instead of code, then there will just be a single Level class, and it’ll always be ‘Level.objectList’, thus solving your problem.

There are other ways to address the problem as well. chrislo27 and others offered some good suggestions earlier, such as using a common Level base class, and/or using dependency injection in some form or another. In any case though, I think switching to data for the levels will make the problem easier to solve.

Minecraft has its own storage format called NBT (Named Binary Tag). It stores everything as a list of bytes that can be encoded and decoded.

Blocks/tiles are easy since they have an ID (numeric or string). Entities are much more complex because each one is unique. They don’t serialize the object itself to a file because in future versions the serialization will become invalid. What they do is write to an NBT tag that contains important data that NEEDS to be re-created when you load the world. Things include health, position, armour, held item, etc. For other entities like a creeper they store if its charged or not; for a horse it’s the horse armour, saddle or not, tamed, etc.

How entities are re-created is through reflection. Reflection is a trickier topic to cover but it basically allows you to see things in a class via code. It also allows you to instantiate a class via the class’s internal object (like Entity.class or .getClass()). Minecraft will store the entity ID in each entity and when it loads it’ll match the ID to a class and re-create it.

If that’s a bit over your head, you could store a tile type that’s special and spawn an entity when the level is loaded. This technique is okay for levels because the entities are always the same; there’s no individual data that’s totally unique per entity.

As Jesse said I also believe migrating your level scheme to data will solve more problems in the long-run.

So let me just recap everything to make sure I understand this.

  1. Instead of manually making all of the objects, read a file and go through it to create the objects at the given location.
  2. Create an Abstract/Base Class called Level, and have players and such access the object list through Level.

The only thing left that I still don’t quite get is what if I want special things done. As in, let’s say events. I don’t know exactly what I mean this is just an idea, but if I want waves of enemies to spawn, and you to be locked in an area until they are all killed, I feel like it would be more difficult to do this without a separate class for that level. The game I think of for this is Destiny. Like, they have it so when you run into an area it changes what you’re doing, or events (public events or other things) randomly happen. In my side scroller, maybe I want items to spawn in depending on other things. So in a sense…

O = block
P = player
E = Enemy
. = air

.P…E.E.E…
OOOOOOOOOOO

all the enemies would spawn in a “wave”, and the player couldn’t move on until they are all killed. I get the thought of adding a wave, but if I wanted to do this in data as you described, I feel as if it would be more complicated. I’m not trying to make you explain every last thing, but the idea of creating a level through data seems limited on what you really can do. Also what if I want things at different sizes, and be able to be within each other (no collision). Ok sorry if that made no sense, but the point is what if I want more than just a location to spawn things in at from the start. Is it still better to store data this way? Sometimes I’m slow at understanding concepts at first, so like is there more to this than just setting the location to where something is created at?

If I’m getting onto something, then yay, but I just really feel like I’m missing something so simple.

Also regarding nbt tags… I thought I understood that as I run a server and have dealt with plugin issues and stuff, but you’re saying every mob/block/item has the ability to have tags, and those are just changed. Ok lol I feel like this is a little over my head like you said, but since in my game I am creating enemies which would save health, if I wanted more than just spawning guys in, wouldn’t I need a class for the level anyway? I’m a scrub I know, sorry I’m only a soon to be senior in highschool, and I just feel like I’m really missing something in the aspect of game programming.

[quote]1. Instead of manually making all of the objects, read a file and go through it to create the objects at the given location.
2. Create an Abstract/Base Class called Level, and have players and such access the object list through Level.
[/quote]
Yes, that sounds exactly correct (edit - although you might find that ‘Level’ can just be a standalone class with no derived classes, rather than a base class).

[quote]The only thing left that I still don’t quite get is what if I want special things done. As in, let’s say events. I don’t know exactly what I mean this is just an idea, but if I want waves of enemies to spawn, and you to be locked in an area until they are all killed, I feel like it would be more difficult to do this without a separate class for that level.
[/quote]
I totally understand why it seems this sort of thing would be easier to hard-code, but for the reasons alluded to elsewhere in the thread, that’s not usually how it’s done. This is one of those cases where you trade a little work up front for much less work later. By that I mean that setting up a data-driven system can be a little more work at first, but for a game of any complexity at all, the amount of work you’ll save in the end will be more than worth it.

[quote]Like, they have it so when you run into an area it changes what you’re doing, or events (public events or other things) randomly happen.
[/quote]
How you would do something like this using data is a good question, IMO, because admittedly it’s not immediately obvious how to do it. The answer is that there’s a variety of ways to do it, ranging from simple to complex.

The (possibly) most complex solution, but the one that arguably offers the most flexibility and power, is to embed a scripting engine and write the game logic in the scripting language. For example, in Java you could use Nashorn and write the game logic in JavaScript. This is often how things are done with large-scale games, especially RPG/adventure games with lots of content. However, the scripting approach can be non-trivial, so you may want to start with something simpler.

An alternative to scripting is just to create an ad-hoc solution that meets your particular needs. For example, your map text file could include a list of ‘trigger’ tiles, each having a code specifying what should happen if the player touches that tile. You Java code would then read these entries along with the rest of the map, set up the triggers appropriately, and then take the appropriate action whenever the player touches a trigger tile. In a sense, this would actually be a scripting solution, just that the scripting language is a very simple ad-hoc language of your own creation rather than an existing language like JavaScript.

[quote]I feel as if it would be more complicated. I’m not trying to make you explain every last thing, but the idea of creating a level through data seems limited on what you really can do.
[/quote]
I certainly understand why it seems that way. The first thing I’d say is that integrating a complete scripting solution (e.g. Nashorn), while perhaps non-trivial, essentially removes all such limitations, and although that may not be where you want to start, it’s likely where you’ll end up eventually if you continue to pursue this. Even without that though, you can definitely implement your own ad-hoc solution that, while perhaps imposing some limitations, will allow you to do what you want to do.

I’ll end by encouraging you to avoid the temptation to hard-code your game content, tempting though it may be :slight_smile: Although it can require a little more work up front, I think data-driven is really the way to go for the kind of thing you’re doing.

Ok, thanks for all the advice. The one thing I just don’t know for sure is with the Level.If I’m going to be using a generic Level class/object, would I just put like a parameter for the name of the level file (data), and like import the Level file into Player/Enemy. I haven’t tried this before, so I don’t know if that’s the right thing to do. so instead of FirstLevel.objectlist … I do Level.objectlist and import the Level class?

You could pass the level file as a parameter in Level.

When you say

[quote]I do Level.objectlist and import the Level class?
[/quote]
please please please say you’re not making objectlist static in Level… because that kind of defeats the purpose of what Jesse and I have said about the code structure. Load the level, store the level object somewhere like in, then when the level loads and creates its entities, pass the level object to the entity to use.

As I’ve said like 20 times, I can’t pass the list cause that is what the whole issue is that I have here.