donderdag 7 april 2011

Rectangle Collision Example

This is a simple example that shows you how to check for collision between 2 rectangles.

 
import java.awt.*;
import java.applet.*;

public class RectangleCollision extends Applet {
 public void init() {
 }
 public void paint(Graphics g) {
  Rectangle rec1 = new Rectangle(10,10,20,20);
  Rectangle rec2 = new Rectangle(15,15,20,20);
  g.fillRect(rec1.x,rec1.y,rec1.width,rec1.height);
  g.fillRect(rec2.x,rec2.y,rec2.width,rec2.height);
  if(rec1.intersects(rec2))
  g.drawString("Filled Rectangle Collision", 50, 60 );
 }
}

This type of collision is mostly used. I think it is faster for the cpu then the polygon and area collision but I have no idea how much faster.

You can copy and paste the example, it should work directly.

Geen opmerkingen:

Een reactie posten