I just made my first game in java, but the collision detection completely sucks. Does anyone have an idea on how to fix it? I've tried multiple things already but none of them worked. I've the rocks come from above the player they get detected, but if the player hits them sideways they don't hit him.
public void checkRock(){
if((x == rockX && y == rockY) || (x==rockX && y+1 ==rockY) || (x==rockX && y+2 ==rockY)){
running = false;
}
else if((x+1 == rockX && y == rockY) || (x+1==rockX && y+1 ==rockY) || (x+1==rockX && y+2 ==rockY)){
running = false;
}
if(!running){
timer.stop();
}
}
Full code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.AttributeSet.ColorAttribute;
import java.util.Random;
import java.util.random.*;
public class GamePanel extends JPanel implements ActionListener{
static final int SCREEN_WIDTH = 600;
static final int SCREEN_HEIGHT = 600;
static final int UNIT_SIZE = 25;
static final int GAME_UNITS = (SCREEN_HEIGHT*SCREEN_WIDTH)/UNIT_SIZE;
static final int DELAY = 75;
int x = 0;
int y = SCREEN_HEIGHT-(UNIT_SIZE*3);
int rocksFallen;
int rockX;
int rockY = 0;
char direction = 'R';
char directionRock = 'D';
boolean running = false;
Timer timer;
Random random;
GamePanel(){
random = new Random();
this.setPreferredSize(new Dimension(SCREEN_WIDTH,SCREEN_HEIGHT));
this.setBackground(Color.BLACK);
this.setFocusable(true);
this.addKeyListener(new MyKeyAdapter());
startGame();
}
public void startGame(){
newRock();
running = true;
timer = new Timer(DELAY,this);
timer.start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g){
if(running){
for(int i = 0;i<SCREEN_HEIGHT/UNIT_SIZE;i++){
g.drawLine(i*UNIT_SIZE, 0, i*UNIT_SIZE, SCREEN_HEIGHT);
g.drawLine(0, i*UNIT_SIZE, SCREEN_WIDTH, i*UNIT_SIZE);
}
g.setColor(Color.red);
g.fillOval(rockX, rockY, UNIT_SIZE, UNIT_SIZE);
g.setColor(Color.green);
g.fillRect(x, y, UNIT_SIZE*2, UNIT_SIZE*3);
}
else{
gameOver(g);
}
}
public void newRock(){
rockX = random.nextInt((int)(SCREEN_WIDTH/UNIT_SIZE))*UNIT_SIZE;
rockY = 0;
}
public void move(){
switch (direction){
case 'R':
if (x<SCREEN_WIDTH-(UNIT_SIZE*2)){
x = x+UNIT_SIZE;
}
break;
case 'L':
if (x>0){
x = x-UNIT_SIZE;
}
break;
}
if(directionRock=='D'){
rockY = rockY+UNIT_SIZE;
}
}
public void checkRock(){
if((x == rockX && y == rockY) || (x==rockX && y+1 ==rockY) || (x==rockX && y+2 ==rockY)){
running = false;
}
else if((x+1 == rockX && y == rockY) || (x+1==rockX && y+1 ==rockY) || (x+1==rockX && y+2 ==rockY)){
running = false;
}
if(!running){
timer.stop();
}
}
public void checkCollisions(){
if(rockY>SCREEN_HEIGHT){
newRock();
rocksFallen++;
}
}
public void gameOver(Graphics g){
g.setColor(Color.red);
g.setFont(new Font("Ink Free",Font.BOLD,75));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Game Over", (SCREEN_WIDTH-metrics.stringWidth("Game OVer"))/2, SCREEN_HEIGHT/2);
}
@Override
public void actionPerformed(ActionEvent e){
if(running){
move();
checkRock();
checkCollisions();
}
repaint();
}
public class MyKeyAdapter extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e){
switch (e.getKeyCode()){
case KeyEvent.VK_LEFT:
direction = 'L';
break;
case KeyEvent.VK_RIGHT:
direction = 'R';
break;
}
}
}
}
GameFrame code:
import javax.swing.*;
import javax.swing.text.AttributeSet.ColorAttribute;
import java.util.Random;
import java.util.random.*;
public class GameFrame extends JFrame{
GameFrame(){
this.add(new GamePanel());
this.setTitle("FlyingRocks");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.pack();
this.setVisible(true);
this.setLocationRelativeTo(null);
}
}
Main Code:
public class Game extends JFrame{
public static void main(String[] args) {
new GameFrame();
}
}