Best way to make 2D Platformer collision?

I have been working on a 2D platformer for the fun of it for a while. I made a few games, but none quite like this.
I am however lost when it comes to collision. I have made some collision, but its absolutely miserable, so I want to do it a different way. I’ll describe what I’m doing, but since code is what really shows what happens, the github is https://github.com/andreja6/Platformer. I will have any classes clickable as well.

So the Level is based on a 2D array of bricks, which have a collision boolean either enabled or disabled. They also have a collectable boolean which I may just do another way. The 2D array is mapped exactly like the map, but x16 in scale when drawn on map, so each block is 16x16 pixels wide, and that is scaled further to fit the GamePanel. For instance, level.block[0][0] starts at position 0,0 and is drawn to 16,16. level.block[1][0] starts at position 16,0 and is drawn to 32,16.

The collision is currently done by checking if any blocks come in between the path of the user, which is calculated in GameTick and GamePanel,
This seemed like an OK way of doing this at first, but with implementation it is incredibly glitchy and I ended up writing more code than I’d expect something like this to require. By the end the amount of times functions are called and the amount of variables make it seem inefficient, but more importantly make it nearly impossible to figure out why something wont work.

One idea I have is generating a collision map using polygons on start, and having blocks return their own collision map, so tilted blocks and round blocks would actually be possible, and then checking whether the players movement would intersect. This is only an idea and i’m not sure how to implement it yet.

Does anyone have any better suggestions or how to improve? This is my first time doing something like this so just a warning, you may facepalm so hard that your hand phases through you, just like my awful collision

Any suggestions would be greatly appreciated

Hi,

You can use some profile tools for java to find where is a bottleneck and try to optimize that piece of your code.
I know that JProfiler is decent.

I have some suggestions to your source code.
You may do some BrickFactory class for creating bricks and load data from a file. It will be more flexible because you just do changes and add more bricks in that file without compilation.

You dont need check collisions with every brick, only with neighbors. For example make a list of neighboring bricks (based on some rough estimations) and check collisions only with them.

I think to many things are going on in your paintComponent() function in GamePanel.
You have twice a for loop inside a for loop and you are reading resources in it.
Better is to prepare everything before and send ready to draw elements into your paintComponent() function.

The BrickFactory sounds interesting, ill be sure to keep that in mind, I haven’t dealt with this kind of stuff before so ill need to do some research first.

For collision, I only check which bricks would be in path compared to the velocity. It’s just that the check doesnt work how i’d think it would.

As for paintComponent(), the resources are loaded dynamically, each image loading only when required and if required. This is only done if the resource is not loaded, so if I’m already doing the check, it wouldn’t hurt to add a bit extra if the check fails. The reason for the two for loops is because the background and foreground need to be drawn at different times so the player is behind foreground, but in front of the other two background layers and the main layer. They are nested because the level is in a 2D array, so x,y.

Hi,

this has nothing to do with collision, but I noticed that you have a lot of images as individual files.
You could merge all those images to a single image and divide it in your code. This prevents you from loading too much into memory. https://stackoverflow.com/questions/621835/how-to-extract-part-of-this-image-in-java this link might be helpful for this.

;D

Whenever I write a platformer (or any 2D game that needs collision) I just use JBox2D. It handles all the collision for you, and you also get physics into the mix which always improves a game.

Here’s a vid of a platformer I wrote using it:

7ehOl4TCUFI

(Plz ignore the crappy graphics).

Thanks for the suggestion! However, from what i see, this still loads all images into an array so I’m not really sure how this would save memory? The way I’m doing it dynamically loads each image when required, so I thought this would be a better option at the time. Is there a reason this happens? Do all image files in a .jar load into memory?

Good suggestion, I’m however trying to make it all from scratch with no libraries (aside from MOD files of course)
Mainly for learning

There are a lot of ways how you can do it. Since is for learning, you can check links below, but to use in a real world, I think Box2D is the answer.

http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/

That`s a nice intro.

Regards

Good suggestion.