Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (754)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (842)
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  
  Best way to make 2D Platformer collision?  (Read 347 times)
0 Members and 1 Guest are viewing this topic.
Offline andreja6

Junior Newbie





« Posted 2018-05-17 17:05:45 »

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
Offline meva

Junior Devvie


Exp: 3 years



« Reply #1 - Posted 2018-05-17 20:40:53 »

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.
Offline andreja6

Junior Newbie





« Reply #2 - Posted 2018-05-18 00:48:29 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Stampler
« Reply #3 - Posted 2018-05-18 07:33:27 »

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.

 Grin

Don't dream big, dream realistic.
beemelonstudio: Website | Twitter | Instagram
Offline SteveSmith
« Reply #4 - Posted 2018-05-18 12:03:02 »

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:

<a href="http://www.youtube.com/v/7ehOl4TCUFI?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/7ehOl4TCUFI?version=3&amp;hl=en_US&amp;start=</a>

(Plz ignore the crappy graphics).




Offline andreja6

Junior Newbie





« Reply #5 - Posted 2018-05-18 17:41:59 »

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.

 Grin

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?
Offline andreja6

Junior Newbie





« Reply #6 - Posted 2018-05-19 02:28:43 »

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:

~insert video here~

(Plz ignore the crappy graphics).
Good suggestion, I'm however trying to make it all from scratch with no libraries (aside from MOD files of course)
Mainly for learning
Pages: [1]
  ignore  |  Print  
 
 

 
DesertCoockie (33 views)
2018-05-13 18:23:11

nelsongames (75 views)
2018-04-24 18:15:36

nelsongames (70 views)
2018-04-24 18:14:32

ivj94 (752 views)
2018-03-24 14:47:39

ivj94 (82 views)
2018-03-24 14:46:31

ivj94 (622 views)
2018-03-24 14:43:53

Solater (98 views)
2018-03-17 05:04:08

nelsongames (179 views)
2018-03-05 17:56:34

Gornova (405 views)
2018-03-02 22:15:33

buddyBro (1065 views)
2018-02-28 16:59:18
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!