Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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  
  Collision detection with tiling, fall problems?  (Read 334 times)
0 Members and 1 Guest are viewing this topic.
Offline danieldean

Senior Devvie


Medals: 5
Projects: 1



« Posted 2014-07-09 20:34:38 »

Evening

I'm making a block-based game, yeah I know, using tiling. As it's convenient to do so I hold all the tiles in a two-dimensional array. It seems to work very well together with having my images for the tiles held in an enum, so they're only loaded once. It works quickly for Java 2D. Everything including the sky in my game is made of tiles apart from the player.

I have a problem with making my player, a robot, fall. It's fine for horizontal collisions but downwards is a problem if the robot is slightly spanning two tiles. I fixed this a bit by making the robot 48px tall and 24px wide when tiles are 32px by 32px but it's still bad:



If I nudge forwards a tiny bit more it's fine a the robot will fall. Here's my code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
// Find the tile the robot spans.
int t = player.getY1() / BLOCK_SIZE; // Top tile coordinate.
int b = (player.getY2() / BLOCK_SIZE); // Bottom tile coordinate.
int l = player.getX1() / BLOCK_SIZE; // Left tile coordinate.
int r = player.getX2() / BLOCK_SIZE; // Right tile coordinate.

// Left movement, -1 to get both tiles on the sides.
if(gameInput.getKeyState(KeyEvent.VK_D) && !blocks[b - 1][r].isSolid() && !blocks[t][r].isSolid()) {
    player.move(1);
}

// Right movement, -1 to get both tiles on the sides.
if(gameInput.getKeyState(KeyEvent.VK_A) && !blocks[b - 1][l].isSolid() && !blocks[t][l].isSolid()) {
    player.move(-1);
}

// Jumping, well more like flying at the moment.
if(gameInput.getKeyState(KeyEvent.VK_W)) {
    player.jump(3);
}

// Falling. This is mostly what's the problem. isSolid() simply returns true unless the tile is sky.
if(!blocks[b][l].isSolid() && !blocks[b][r].isSolid()) {
    player.fall(1);
}


I recall other games with similar problems but can't recall if they work around it or leave it. Does anyone have any suggestions?  Huh
Offline cubemaster21
« Reply #1 - Posted 2014-07-10 12:21:19 »

Currently, you are getting the the collisions based on two different tiles, when your character is only less than one tile wide.
1  
2  
3  
4  
5  
// Find the tile the robot spans.
int t = player.getY1() / BLOCK_SIZE; // Top tile coordinate.
int b = (player.getY2() / BLOCK_SIZE); // Bottom tile coordinate.
int l = player.getX1() / BLOCK_SIZE; // Left tile coordinate.
int r = player.getX2() / BLOCK_SIZE; // Right tile coordinate.


if the width of the player is less that the tile width, I would only check the center of the player with collision with the ground. So maybe add another variable to your tiles to make it:

1  
2  
3  
4  
5  
6  
// Find the tile the robot spans.
int t = player.getY1() / BLOCK_SIZE; // Top tile coordinate.
int b = (player.getY2() / BLOCK_SIZE); // Bottom tile coordinate.
int l = player.getX1() / BLOCK_SIZE; // Left tile coordinate.
int r = player.getX2() / BLOCK_SIZE; // Right tile coordinate.
int m = ((player.getX2() + player.getX1()) / 2 ) /  BLOCK_SIZE; // The NEW middle tile coordinate

And then change your fall code so that it uses that variable instead:
1  
2  
3  
if(!blocks[b][m].isSolid()) { 
   player.fall(1);
}


With these changes, you may see slight overlap of the character on the wall that he just falls over, but he should be able to move out of it. Just looking at your collisions code, it looks like walls will be troublesome for you as you can't quite get very close to them.

Edit: changed a minus to a plus.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline pjt33
« Reply #2 - Posted 2014-07-10 12:54:38 »

I recall other games with similar problems but can't recall if they work around it or leave it. Does anyone have any suggestions?  Huh
Easy option: make the base of the robot be its widest part.

Nice option: have a width (e.g. 24px) and an effective footprint (corresponding to the graphic; e.g. 18px). If the effective footprint is above empty space, the robot falls, but you nudge it sideways if necessary to prevent its width from passing through solid space.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline danieldean

Senior Devvie


Medals: 5
Projects: 1



« Reply #3 - Posted 2014-07-10 14:13:25 »

Thanks for you're suggestions, I've just reworked the code to have separate timing for ticks and frames and it's not yet ready to run but once it is I'll give you suggestion a go. I realise I won't get this perfect but just a bit better will be fine.
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (46 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!