Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (764) Games in Android Showcase (229) games submitted by our members Games in WIP (852) 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
 2D Terrain Collision Detection  (Read 1229 times) 0 Members and 1 Guest are viewing this topic.
QuicK

Junior Devvie

 « Posted 2014-04-24 02:23:48 »

I am messing around with a project, built in the style of a 2D side-scroller game.

The ground will eventually look something similar to this:

What is my best course of action for collision detection assuming the only data I'll have are the 2D Vector coordinates of each vertex on the terrain?
Opiop
 « Reply #1 - Posted 2014-04-24 03:27:42 »

That should actually be fairly simple.

What I would do is create a low poly mesh of the terrain. Go through every couple of vertices in the terrain and find the average "height" for the data points. Store these in a list somehow. Then, when the player moves, simply find the vertex in the low poly mesh that the player would be "touching" and then perform a point to rectangle collision detection.
QuicK

Junior Devvie

 « Reply #2 - Posted 2014-04-24 03:56:27 »

wouldn't this provide a somewhat loose collision detection? with the result being that the bounding box of the character sometimes going inside the mesh?

or would they be so close together that it wouldn't matter?
 Games published by our own members! Check 'em out!
Grunnt

JGO Kernel

Medals: 143
Projects: 8
Exp: 5 years

Complex != complicated

 « Reply #3 - Posted 2014-04-24 08:35:58 »

You can use Box2D for this quite well. I've recently experimented with generated 2D polygon terrain and Box2D physics, and this turned out to be very doable. The ground shape can be a ChainShape, which is basically a chained line. I also experimented a bit with updating the ChainShape so that it only covered a bit of ground below the player (to be updated whenever the player reached the edge), but that turned out to be unnecessary performance-wise.

Here's the repository of a prototype lander-style game (very rough pre-alpha) if you are interested: https://bitbucket.org/BasvS/lander
Here I generate the landscape: https://bitbucket.org/BasvS/lander/src/316dd5c7ef29108b7d056a846bd58bbe1f16a678/lander/src/nl/grunnt/lander/mission/MissionGenerator.java?at=master
Here I create a Box2D shape of the landscape https://bitbucket.org/BasvS/lander/src/316dd5c7ef29108b7d056a846bd58bbe1f16a678/lander/src/nl/grunnt/lander/EntityFactory.java?at=master

p.s. LibGDX comes with Box2D built in

Pages: [1]
 ignore  |  Print

 EgonOlsen (591 views) 2018-06-10 19:43:48 EgonOlsen (694 views) 2018-06-10 19:43:44 EgonOlsen (478 views) 2018-06-10 19:43:20 DesertCoockie (831 views) 2018-05-13 18:23:11 nelsongames (1026 views) 2018-04-24 18:15:36 nelsongames (1083 views) 2018-04-24 18:14:32 ivj94 (1664 views) 2018-03-24 14:47:39 ivj94 (590 views) 2018-03-24 14:46:31 ivj94 (1501 views) 2018-03-24 14:43:53 Solater (568 views) 2018-03-17 05:04:08
 Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46Deployment and Packagingby philfrei2018-08-19 23:53:08Deployment and Packagingby philfrei2018-08-19 23:50:04Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39
 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