Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  tile based collision detection question  (Read 1624 times)
0 Members and 1 Guest are viewing this topic.
Offline g666

Junior Member





« Posted 2005-07-28 19:20:33 »

So im making a tile based game but the colision detection with the tiles doenst work right(the collision rect goes thru the corners of the blocked tile). although i know why it doesnt work , i cant think of a way around it. here is the method where the problem is at:
   
   //moves the sprite, acording to the velx, vely, if it is possible to do so
   //this method moves sprites using the hitsquare collision method with the map tiles
   //calls the sprites mapCollision() method if it collides with a blocked tile
   public void moveSquareSprite(Sprite s){
      int sl = s.getHitSquareLength();
      int newx = (int) (s.getPosX()+s.getVelX());
      int newy = (int) (s.getPosY()+s.getVelY());
      //calc row/col of tiles that each side is on...   
      int top    = (newy-(sl/2)) / tileHeight ;
      int bottom    = (newy+(sl/2)) / tileHeight ;
      int left    = (newx-(sl/2)) / tileWidth  ;
      int right    = (newx+(sl/2)) / tileWidth  ;
      
      boolean topleft = tiles
[top].blocked;
      boolean bottomleft = tiles
[bottom].blocked;
      boolean topright = tiles
[top].blocked;
      boolean bottomright = tiles
[bottom].blocked;
      
      boolean collided = false;
      
      //test if moving upward
      if(s.getVelY() < 0){
         if(topright && topleft){
            s.setPosY( (tileHeight*(top+1)) + (sl/2));
            s.setVelY(0);
            collided=true;
         }else{
            s.setPosY( s.getPosY()+s.getVelY() );
         }
      }
      
      //test for moving downward
      else if(s.getVelY() > 0){
         if(bottomright && bottomleft){
            s.setPosY( tileHeight*bottom - (sl/2));
            s.setVelY(0);
            collided=true;
         }else{
            s.setPosY( s.getPosY()+s.getVelY() );
         }   
      }
      
      //test for moving left
      if(s.getVelX() < 0){
         if(topleft && bottomleft){
            s.setPosX( (tileWidth*(left+1)) + (sl/2));
            s.setVelX(0);
            collided=true;
         }else{
            s.setPosX( s.getPosX()+s.getVelX() );
         }
      }
      
      //test for moving right
      else if(s.getVelX() > 0){
         if(bottomright && topright){
            s.setPosX( tileWidth*right - (sl/2));
            s.setVelX(0);
            collided=true;
         }else{
            s.setPosX( s.getPosX()+s.getVelX() );
         }   
      }
      
      if(collided)
         s.mapCollision();
   }

I have attached the whole game in a zip file renamed to a *.txt file if you want to look at it.

thanks, please help me Wink

PS.in the renamed zip file is also the server for my game, if you want you could look at that and inform me if it is efficent or not?

desperately seeking sanity
Offline Jeff

JGO Coder




Got any cats?


« Reply #1 - Posted 2005-07-29 21:21:36 »

I need to understand this problem better.

Most tile based games use very simple tile based blocking.  Characters move from tile center to tile center.  If a tile is marked as blocked in the file map, the character is not allowed to move there.

Is there a game design reason you need pixel-accurate positioning?

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline g666

Junior Member





« Reply #2 - Posted 2005-07-30 10:37:24 »

I need to understand this problem better.

Most tile based games use very simple tile based blocking.  Characters move from tile center to tile center.  If a tile is marked as blocked in the file map, the character is not allowed to move there.

Is there a game design reason you need pixel-accurate positioning?

I need accurate collision detection as i wan it to have a good feel. i just dont think it would be right if the ship could only move like 20px at a time, lots of games use this kind of collision detection eg mario, zelda - (well i think they do, you are not limited to the center of each tile)

desperately seeking sanity
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline weston

Junior Member





« Reply #3 - Posted 2005-07-30 11:43:22 »

he only said that they move from tile center to tile center, not that there is no animation between these steps Smiley if you know you are only moving between these locations you can make collision really simple (in the way jeff mentioned). Just check if the next tile is empty before making the transition from one to the next, and don't do any detection at all during the transition (you already know the character is moving to a safe location at that point).

Edit: realized later that you probably understand the above, I think I misread your post.

for(int i = 1; i > 0; i++)
{
System.out.println(i+" cups of java downed");
}
Offline Jeff

JGO Coder




Got any cats?


« Reply #4 - Posted 2005-07-31 04:06:46 »

No,I got the same thing from his psot that you did.

Its jnto that the ship jumps from center to center, but that you check your collision a tile at a time and dont start a move into a  tile if its blocked.

This is how games al lthe way up through things like the SSI Dark Sun engine worked.  Its a lot simpler and a lot lower load.

The only limtiation is that the character most move in the 8 cardoinal directions and that the final stop position
must be centered on a tile,

I believe this is how most of the games you mentioned (eg Zelda) function.



Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline g666

Junior Member





« Reply #5 - Posted 2005-07-31 19:52:27 »

No,I got the same thing from his psot that you did.

Its jnto that the ship jumps from center to center, but that you check your collision a tile at a time and dont start a move into a  tile if its blocked.

This is how games al lthe way up through things like the SSI Dark Sun engine worked.  Its a lot simpler and a lot lower load.

The only limtiation is that the character most move in the 8 cardoinal directions and that the final stop position
must be centered on a tile,

I believe this is how most of the games you mentioned (eg Zelda) function.




i really think you have made a mistake, games like zelda dont limit you to the center of each tile, they allow you everywhere in the map, becuase it makes the player feel more in control. play zelda, you will see what i mean. Smiley


desperately seeking sanity
Offline weston

Junior Member





« Reply #6 - Posted 2005-07-31 21:32:30 »

its true that zelda doesn't do this, but many other similarly styled games do (check out the final fantasy games for snes for example). You can still use some of the basic idea though, before transitioning to a new tile, check if it is solid or not, if it isn't than transition and don't check again until you are going to a new tile.

Now this works for most things, but there will be tiles where it seems like only one corner (or half the corners) should be blocked or maybe only one side should be blocked and you should be able to walk on the rest of the tile. You'll have to come up with a way to handle these cases (or ignore them and only have solid/not-solid tiles).

for(int i = 1; i > 0; i++)
{
System.out.println(i+" cups of java downed");
}
Offline g666

Junior Member





« Reply #7 - Posted 2005-08-01 19:15:55 »

I have now managed to fix it so that it basically works, but i still get the problem of "sticking" to the tiles that i didnt get before. IE you cant slide along the tile, you just stop.

desperately seeking sanity
Offline Jeff

JGO Coder




Got any cats?


« Reply #8 - Posted 2005-08-01 19:23:57 »

Well, pixel positioning really is a minor varioation on what I already stated, if thats what you really want.

You still have a "hot point", which is typically the middle of the base.  For up and down, you check the tile map when the y of that point is abotu to cross over into that tile.

For X, youi do the same but add (or subtract going the other way) an offset first thats hal;f the width

Follow?

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline g666

Junior Member





« Reply #9 - Posted 2005-08-01 20:05:19 »

Well, pixel positioning really is a minor varioation on what I already stated, if thats what you really want.

You still have a "hot point", which is typically the middle of the base.  For up and down, you check the tile map when the y of that point is abotu to cross over into that tile.

For X, youi do the same but add (or subtract going the other way) an offset first thats hal;f the width

Follow?

I never thought of doing it that way. sounds good. Smiley

desperately seeking sanity
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

BurntPizza (14 views)
2014-09-19 03:14:18

Dwinin (31 views)
2014-09-12 09:08:26

Norakomi (58 views)
2014-09-10 13:57:51

TehJavaDev (79 views)
2014-09-10 06:39:09

Tekkerue (40 views)
2014-09-09 02:24:56

mitcheeb (62 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (30 views)
2014-09-07 01:12:14

Longarmx (36 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!