Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  How to check tile in front of player  (Read 355 times)
0 Members and 1 Guest are viewing this topic.
Offline Bassex96

Senior Duke


Medals: 4



« Posted 2013-05-29 00:08:22 »

I'm working on my pacman game now, i've got it setup to detect which tiles are suppose to be blocked which ive added to an array list. I'm using ShapeRenderer to show which tiles are blocked and which aren't...Now i'm having trouble deciding which tile is in front of the player to see if it's blockable...

This is my map class

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  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
package com.psillidev.pacman1;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.maps.tiled.*;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;

public class Map {
TiledMap tiledMap;
public TiledMapTileLayer layer;
int[] tileLayer = {0} ;
int[] collisionLayer;

Cell tileCell;

Array<Rectangle> blockedTiles = new Array<Rectangle>();

public OrthogonalTiledMapRenderer renderer;
float unitScale;

   public Map() {
      float unitScale = 1/16f;
     
      tiledMap = new TmxMapLoader().load("data/pactest.tmx");
     
      layer = (TiledMapTileLayer) tiledMap.getLayers().get(1);
      renderer = new OrthogonalTiledMapRenderer(tiledMap, 1/16f);
     
      setBlockedTiles();
   }
   
   void setBlockedTiles() {
      for (int y = 0;y < layer.getHeight();y++ ) {
         for (int x = 0; x < layer.getWidth();x++) {
            Cell cell = layer.getCell(x, y);
            if (layer.getCell(x,y)!=null) {
            Rectangle tempRect = new Rectangle(x*16,y*16,16,16);
           
           
           
            blockedTiles.add(tempRect);
           
            //System.out.println(blockedTiles.size);
           
            if (layer.getCell(x, y)==null) continue;
           
            }
         }
           
      }
   
   }
   
   public TiledMap getMap() {
      return tiledMap;
   }
   
   public float getScale() {
      return unitScale;
   }
   
   public Cell getCell(int x, int y) {
      return layer.getCell(x, y);
   }
   
   public Array<Rectangle> getBlockedTiles() {
      return blockedTiles;
   }
   
   public TiledMapTileLayer getLayer() {
      return layer;
   }
   
   public void render(OrthographicCamera camera) {
      renderer.setView(camera);
      //renderer.renderTileLayer(layer);
      //renderer.render();
      renderer.render(tileLayer);
     
      System.out.println(blockedTiles.size);
     
      ShapeRenderer rend = new ShapeRenderer();
      rend.begin(ShapeType.Line);
      for (int t = 0;t < blockedTiles.size;t++) {
      rend.rect(blockedTiles.get(t).x, blockedTiles.get(t).y, blockedTiles.get(t).width, blockedTiles.get(t).height);
      System.out.println("Tile: " + t + " X: " + blockedTiles.get(t).x + " Y: " + blockedTiles.get(t).y + " Width: " + blockedTiles.get(t).width + " Height: " + blockedTiles.get(t).height);
      }
      rend.end();
     
   }
   
}


This is my player class where i'm moving the pacman..

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public void move(Map map) {
     
      //for (int b = 0;b < map.getBlockedTiles().size;b++) {
      //   Rectangle tempRect = map.getBlockedTiles().get(b);
      if (direction == 0) {
         
         setY(getY() + (int)(Gdx.graphics.getDeltaTime() + 3));
      }
     
      if (direction ==1) {
         setX(getX() + (int)(Gdx.graphics.getDeltaTime() + 3));
      }
     
      if (direction ==2) {
         setY(getY() - (int)(Gdx.graphics.getDeltaTime() + 3));
      }
     
      if (direction ==3) {
         setX(getX() - (int)(Gdx.graphics.getDeltaTime() + 3));
      }
      //}
      }


I've commented a few lines out while playing with the code..I'm stumped.
Offline GabrielBailey74
« Reply #1 - Posted 2013-05-29 00:26:27 »

