Java 2D les collisions entre deux rectangles
Bonjour tout le monde.
En ce moment ma curiosité me pousse à comprendre le fonctionnement des collisions entre deux objets.
Pour mes expériences, j'ai pris deux formes qui représentent un rectangle, j'ai mis des couleurs différentes pour les différencier.
Sur l'exemple je contrôle le rectangle vert et quand je déplace sur le rectangle bleu, la détection se passe bien, vous verrez un drawstring de couleur jaune en haut à gauche quand les rectangles sont en collision.
voici une image qui représente ci-dessous mon explication.
http://img15.hostingpics.net/thumbs/...ecollision.png
Je vous mets tout le code source :
Class : Programme
Code:
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
|
import javax.swing.JFrame;
public class Programme extends JFrame {
private static final long serialVersionUID = 1L;
Panneau panneau;
public Programme() {
panneau = new Panneau();
this.setTitle("titre fenetre");
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.addKeyListener(panneau);
this.add(panneau);
this.setVisible(true);
}
public static void main(String[] args) {
new Programme();
}
} |
Class: Panneau
Code:
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
import java.awt.Color;
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 javax.swing.JPanel;
import javax.swing.Timer;
public class Panneau extends JPanel implements ActionListener, KeyListener {
private static final long serialVersionUID = 1L;
public Player p1;
public Player p2;
Timer timer;
public int x;
public int y;
public int speed;
public boolean collision;
public Panneau() {
setBackground(Color.black);
p1 = new Player();
p2 = new Player();
timer = new Timer(15, this);
timer.start();
p1.x = 200;
p1.y = 200;
p2.x = 200;
p2.y = 100;
}
public void paint(Graphics gr) {
super.paintComponent(gr);
gr.setColor(Color.green);
gr.fillRect(p1.x, p1.y, p1.width, p1.height);
gr.setColor(Color.blue);
gr.fillRect(p2.x, p2.y, p2.width, p2.height);
if (collision) {
gr.setColor(Color.yellow);
gr.drawString("COLLISION", 0, 15);
}
}
public void collision() {
Rectangle rect1 = p1.bounds();
Rectangle rect2 = p2.bounds();
if (rect1.intersects(rect2)) {
collision = true;
} else {
collision = false;
}
}
@Override
public void keyPressed(KeyEvent e) {
speed = 1;
if (e.getKeyCode() == KeyEvent.VK_Z) {
y = -speed;
}
if (e.getKeyCode() == KeyEvent.VK_S) {
y = +speed;
}
if (e.getKeyCode() == KeyEvent.VK_Q) {
x = -speed;
}
if (e.getKeyCode() == KeyEvent.VK_D) {
x = +speed;
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_Z) {
y = 0;
}
if (e.getKeyCode() == KeyEvent.VK_S) {
y = 0;
}
if (e.getKeyCode() == KeyEvent.VK_Q) {
x = 0;
}
if (e.getKeyCode() == KeyEvent.VK_D) {
x = 0;
}
}
@Override
public void keyTyped(KeyEvent arg0) {
}
public void move() {
p1.x += x;
p1.y += y;
}
@Override
public void actionPerformed(ActionEvent arg0) {
move();
collision();
repaint();
}
} |
Class: Player
Code:
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
|
import java.awt.Rectangle;
public class Player {
public int x;
public int y;
public int width;
public int height;
public Player() {
x = 0;
y = 0;
width = 64;
height = 64;
}
public int getProfondeurY() {
return y + height;
}
public int getProfondeurX() {
return x + width;
}
public Rectangle bounds() {
return (new Rectangle(x, y, width, height));
}
} |
Ma question est : Comment je pourrais empêcher le rectangle vert d'entrer dans le périmètre du rectangle bleu ?
Je me doute bien qu'il faudrait mettre des conditions if, mais si je suis venue vous demander de l'aide c'est que je ne parviens pas à mes fins.
D'avance merci.