robocode.util
Class Collision

java.lang.Object
  extended by robocode.util.Collision

public class Collision
extends Object

This class contains more advanced collision detection methods then the original Robocode has. It also contains some helper methods to make the job slightly easier.

Since:
Robocode 1.9.1.2 / Naval Edition
Version:
1.1
Author:
Thales B.V. / Jiri Waning / Thomas Hakkers

Constructor Summary
Collision()
           
 
Method Summary
static boolean collide(ITransformable p0, ITransformable p1)
          Determines whether two robots collided.
static boolean collide(ITransformable p, Line2D.Double l)
          Determines whether the robot collides with a Line2D.Double.
static boolean collide(ITransformable p, Rectangle2D rectangle)
          THOMA: STILL NEEDS A FEW JUNIT TESTS Collision method that is mainly used for mines.
static boolean collide(Line2D.Double lineA, Line2D.Double lineB)
          Determines whether the Line2D.Double intersect with each other.
protected static HashMap<Integer,List<Integer>> collisionPointsPolygon(Polygon p0, Polygon p1)
          Returns a HashMap with a list of Integers in it.
protected static ArrayList<Point> distinct(ITransformable peer)
          Generates a list of distinct points from the bounding box of a robot.
protected static ArrayList<Point> distinct(Polygon p)
          Generates a list of distinct points from a polygon.
static Map<Integer,List<Integer>> getCollisionPoints(ITransformable p0, ITransformable p1)
          Returns a List with all the points of collision.
protected static Polygon getPolygon(Arc2D a, AffineTransform at)
          Generates a Polygon that resembles the given Arc2D, when the given AffineTransform is applied.
static Polygon getPolygon(ITransformable peer)
          Get the polygon of a peer.
static Polygon getPolygon(Rectangle2D r, AffineTransform at)
          Generates a polygon that resembles the given rectangle, when the given transformation is applied.
static Rectangle2D getRectangle(ITransformable peer)
          Get the bounding box rectangle for the specified robot.
protected static boolean insidePolygon(ITransformable peer, Line2D.Double line)
          Determines whether the line is inside the polygon of the peer.
static boolean insidePolygon(Polygon p0, Polygon p1)
           
static boolean insideScan(Arc2D a, double x, double y)
          Determines whether or not the projectile is inside the scan arc.
static boolean insideScan(Arc2D a, ITransformable peer)
          Determines whether or not the peer is inside the shape.
protected static boolean intersect(Line2D.Double lineA, Line2D.Double lineB)
          Determines whether two (straight) lines intersect with each other.
protected static boolean intersect(Rectangle2D r, AffineTransform at, Line2D.Double line)
          Determines whether the rectangle and line intersect with each other.
protected static boolean intersect(Rectangle2D r0, AffineTransform at0, Rectangle2D r1, AffineTransform at1)
          Determines whether two rectangles intersect with each other.
protected static boolean intersectLine(Polygon p, Line2D.Double lineA)
          Determines whether the line intersects with the polygon.
protected static boolean intersectPolygon(Polygon p0, Polygon p1)
          Determines whether two polygons intersect with each other.
static Polygon rectangleToPolygon(Rectangle2D rectangle)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Collision

public Collision()
Method Detail

intersect

protected static final boolean intersect(Line2D.Double lineA,
                                         Line2D.Double lineB)
Determines whether two (straight) lines intersect with each other.

This is a workaround for Line2D.linesIntersect which returns an incorrect value on certain versions of Java. This is confirmed to be at least the case in Java 5. (Status as of writing is still unresolved!)

Parameters:
lineA - The first of the two lines that has to intersect.
lineB - The second of the two lines that has to intersect.
Returns:
true if the lines intersect with one another; false otherwise.

getPolygon

public static final Polygon getPolygon(Rectangle2D r,
                                       AffineTransform at)
Generates a polygon that resembles the given rectangle, when the given transformation is applied.

Parameters:
r - The rectangle on which the transformation has to take place.
at - The transformation to apply.
Returns:
A Polygon representing the transformed rectangle.

getPolygon

protected static final Polygon getPolygon(Arc2D a,
                                          AffineTransform at)
Generates a Polygon that resembles the given Arc2D, when the given AffineTransform is applied.

Parameters:
a - The Arc2D on which the transformation has to take place.
at - The AffineTransform to apply to the Arc2D.
Returns:
A Polygon representing the transformed rectangle.

distinct

protected static final ArrayList<Point> distinct(ITransformable peer)
Generates a list of distinct points from the bounding box of a robot.

Parameters:
peer - The peer from whom to get the distinct points of its bounding box.
Returns:
ArrayList<Point> containing the distinct points of a robot's bounding box.

distinct

protected static final ArrayList<Point> distinct(Polygon p)
Generates a list of distinct points from a polygon.

Parameters:
p - The polygon from whom to get the points.
Returns:
ArrayList<Point> containing the distinct points of a polygon.

intersectLine

protected static final boolean intersectLine(Polygon p,
                                             Line2D.Double lineA)
Determines whether the line intersects with the polygon.

Parameters:
p - The polygon that has the intersect.
lineA - The line that has to intersect the polygon.
Returns:
true when the polygon and line intersect. false otherwise.

insidePolygon

protected static final boolean insidePolygon(ITransformable peer,
                                             Line2D.Double line)
Determines whether the line is inside the polygon of the peer.

Parameters:
peer - The peer that should contain the line.
line - The line that should be inside the polygon.
Returns:
true when the line is inside the polygon. false otherwise.

intersectPolygon

protected static final boolean intersectPolygon(Polygon p0,
                                                Polygon p1)
Determines whether two polygons intersect with each other.

Parameters:
p0 - The first polygon that has to intersect.
p1 - The second polygon that has to intersect.
Returns:
true when the polygons intersect.

collisionPointsPolygon

protected static final HashMap<Integer,List<Integer>> collisionPointsPolygon(Polygon p0,
                                                                             Polygon p1)
Returns a HashMap with a list of Integers in it. Only used for Ship at the moment The sides of a Ship are like so: 1 / \ 0 | | 2 |__| 3 Say we got two Ships of which Ship1 has hit Ship2 from the side (Ship1_Side1 hit Ship2_Side0) In that case this Map would return the following. 0 : Nothing 1 : 0 (as in: Side 0) 2 : Nothing 3 : Nothing Based on this Map you can conclude that Ship1 rammed Ship2 his Side0, using Side1.

Parameters:
p0 - "Ship1" It's the Polygon you will get the collision points of
p1 - "Ship2" The Polygon colliding with p0
Returns:
A Hashmap with the Collision points of the given polygon.

intersect

protected static final boolean intersect(Rectangle2D r,
                                         AffineTransform at,
                                         Line2D.Double line)
Determines whether the rectangle and line intersect with each other.

The transformation of the rectangle will be applied before determining whether the rectangle and line intersect!

Parameters:
r - The rectangle that has to intersect with the line.
at - The transformation that has to be applied to the rectangle.
line - The line that has to intersect with the rectangle.
Returns:
true when the rectangle and line intersect.

intersect

protected static final boolean intersect(Rectangle2D r0,
                                         AffineTransform at0,
                                         Rectangle2D r1,
                                         AffineTransform at1)
Determines whether two rectangles intersect with each other.

Parameters:
r0 - The first rectangle that has to intersect.
at0 - The transformation of the first rectangle.
r1 - The second rectangle that has to intersect.
at1 - The transformation of the second rectangle.
Returns:
true when the two rectangles intersect; false otherwise.

collide

public static final boolean collide(ITransformable p0,
                                    ITransformable p1)
Determines whether two robots collided.

Parameters:
p0 - The first robot that has to collide.
p1 - The second robot that has to collide.
Returns:
true when the robots collide. false otherwise.

getCollisionPoints

public static Map<Integer,List<Integer>> getCollisionPoints(ITransformable p0,
                                                            ITransformable p1)
Returns a List with all the points of collision. This function is used to check where a Ship got hit and by which side. * The sides of a Ship are like so: 1 / \ 0 | | 2 |__| 3 With this Map you can ask for each side, by which sides of the other Ship you got hit.

Parameters:
p0 - The Peer we want to check the Collision of
p1 - The Peer that collides with the above peer
Returns:

insideScan

public static final boolean insideScan(Arc2D a,
                                       ITransformable peer)
Determines whether or not the peer is inside the shape.

Parameters:
a - The scan arc of the ship's radar.
peer - The peer that may be spotted by the radar.
Returns:
true when the ship is inside the arc. false otherwise.

insideScan

public static final boolean insideScan(Arc2D a,
                                       double x,
                                       double y)
Determines whether or not the projectile is inside the scan arc.

Parameters:
a - The scan arc that should scan the projectile.
projectile - The projectile that has to be scanned.
Returns:
true if the projectile got scanned; false otherwise.

collide

public static final boolean collide(ITransformable p,
                                    Line2D.Double l)
Determines whether the robot collides with a Line2D.Double.

Parameters:
p - The peer that has to collide.
l - The line that has to collide with the peer.
Returns:
true when the peer and the line collide. false otherwise.

collide

public static final boolean collide(Line2D.Double lineA,
                                    Line2D.Double lineB)
Determines whether the Line2D.Double intersect with each other.

Parameters:
lineA - The first line that has to intersect.
lineB - The second line that has to intersect.
Returns:
true when the lines intersect. false otherwise.

collide

public static final boolean collide(ITransformable p,
                                    Rectangle2D rectangle)
THOMA: STILL NEEDS A FEW JUNIT TESTS Collision method that is mainly used for mines. Checks whether a Rectangle (standing up straight) collides with a turning rectangle (like that of a ship)

Parameters:
p -
rectangle -
Returns:

insidePolygon

public static boolean insidePolygon(Polygon p0,
                                    Polygon p1)

rectangleToPolygon

public static Polygon rectangleToPolygon(Rectangle2D rectangle)

getRectangle

public static final Rectangle2D getRectangle(ITransformable peer)
Get the bounding box rectangle for the specified robot.

Parameters:
peer - The robot from whom to get the bounding box rectangle.
Returns:
Bounding box Rectangle2D

getPolygon

public static final Polygon getPolygon(ITransformable peer)
Get the polygon of a peer.

Parameters:
peer - The peer to get the polygon from.
Returns:
The polygon of the peer.


Copyright © 2015 Robocode. All Rights Reserved.