Skip to content

Instantly share code, notes, and snippets.

@avh4
Created August 7, 2008 07:27
Show Gist options
  • Save avh4/4356 to your computer and use it in GitHub Desktop.
Save avh4/4356 to your computer and use it in GitHub Desktop.
Example of collision detection using java.awt.geom.Area with AffineTransform's
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package collisiondemo;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import javax.swing.JComponent;
/**
*
* @author avh4
*/
public class DemoPainter extends JComponent {
private DemoState theDemoState;
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
// Draw the triangle
Triangle t = theDemoState.getTheTriangle();
g2.setTransform(new AffineTransform());
g2.setColor(Color.GREEN);
if (theDemoState.doTrianglesIntersect())
{
g2.setColor(Color.BLUE);
}
g2.translate(t.getX(), t.getY());
g2.rotate(Math.toRadians(-90));
g2.rotate(Math.toRadians(t.getHeading()));
g2.scale(50, 100);
g2.fillPolygon(t.getShape());
// Draw the main triangle's center dot
g2.setTransform(new AffineTransform());
g2.setColor(Color.RED);
g2.translate(t.getX(), t.getY());
g2.fillOval(-1, -1, 3, 3);
// Draw the tiny triangle
t = theDemoState.getTinyTriangle();
g2.setTransform(new AffineTransform());
g2.setColor(Color.GREEN);
if (theDemoState.doTrianglesIntersect())
{
g2.setColor(Color.BLUE);
}
g2.translate(t.getX(), t.getY());
g2.rotate(Math.toRadians(-90));
g2.rotate(Math.toRadians(t.getHeading()));
g2.scale(4, 7);
g2.fillPolygon(t.getShape());
// Draw the tiny triangle's center dot
g2.setTransform(new AffineTransform());
g2.setColor(Color.RED);
g2.translate(t.getX(), t.getY());
g2.fillOval(-1, -1, 3, 3);
}
public void setTheDemoState(DemoState theDemoState)
{
this.theDemoState = theDemoState;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package collisiondemo;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
/**
*
* @author avh4
*/
public class DemoState {
private Triangle mainTriangle;
private Triangle tinyTriangle;
public void setUpDemo() {
mainTriangle = new Triangle();
getTheTriangle().setX(200);
getTheTriangle().setY(250);
getTheTriangle().setHeading(0);
tinyTriangle = new Triangle();
tinyTriangle.setX(300);
tinyTriangle.setY(300);
tinyTriangle.setHeading(80);
}
public void moveObjects() {
getTheTriangle().setX(getTheTriangle().getX() + 1);
mainTriangle.setHeading(mainTriangle.getHeading() + 1);
tinyTriangle.setHeading(tinyTriangle.getHeading() - 2);
}
public boolean doTrianglesIntersect() {
// Set up the tiny triangle transform
AffineTransform tinyTriangleTransform = new AffineTransform();
tinyTriangleTransform.translate(tinyTriangle.getX(), tinyTriangle.getY());
tinyTriangleTransform.rotate(Math.toRadians(-90));
tinyTriangleTransform.rotate(Math.toRadians(tinyTriangle.getHeading()));
tinyTriangleTransform.scale(4, 7);
// Create the transformed tiny triangle area from it's shape
Area tinyArea = new Area(tinyTriangle.getShape());
tinyArea.transform(tinyTriangleTransform);
// Set up the main triangle transform
AffineTransform mainTriangeTransform = new AffineTransform();
mainTriangeTransform.translate(mainTriangle.getX(), mainTriangle.getY());
mainTriangeTransform.rotate(Math.toRadians(-90));
mainTriangeTransform.rotate(Math.toRadians(mainTriangle.getHeading()));
mainTriangeTransform.scale(50, 100);
// Create the transformed main triangle area from it's shape
Area mainTriangleArea = new Area(mainTriangle.getShape());
mainTriangleArea.transform(mainTriangeTransform);
// Find the overlap of the main triangle area and the tiny triangle area
Area overlapArea = (Area) mainTriangleArea.clone();
overlapArea.intersect(tinyArea);
return ! overlapArea.isEmpty();
}
public Triangle getTinyTriangle()
{
return tinyTriangle;
}
public Triangle getTheTriangle()
{
return mainTriangle;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package collisiondemo;
import java.awt.Component;
import javax.swing.JFrame;
/**
*
* @author avh4
*/
public class DemoWindow {
private DemoPainter thePainter;
public void createWindow() {
JFrame window = new JFrame("Collision Demo");
window.add(thePainter);
window.pack();
window.setSize(700, 700);
window.setLocationRelativeTo(null);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setThePainter(DemoPainter thePainter)
{
this.thePainter = thePainter;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package collisiondemo;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author avh4
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
DemoState theDemoState = new DemoState();
theDemoState.setUpDemo();
DemoPainter mainPainter = new DemoPainter();
mainPainter.setTheDemoState(theDemoState);
DemoWindow mainWindow = new DemoWindow();
mainWindow.setThePainter(mainPainter);
mainWindow.createWindow();
while (true) {
mainPainter.repaint();
try
{
Thread.sleep(100);
}
catch (InterruptedException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
theDemoState.moveObjects();
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package collisiondemo;
import java.awt.Polygon;
/**
*
* @author avh4
*/
public class Triangle {
private double x;
private double y;
private double heading;
int xpoints[] = { 0, 1, -1, 0 };
int ypoints[] = { 1, 0, 0, 1 };
private Polygon shape = new Polygon(xpoints, ypoints, xpoints.length);
public double getX()
{
return x;
}
public void setX(double x)
{
this.x = x;
}
public double getY()
{
return y;
}
public void setY(double y)
{
this.y = y;
}
public double getHeading()
{
return heading;
}
public void setHeading(double heading)
{
this.heading = heading;
}
public Polygon getShape()
{
return shape;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment