Tuesday, March 15, 2016

collision detection - Making MineCraft In 2D


I am making a 2D game that is based on Minecraft. I made the terrain but I just don't know how to do the collision between the player. I tied to draw the rectangles with a for loop and detect collision between the player player and the rectangles but when I run it it only detects collision between the last rectangle on the map and the player. Please help.


enter image description here


This is my code.


    import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Gameplay extends JPanel implements ActionListener, KeyListener{
private BufferedImage player;
private BufferedImage block;

private BufferedImage dbImage;
private ImageIcon player2;
private Graphics dbg;
private int x = 0;
private int y = 0;
private double velX = 5.0f;
private double velY = 5.0f;
public Rectangle rectangle;
public Rectangle rectangle2;
private int[] xb = {0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650};

private int[] yb = {0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650};
//
public Gameplay(){
addKeyListener(this);
setFocusable(true);
}

public void paint(Graphics g){
dbImage = (BufferedImage) createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();

paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);
repaint();
}
public void paintComponent(Graphics g){
try {
player = ImageIO.read(getClass().getResource("Player.png"));
block = ImageIO.read(getClass().getResource("Grass.png"));
} catch (IOException e) {
// TODO Auto-generated catch block

e.printStackTrace();
}
drawBlock(g);
g.drawImage(player, x, y, this);
rectangle2 = new Rectangle(x, y, 50, 50);
if(rectangle2.intersects(rectangle)){
System.out.println("Hello");
}
}


public void drawBlock(Graphics g){
for(int first = 0; first < 10; first++){
for(int second = 0; second < 10; second++){
g.drawImage(block, xb[first], yb[second], this);
rectangle = new Rectangle(xb[first], yb[second], 50, 50);
}
}

}


@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP){
y += -velY;

}
else if(e.getKeyCode() == KeyEvent.VK_DOWN){
y += velY;
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT){
x += -velX;
}
else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
x += velX;
}



}

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}


@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}
}


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...