Don't know how much use this will be to you, but here's how I'm checking if a tile is north of me, south etc
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  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
// returns false if a tile is present in the direction given.
// translated_x is your player.x
// translated_y is your player.y
   private boolean isNoTile(int direction, int translated_x, int translated_y) {
      for (Tile t : getCollision_tiles()[0]) {
         switch (direction) {
         case 0: // up
            Rectangle player1 = new Rectangle(translated_x, translated_y - tileHeight, 32, 32); // perform a move-check
            if (player1.intersects(t.getRect())) {
               return false;
            }
            break;
         case 1: // right
            Rectangle player2 = new Rectangle(translated_x + tileWidth, translated_y, 32, 32); // perform a move-check
            if (player2.intersects(t.getRect())) {
               return false;
            }
            break;
         case 2: // down
            Rectangle player3 = new Rectangle(translated_x, translated_y + tileHeight, 32, 32); // perform a move-check
            if (player3.intersects(t.getRect())) {
               return false;
            }
            break;
         case 3: // left
            Rectangle player4 = new Rectangle(translated_x - tileWidth, translated_y, 32, 32); // perform a move-check
            if (player4.intersects(t.getRect())) {
               return false;
            }
            break;
         default: // 0_0
            break;
         }
      }
      return true;
   }

Offline Bassex96

Senior Duke


Medals: 4



« Reply #2 - Posted 2013-05-29 01:32:27 »

Thanks. I got it working. How can I actually keep my pacman fully in a tile? i'm finding the collision detection is going 2 over so it's not allowing it to move. I've tried everything I can think of lol.

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  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
public void move(Map map) {
     
     
      if (direction == 0) {
         if (isNoTile(map,direction,getX(),getY())) {
         
         
       
            setY(getY() + (int)(Gdx.graphics.getDeltaTime() + 2));
         }
         }
         
     
     
      if (direction ==1) {
         if (isNoTile(map,direction,getX(),getY())) {
         setX(getX() + (int)(Gdx.graphics.getDeltaTime() + 2));
      }
      }
     
      if (direction ==2) {
         if (isNoTile(map,direction,getX(),getY())) {
         setY(getY() - (int)(Gdx.graphics.getDeltaTime() + 2));
      }
      }
     
      if (direction ==3) {
         if (isNoTile(map,direction,getX(),getY())) {
         setX(getX() - (int)(Gdx.graphics.getDeltaTime() + 2));
      }
      }
     
      }
   
   private boolean isNoTile(Map map, int dir, int translated_x, int translated_y) {
      System.out.println("X: " + translated_x + " Y: " + translated_y);
      for (Rectangle r : map.getBlockedTiles()) {
         switch (dir) {
         case 0:
            Rectangle up = new Rectangle(translated_x,translated_y , 16, 16);
            if (up.overlaps(r)) {
               
            return false;
           
            }
            break;
           
         case 1:
            Rectangle right = new Rectangle(translated_x+16,translated_y, 16, 16);
            if (right.overlaps(r)) {
            return false;
            }
            break;
           
         case 2:
            Rectangle down = new Rectangle(translated_x,translated_y - 16, 16, 16);
            if (down.overlaps(r)) {
            return false;
            }
            break;
           
         case 3:
            Rectangle left = new Rectangle(translated_x-16,translated_y, 16, 16);
            if (left.overlaps(r)) {
            return false;
            }
            break;
           
            default:
               break;
         }
      }
   return true;
   }
   
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GabrielBailey74
« Reply #3 - Posted 2013-05-29 01:49:30 »

How can I actually keep my pacman fully in a tile? i'm finding the collision detection is going 2 over so it's not allowing it to move.

You would start by initializing your players position to a tiles position (Exact same coordinates as a tile).

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
int velocity_x = tile.width;
int velocity_y = tile.height;
if (right) {
     /* Where velocity_x is = the tiles width */
     player.x += velocity_x;
}
if (up) {
     /* Where velocity_y is = the tiles height */
     player.y += velocity_y;
}


That's how I went from moving 2 pixels at a time to moving 1 tile at a time (Collision is way easier..!)

Offline Bassex96

Senior Duke


Medals: 4



« Reply #4 - Posted 2013-05-29 01:50:37 »

Thanks. I actually just figured it out. I was checking a whole tile in front of the player instead of the speed/velocity in front of the player.
Offline GabrielBailey74
« Reply #5 - Posted 2013-05-29 01:51:07 »

Great xD glad I could help :>

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.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (59 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
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

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