package greenfoot.collision;
import greenfoot.TestUtilDelegate;
import greenfoot.World;
import greenfoot.TestObject;
import greenfoot.WorldCreator;
import greenfoot.core.Simulation;
import greenfoot.core.WorldHandler;
import greenfoot.util.GreenfootUtil;
import java.util.Collection;
import junit.framework.TestCase;
| Test for collisions between Actors
|
| @author Poul Henriksen
|
public class IntersectionTest
extends TestCase{
private World world;
@Override
protected void setUp()
throws Exception
{
GreenfootUtil.initialise(new TestUtilDelegate());
Simulation.initialize();
}
@SuppressWarnings("unchecked")
public void testIntersectingSingleCell()
{
world = WorldCreator.createWorld(10, 10, 10);
TestObject o1 = new TestObject(10,10);
world.addObject(o1, 2, 2);
TestObject o2 = new TestObject(10,10);
world.addObject(o2, 2, 2);
assertTrue(o1.intersectsP(o2));
assertTrue(o2.intersectsP(o1));
Collection c = o1.getIntersectingObjectsP(TestObject.class);
assertTrue(c.contains(o2));
assertEquals(1, c.size());
o2.setLocation(3,2);
assertFalse(o1.intersectsP(o2));
assertFalse(o2.intersectsP(o1));
}
@SuppressWarnings("unchecked")
public void testIntersectingPixelLevelOdd()
{
world = WorldCreator.createWorld(70, 70, 1);
WorldHandler.initialise();
WorldHandler.getInstance().setWorld(world, false);
TestObject o1 = new TestObject(7,7);
world.addObject(o1, 0 ,0);
TestObject o2 = new TestObject(7,7);
world.addObject(o2, 6, 6);
assertTrue(o1.intersectsP(o2));
assertTrue(o2.intersectsP(o1));
Collection c = o1.getIntersectingObjectsP(TestObject.class);
assertTrue(c.contains(o2));
assertEquals(1, c.size());
o2.setLocation(7,7);
assertFalse(o1.intersectsP(o2));
assertFalse(o2.intersectsP(o1));
}
@SuppressWarnings("unchecked")
public void testIntersectingPixelLevelEven()
{
world = WorldCreator.createWorld(80, 80, 1);
TestObject o1 = new TestObject(8,8);
world.addObject(o1, 0 ,0);
TestObject o2 = new TestObject(8,8);
world.addObject(o2, 7, 7);
assertTrue(o1.intersectsP(o2));
assertTrue(o2.intersectsP(o1));
Collection c = o1.getIntersectingObjectsP(TestObject.class);
assertTrue(c.contains(o2));
assertEquals(1, c.size());
o2.setLocation(8,8);
assertFalse(o1.intersectsP(o2));
assertFalse(o2.intersectsP(o1));
o2.setLocation(9,9);
assertFalse(o1.intersectsP(o2));
assertFalse(o2.intersectsP(o1));
}
public void testRotationIntersection45()
{
world = WorldCreator.createWorld(200, 200, 1);
TestObject o1 = new TestObject(50,50);
world.addObject(o1, 0 ,0);
TestObject o2 = new TestObject(50,50);
world.addObject(o2, 55 ,0);
assertNull( o2.getOneIntersectingObjectP(TestObject.class));
o2.setRotation(45);
assertEquals(o1, o2.getOneIntersectingObjectP(TestObject.class));
o2.setLocation(55, 55);
assertNull(o2.getOneIntersectingObjectP(TestObject.class));
o1.setRotation(45);
assertNull(o2.getOneIntersectingObjectP(TestObject.class));
}
public void testRotationIntersection90()
{
world = WorldCreator.createWorld(200, 200, 1);
TestObject o1 = new TestObject(100,10);
world.addObject(o1, 0 ,0);
TestObject o2 = new TestObject(10,10);
world.addObject(o2, 0, 40);
assertNull( o2.getOneIntersectingObjectP(TestObject.class));
o1.setRotation(90);
assertEquals(o1, o2.getOneIntersectingObjectP(TestObject.class));
}
| Tests intersection when both actors are rotated by the same multiple of 90 degrees
|
public void testRotationIntersectionBoth()
{
world = WorldCreator.createWorld(100, 100, 1);
TestObject actor1 = new TestObject(3,3);
TestObject actor2 = new TestObject(3,3);
int xoffs = 2;
int yoffs = 0;
for (int rot = 0; rot < 360; rot += 90) {
actor1.setRotation(rot);
actor2.setRotation(rot);
world.addObject(actor1, 50, 50);
world.addObject(actor2, 50 + xoffs, 50 + yoffs);
assertTrue(actor2.isTouchingP(TestObject.class));
int pyoffs = yoffs;
yoffs = xoffs;
xoffs = -pyoffs;
world.removeObject(actor1);
world.removeObject(actor2);
}
}
public void testIntersectionSmallObject()
{
world = WorldCreator.createWorld(200, 200, 1);
TestObject o1 = new TestObject(100,100);
world.addObject(o1, 55, 55);
TestObject o2 = new TestObject(1,1);
o2.setRotation(45);
world.addObject(o2, 56, 56);
assertNotNull(o2.getOneIntersectingObjectP(TestObject.class));
o1.setRotation(45);
o2.setLocation(100, 100);
assertNull(o2.getOneIntersectingObjectP(TestObject.class));
}
}
top,
use,
map,
class IntersectionTest
. setUp
. testIntersectingSingleCell
. testIntersectingPixelLevelOdd
. testIntersectingPixelLevelEven
. testRotationIntersection45
. testRotationIntersection90
. testRotationIntersectionBoth
. testIntersectionSmallObject
220 neLoCode
+ 3 LoComm