Original Post
🔒 Locked
Java tile Hit Detection
Advertisement
The general way to go about collision detection in a 2D tile environment is to stop your sprite (or whatever is moving) and perform some kind of animation if it is trying to move on a kind of tile that wouldn't be natural. In this type of a setup, each kind of tile (grass, water, rock, etc.) has its own number in an array, and that is how you are loading the tiles in the first place. So, each time you have something move, check to see if the tile right in front of it is solid, and if so perform some animation or make a sound or just have it stop.
Is that what you were looking for?
Is that what you were looking for?
Without more details of your setup, I can't give you an exact method of detecting collision.
I assume you know which tile your character is on. If you're going to move left one space, instead of Player.x--
Do something like. If(tiles[Player.x - 1].isWalkable()) Player.x--;
You can check to see if the tile is walkable or not the same way you draw them. You know when you're drawing a water tile. If water is not walkable... check to see if it's a water tile similar to how you draw it.
I assume you know which tile your character is on. If you're going to move left one space, instead of Player.x--
Do something like. If(tiles[Player.x - 1].isWalkable()) Player.x--;
You can check to see if the tile is walkable or not the same way you draw them. You know when you're drawing a water tile. If water is not walkable... check to see if it's a water tile similar to how you draw it.
Here is my code...and how do i know or declare a tile walkable
import java.awt.*;import javax.swing.*;import java.applet.*;import java.awt.event.*;import java.util.*;import javax.swing.*; public class GreatGame extends Applet implements KeyListener,Runnable{//Variable Declaration Cursor c = new Cursor (Cursor.CROSSHAIR_CURSOR); private Image dbImage; private Graphics dbg; Thread th; String str; // DEFINE THE BULLETS int joint; int score=0; int x_1; int y_1; int x_2; int y_2; int x_3; int y_3; // END DEFING THE BULLETS int across; int vert; int x=45; int y=45; int width = 15; int height = 15; int[][] A = { {3,0,0,0,0,0,0,0,0,0,1,0,0,0,1}, {1,1,1,1,1,1,1,1,0,0,0,1,0,1,1}, {1,1,1,1,0,0,0,1,0,0,0,1,1,1,1}, {1,1,0,1,1,0,0,1,0,0,0,1,0,1,1}, {1,1,0,1,1,0,0,1,0,0,0,1,3,1,1}, {1,1,0,1,1,0,0,1,0,0,0,1,0,1,1}, {1,1,0,3,1,1,1,1,1,1,1,1,0,1,1}, {1,1,0,0,1,1,1,1,1,1,1,1,0,1,1}, {1,1,1,1,1,1,0,0,0,0,0,1,0,1,1}, {1,3,0,0,0,1,0,4,1,1,1,1,1,1,1}, {1,0,0,0,0,1,0,0,1,0,0,0,0,1,1}, {1,0,0,0,0,1,0,0,1,0,0,0,0,1,1}, {1,0,0,0,0,1,3,0,1,0,0,1,1,1,1}, {1,1,1,1,1,1,1,1,1,0,0,1,0,1,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} }; //End Variable Delcaration // Image Declaration Image img = Toolkit.getDefaultToolkit().getImage("Game.GIF"); Image pic = Toolkit.getDefaultToolkit().getImage("Game1.GIF"); Image hero = Toolkit.getDefaultToolkit().getImage("CHara.GIF"); Image attach = Toolkit.getDefaultToolkit().getImage("Game3.GIF"); Image bullet = Toolkit.getDefaultToolkit().getImage("Bullet.GIF"); // End Image Delclaration MediaTracker track = new MediaTracker(this); public void init() { setSize(225,240); try { repaint(); th.sleep(50); } catch (InterruptedException e){} // Register images with media tracker track.addImage(img, 0); track.addImage(pic,1); track.addImage(hero,3); track.addImage(attach,4); track.addImage(bullet,5); // End registering this.setCursor(c); addKeyListener(this); } /************************************************************** * STARTING THE THREAD * *************************************************************/ public void run() { while(th == Thread.currentThread()) { repaint(); x_1=x_1+15; x_2=x_2+15; x_3=x_3+15; score++; try{Thread.sleep(50); } catch(InterruptedException e){} } } public void start() { if(th == null) th = new Thread(this); th.start(); } public void stop() { Graphics gr = getGraphics(); if(th != null) th = null; if(score > 400) { gr.setColor(Color.red); gr.drawString("NICE NOT BAD",75,200); } else if(score < 400){ gr.setColor(Color.black); gr.drawString("HAHHA YOU SUCK",75,200); } } /**************************************************************** * ENDING THE START OF THE THREAD * ****************************************************************/ public void paint(Graphics g) { for(int across = 0; across < width ; across++) { for(int vert = 0; vert < height ; vert++) { if (A[across][vert] == 0) { // we draw img g.drawImage(pic,across*15,vert*15,this); } else if(A[across][vert]==1){ // we draw pic g.drawImage(img,across*15,vert*15,this); String str = "BLOCKED"; }else{ g.drawImage(attach,across*15,vert*15,this); } } } //Text To show where hero is and draw Hero g.drawString("x = "+x+"y = "+y,10,235); g.drawImage(bullet,x_1,y_1,this); g.drawImage(bullet,x_2,y_2,this); g.drawImage(hero,x,y,this); g.drawImage(bullet,x_3,y_3,this); Font myFot = new Font("Serif",Font.BOLD,12); g.setFont(myFot); g.drawString("SCORE :"+score,150,235); /************************************************************** * HIT DETECTION * *************************************************************/ if(x <= 0) { x=0; } if( y<= 0) { y=0; } if(x >= 210) { x=210; } if(y >= 210) { y=210; } //BULLET HIT DETECTION if(x_1 > 210) { x_1= -30; y_1 = (int) Math.ceil(Math.random()*14)*15; } if(x_2 > 210) { x_2 = -15; y_2 = (int) Math.ceil(Math.random()*14)*15; } if(x_3 > 210) { x_3 = -5; y_3 = (int) Math.ceil(Math.random()*14)*15; } if(x_1 == x && y_1 == y) { Font myFont = new Font("Serif",Font.BOLD,20); g.setFont(myFont); g.setColor(Color.red); g.drawString("YOU LOSE!",60,80); stop(); g.setColor(Color.white); g.drawString("YOUR SCORE :"+score,30,30); } if(x_2 == x && y_2 == y) { Font myFont = new Font("Serif",Font.BOLD,20); g.setFont(myFont); g.setColor(Color.red); g.drawString("YOU LOSE!",60,80); stop(); g.setColor(Color.white); g.drawString("YOUR SCORE :"+score,30,30); } if(x_3 == x && y_3 == y) { Font myFont = new Font("Serif",Font.BOLD,20); g.setFont(myFont); g.setColor(Color.red); g.drawString("YOU LOSE!",60,80); g.setColor(Color.white); g.drawString("YOUR SCORE :"+score,30,30); stop(); } //END BULLET HIT DETECTION /************************************************************** * END HIT DETECTION * *************************************************************/ } public int[][] getA() { return A; } public void keyPressed(KeyEvent evt) { switch (evt.getKeyCode()) { case KeyEvent.VK_LEFT: x=x-15; repaint(); break; case KeyEvent.VK_RIGHT: repaint(); break; case KeyEvent.VK_UP: y=y-15; repaint(); break; case KeyEvent.VK_DOWN: y=y+15; repaint(); break; default: // don't do anything, not an arrow pressed } } public void keyReleased(KeyEvent evt) { } public void keyTyped(KeyEvent evt) { } /******************************************************************** * DOUBLE BUFFERING * *******************************************************************/ public void update (Graphics g){ // initialize buffer if (dbImage == null) { dbImage = createImage (this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics (); } // clear screen in background dbg.setColor (getBackground ()); dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); // draw elements in background dbg.setColor (getForeground()); paint (dbg); // draw image on the screen g.drawImage (dbImage, 0, 0, this); /****************************************************************** * END DOUBLE BUFFERING * *****************************************************************/} }Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement