001 /** 002 * Tests the functionality of GamePhysics -- moving objects around 003 */ 004 package test; 005 006 import java.util.ArrayList; 007 import java.util.List; 008 009 import junit.framework.TestCase; 010 import physics3d.GamePhysics; 011 import physics3d.PhysicsShape; 012 import physics3d.Vect3; 013 014 import components.Ball; 015 import components.GameObject; 016 import components.GameSettings; 017 import components.GameSpace; 018 import components.SquareBumper; 019 020 /** 021 * @author ruthdhan 022 * 023 */ 024 public class TestGamePhysics extends TestCase { 025 026 //fields 027 GameSpace gs; 028 Ball b; 029 030 /** 031 * @param name 032 */ 033 public TestGamePhysics(String name) { 034 super(name); 035 } 036 037 /* (non-Javadoc) 038 * @see junit.framework.TestCase#setUp() 039 */ 040 protected void setUp() throws Exception { 041 super.setUp(); 042 gs = new GameSpace(new GameSettings()); 043 gs.getSettings().setFPS(1); 044 b = new Ball(new Vect3(0, 5, 0), Vect3.ZERO, "test ball", gs); 045 gs.add(b); 046 } 047 048 /* (non-Javadoc) 049 * @see junit.framework.TestCase#tearDown() 050 */ 051 protected void tearDown() throws Exception { 052 super.tearDown(); 053 } 054 055 /** test whether willCollideWithBound works in an empty gs **/ 056 public void testWillCollideWithBoundEmpty() { 057 assertEquals(7, gs.getObjects().size()); 058 assertNotNull("b is null!", b); 059 assertNotNull("gs is null!", gs); 060 List<GameObject> lst = GamePhysics.willCollideWithBound(b, gs.getObjects(), 1); //1 second 061 assertEquals(0, lst.size()); 062 } 063 064 /** test a ball that should simply float in space **/ 065 public void testMoveBallFloating() { 066 gs.getSettings().setGravity(Vect3.ZERO); 067 Vect3 x_i = b.getCenter(); 068 Vect3 v_i = b.getVelocity(); 069 GamePhysics.moveBall(b); 070 //with no gravity, no initial velocity, the ball should stay put 071 assertEquals(x_i, b.getCenter()); 072 assertEquals(v_i, b.getVelocity()); 073 } 074 075 /** test a ball that should fall down (y-direction) **/ 076 public void testMoveBallFallingNoDrag() { 077 gs.getSettings().setGravity(Vect3.Y_HAT.neg()); //-1 in y direction 078 gs.getSettings().setMu2(0.0); //no drag 079 080 Vect3 x_i = b.getCenter(); 081 Vect3 v_i = b.getVelocity(); 082 GamePhysics.moveBall(b); 083 //GamePhysics.freeMove(b, 1.0); 084 assertEquals(Vect3.Y_HAT.neg(), b.getVelocity()); 085 assertEquals(new Vect3(0, 4.5, 0), b.getCenter()); 086 087 x_i = b.getCenter(); 088 v_i = b.getVelocity(); 089 GamePhysics.moveBall(b); 090 //GamePhysics.freeMove(b, 1.0); 091 assertEquals(new Vect3(0, 3.0, 0), b.getCenter()); 092 assertEquals(new Vect3(0, -2, 0), b.getVelocity()); 093 } 094 095 /** test whether it detects about-to-collide stuff using bound check **/ 096 public void testCollideWithBoundNotEmpty() { 097 b.setCenter(Vect3.Y_HAT.times(2.5)); // start ball at 2.5 in y direction 098 gs.getSettings().setGravity(Vect3.Y_HAT.neg()); //-1 in y direction 099 gs.getSettings().setMu2(0.0); //no drag 100 SquareBumper bumper = new SquareBumper(new Vect3(0, -0.5, 0), "sq bumper", gs); 101 gs.add(bumper); 102 103 GamePhysics.moveBall(b); 104 assertEquals(b.getCenter(), new Vect3(0, 2.0, 0)); 105 assertEquals(b.getVelocity(), new Vect3(0, -1.0, 0)); 106 //now (0, 2.0, 0), v=-1 107 GamePhysics.moveBall(b); 108 assertEquals(new Vect3(0, 0.5, 0), b.getCenter()); 109 assertEquals(new Vect3(0, -2.0, 0), b.getVelocity()); 110 //now (0, 0.5, 0), with v=2 111 112 assertEquals(8, gs.getObjects().size()); 113 List<GameObject> cands = GamePhysics.willCollideWithBound(b, gs.getObjects(), 1.0); 114 assertEquals(2, cands.size()); //there's also the wall 115 } 116 117 /** test whether the ball collides with the square bumper **/ 118 public void testMoveBallBounce() { 119 b.setCenter(Vect3.Y_HAT.times(2.5)); // start ball at 2.5 in y direction 120 gs.getSettings().setGravity(Vect3.Y_HAT.neg()); //-1 in y direction 121 gs.getSettings().setMu2(0.0); //no drag 122 SquareBumper bumper = new SquareBumper(new Vect3(0, -0.5, 0), "sq bumper", gs); 123 gs.add(bumper); 124 125 GamePhysics.moveBall(b); 126 assertEquals(b.getCenter(), new Vect3(0, 2.0, 0)); 127 assertEquals(b.getVelocity(), new Vect3(0, -1.0, 0)); 128 //now (0, 2.0, 0), v=-1 129 130 GamePhysics.moveBall(b); 131 assertEquals(new Vect3(0, 0.5, 0), b.getCenter()); 132 assertEquals(new Vect3(0, -2.0, 0), b.getVelocity()); 133 //now (0, 0.5, 0), with v=2 134 135 GamePhysics.moveBall(b); 136 assertEquals(b.getVelocity(), new Vect3(0, 1.0, 0)); 137 assertEquals(b.getCenter(), new Vect3(0, 2.0, 0)); 138 //new (0, 2, 0) with v=1 bouncing upward... i think...? 139 } 140 }