Ads 468x60px

Thursday, March 8, 2012

Billiards Ball Movement Simulation

Abstract:
In this project we will talk about ball collisions which is happened inside Billiards Squared Table, what happened to ball before and after the collusion, In order to realistic this behavior we will mention some of basics and rules about this collisions, I have put these rules and assumptions in order to realistic this movement. The flow for this project will be as follows, first of all we will take an introduction to what happened to speed after and before the collision, then we will choose 8 movements instead of know the angle of shoot, then we will see the samples for this simulation according to different speeds and positions. Finally we will go throughout simulation code about this problem.

Assumptions & Movement Rules:
In this part of project we should take the following two assumptions related to speed, direction and Block Size.
  1. The table which we use it is a square area, and we will divide it into n * n grid.
  2. We have two assumptions related to speed, if we assume the speed when hit the ball is n m/s, there are two factors affect on speed, the first one is the speed is decreased by 5 m/s when it is runs 20m, another thing is we will lost speed/1.01 of the speed when the ball hits any of the boundaries of the squared area.
  3. The last thing related to direction of ball, to make the movement is easier, we will define 8 directions of the ball instead of determine the angle of movement. If we assume t he current position for the ball is X and Y then we will define the following 8 directions.
    1. movUp: Ball at (X,Y) and moves in direction (X, Y+1) Upward Movement.
    2. movDown : Ball at (X,Y) and moves in direction (X, Y-1) Downward Movement.
    3. movRight : Ball at (X,Y) and moves in direction (X+1, Y) Forward Movement.
    4. movLeft : Ball at (X,Y) and moves in direction (X-1, Y) Backward Movement .
    5. movLeftUp : Ball at (X,Y) and moves in direction (X-1, Y+1) .
    6. movLeftDown : Ball at (X,Y) and moves in direction (X-1, Y-1).
    7. movRightUp : Ball at (X,Y) and moves in direction (X+1, Y+1).
    8. movRightDown : Ball at (X,Y) and moves in direction (X+1, Y-1).
  4. Movements After Collisions
    1. MoveUp is changed to MoveDown
    2. MoveDown is changed to MoveUp
    3. MoveLeft is changed to MoveRight
    4. MoveRight is changed to MoveLeft
    5. MoveRightUp is changed to
      1. point (n,n) the direction will be MoveLeftDown
      2. at the other points the direction will be MoveRightDown
    6. MoveLeftUp is changed to
      1. at point (1,n) the direction will be MoveRightDown
      2. at the other points the direction will be MoveLeftDown
    7. MoveLeftDown is changed to
      1. at point (1,1) the direction will be MoveRightUp
      2. at the other points the direction will be MoveLeftUp
    8. MoveRightDown is changed to
      1. at point (n,1) the direction will be MoveLeftUp
      2. at the other points the direction will be MoveRightUp
 Example:
 The purpose for this part is to know how we apply the rules that we have learn in the movement rules, then assume that.

We assume that we have a squared area with (100 * 100 ), and the ball was located at position (20,20) and we have hit it at speed 50 m/s in right-up direction ?

Each 20m the speed decrease 5 m/s, when ball reaches point (100,100) the speed will decrease by (80/20)*5 = 20, so the speed will be 50-20=30 at point (100,100), after collision the movement will change from right-up to leftdown and the speed will be 30/2=15, so when the ball again reach point(40,40) the speed will be (60/20)*5 = 15-15 = 0 m/s, then the ball stop at point(0,0).

Simulation part:
Before we talked about this part, the source code in appendix is a java code, the code will  take two inputs, the first one is the velocity and the second the direction, as we defined  above we have 8 directions, the output for this program the flow of motion from starting  point the stopping point as points. so I will use excel to generate X-Y chart to display the  motion.

Returned to this part we see different cases for this simulation, the first example we will  test simple example that we will discussed above :
  1. Starting Point (20,20) and Velocity = 50 in right up direction
  2.  Starting Point (20,22) and Velocity = 50 in right up direction
  3. In this case, just we increase the speed, Starting Point (20,22) and Velocity = 100  in right down direction
  4. In this case, just we increase the speed, Starting Point (88,88) and Velocity = 100  in left up direction
Appendix:
  • Java Simulation Code
  • Use Excel to plot the output data from java program
   1:  package ballcollisions;
   2:  /**
   3:   *
   4:   * @author Yaser Aburrub
   5:   */
   6:  public class Main {
   7:      /**
   8:           * @param args the command line arguments
   9:           */
  10:      static int Distance= 0;
  11:      /* Here you can change the starting speed */
  12:      static int speed= 100;
  13:      public static void main(String[] args)
  14:      {
  15:          // Here you select the starting position for the ball
  16:          Position p = new Position(88,88);
  17:          /* We Decide the direction which is 6 and size of box which is (100*100) */
  18:          /* We have 8 direction each of them has number */
  19:          /*
  20:          (1) : Move Up
  21:          (2) : Move Down
  22:          (3) : Move Right
  23:          (4) : Move Left
  24:          (5) : Move Right-Up
  25:          (6) : Move Left-Up
  26:          (7) : Move Right-Down
  27:          (8) : Move Left-Down
  28:          */
  29:          StartSimulation(p, 6,100,100);
  30:      }
  31:   
  32:      /* Main Execution Function */
  33:      /* Parameters: Current Position and Number of Rows & Columns */
  34:      static void StartSimulation(Position pos, int MoveCommand, int NumberOfRows, int
  35:      NumberOfColumns)
  36:      {
  37:          if(speed <= 0)
  38:              MoveCommand = 10;
  39:      /* In Each movement, we call its function, then print the result the prepared for the 
  40:      next movement */
  41:      switch(MoveCommand)
  42:      {
  43:          /* Up Movement */
  44:          case 1:
  45:              MoveCommand = MoveUp(pos, NumberOfRows, NumberOfColumns);
  46:              print(pos);
  47:              StartSimulation(pos,MoveCommand,NumberOfRows, NumberOfColumns);
  48:              break;
  49:          /* Down Movement */
  50:          case 2:
  51:              MoveCommand = MoveDown(pos, NumberOfRows, NumberOfColumns);
  52:              print(pos);
  53:              StartSimulation(pos,MoveCommand,NumberOfRows, NumberOfColumns);
  54:              break;
  55:          /*  Right Movement */
  56:          case 3:
  57:              MoveCommand = MoveRight(pos, NumberOfRows, NumberOfColumns);
  58:              print(pos);
  59:              StartSimulation(pos,MoveCommand,NumberOfRows, NumberOfColumns);
  60:              break;
  61:          /* Left Movement */
  62:          case 4:
  63:              MoveCommand = MoveLeft(pos, NumberOfRows, NumberOfColumns);
  64:              print(pos);
  65:              StartSimulation(pos,MoveCommand,NumberOfRows, NumberOfColumns);
  66:              break;
  67:          /*  Right-Up Movement */
  68:          case 5:
  69:              MoveCommand = MoveRightUp(pos, NumberOfRows, NumberOfColumns);
  70:              print(pos);
  71:              StartSimulation(pos,MoveCommand,NumberOfRows, NumberOfColumns);
  72:              break;
  73:          /* Left-Up Movement */
  74:          case 6:
  75:              MoveCommand = MoveLeftUp(pos, NumberOfRows, NumberOfColumns);
  76:              print(pos);
  77:              StartSimulation(pos,MoveCommand,NumberOfRows, NumberOfColumns);
  78:              break;
  79:          /* Right-Down Movement */
  80:          case 7:
  81:              MoveCommand = MoveRightDown(pos, NumberOfRows, NumberOfColumns);
  82:              print(pos);
  83:              StartSimulation(pos,MoveCommand,NumberOfRows, NumberOfColumns);
  84:              break;
  85:          /* Left-Down Movement */
  86:          case 8:
  87:              MoveCommand = MoveLeftDown(pos, NumberOfRows, NumberOfColumns);
  88:              print(pos);
  89:              StartSimulation(pos,MoveCommand,NumberOfRows, NumberOfColumns);
  90:              break;
  91:          /* when the speed become ZERO, we get MoveCommand 10 which means we reach the end 
  92:          point */
  93:          case 10:
  94:              System.out.println("End Process");
  95:              break;
  96:          }
  97:      }
  98:      
  99:      static int MoveUp(Position pos, int NumberOfRows, int NumberOfColumns)
 100:      {
 101:          int MoveCommand=1;
 102:          /* Change Direction when we reach upper bound */
 103:          if(pos.GetY() == NumberOfRows)
 104:          {
 105:              MoveCommand = 2;
 106:              decrease(1);
 107:              MoveDown(pos, NumberOfRows, NumberOfColumns);
 108:          }
 109:          else
 110:              pos.IncrementY();
 111:          decrease(2);
 112:          return MoveCommand;
 113:      }
 114:      static int MoveDown(Position pos, int NumberOfRows, int NumberOfColumns)
 115:      {
 116:          int MoveCommand=2;
 117:          /* Change Direction when we reach lower bound */
 118:          if(pos.GetY() == 1)
 119:          {
 120:              MoveCommand=1;
 121:              decrease(1);
 122:              MoveUp(pos, NumberOfRows, NumberOfColumns);
 123:          }
 124:          else
 125:              pos.DecrementY();
 126:          decrease(2);
 127:          return MoveCommand;
 128:      }
 129:      static int MoveRight(Position pos, int NumberOfRows, int NumberOfColumns)
 130:      {
 131:          int MoveCommand=3;
 132:          /* Change Direction when we reach right bound */
 133:          if(pos.GetX() == NumberOfColumns)
 134:          {
 135:              MoveCommand=4;
 136:              decrease(1);
 137:              MoveLeft(pos, NumberOfRows, NumberOfColumns);
 138:          }
 139:          else
 140:              pos.IncrementX();
 141:          decrease(2);
 142:          return MoveCommand;
 143:      }
 144:      
 145:      static int MoveLeft(Position pos, int NumberOfRows, int NumberOfColumns)
 146:      {
 147:          int MoveCommand=4;
 148:          /* Change Direction when we reach left bound */
 149:          if(pos.GetX() == 1)
 150:          {
 151:              MoveCommand = 3;
 152:              decrease(1);
 153:              MoveRight(pos, NumberOfRows, NumberOfColumns);
 154:          }
 155:          else
 156:              pos.DecrementX();
 157:          decrease(2);
 158:          return MoveCommand;
 159:      }
 160:      
 161:      static int MoveRightUp(Position pos, int NumberOfRows, int NumberOfColumns)
 162:      {
 163:          int MoveCommand = 5;
 164:          if(pos.GetX() == NumberOfColumns && pos.GetY() == NumberOfRows)
 165:          {
 166:              MoveCommand = 8;
 167:              decrease(1);
 168:              MoveLeftDown(pos, NumberOfRows, NumberOfColumns);
 169:          }
 170:          else if (pos.GetY() == NumberOfRows)
 171:          {
 172:              MoveCommand = 7;
 173:              decrease(1);
 174:              MoveRightDown(pos, NumberOfRows, NumberOfColumns);
 175:          }
 176:          else if (pos.GetX() == NumberOfColumns)
 177:          {
 178:              MoveCommand = 6;
 179:              decrease(1);
 180:              MoveLeftUp(pos, NumberOfRows, NumberOfColumns);
 181:          }
 182:          else
 183:          {
 184:              pos.IncrementX();
 185:              pos.IncrementY();
 186:          }
 187:          decrease(2);
 188:          return MoveCommand;
 189:      }
 190:      static int MoveLeftUp(Position pos, int NumberOfRows, int NumberOfColumns)
 191:      {
 192:          int MoveCommand = 6;
 193:          if(pos.GetX() == 1 && pos.GetY() == NumberOfRows)
 194:          {
 195:              MoveCommand = 7;
 196:              decrease(1);
 197:              MoveRightDown(pos, NumberOfRows, NumberOfColumns);
 198:          }
 199:          else if (pos.GetX() == 1)
 200:          {
 201:              MoveCommand = 5;
 202:              decrease(1);
 203:              MoveRightUp(pos, NumberOfRows, NumberOfColumns);
 204:          }
 205:          else if (pos.GetY() == NumberOfRows)
 206:          {
 207:              MoveCommand = 8;
 208:              decrease(1);
 209:              MoveLeftDown(pos, NumberOfRows, NumberOfColumns);
 210:          }
 211:          else
 212:          {
 213:              pos.DecrementX();
 214:              pos.IncrementY();
 215:          }
 216:          decrease(2);
 217:          return MoveCommand;
 218:      }
 219:      
 220:      static int MoveRightDown(Position pos, int NumberOfRows, int NumberOfColumns)
 221:      {
 222:          int MoveCommand = 7;
 223:          if(pos.GetX() == NumberOfColumns && pos.GetY() == 1)
 224:          {
 225:              MoveCommand = 6;
 226:              decrease(1);
 227:              MoveLeftUp(pos, NumberOfRows, NumberOfColumns);
 228:          }
 229:          else if (pos.GetX() == NumberOfColumns)
 230:          {
 231:              MoveCommand = 8;
 232:              decrease(1);
 233:              MoveLeftDown(pos, NumberOfRows, NumberOfColumns);
 234:          }
 235:          else if (pos.GetY() == 1)
 236:          {
 237:              MoveCommand = 5;
 238:              decrease(1);
 239:              MoveRightUp(pos, NumberOfRows, NumberOfColumns);
 240:          }
 241:          else
 242:          {
 243:              pos.IncrementX();
 244:              pos.DecrementY();
 245:          }
 246:          decrease(2);
 247:          return MoveCommand;
 248:      }
 249:      static int MoveLeftDown(Position pos, int NumberOfRows, int NumberOfColumns)
 250:      {
 251:          int MoveCommand = 8;
 252:          if(pos.GetX() == 1 && pos.GetY() == 1)
 253:          {
 254:              MoveCommand = 5;
 255:              decrease(1);
 256:              MoveRightUp(pos, NumberOfRows, NumberOfColumns);
 257:          }
 258:          else if(pos.GetX() == 1)
 259:          {
 260:              MoveCommand = 7;
 261:              decrease(1);
 262:              MoveRightDown(pos, NumberOfRows, NumberOfColumns);
 263:          }
 264:          else if(pos.GetY() == 1)
 265:          {
 266:              MoveCommand = 6;
 267:              decrease(1);
 268:              MoveLeftUp(pos, NumberOfRows, NumberOfColumns);
 269:          }
 270:          else
 271:          {
 272:              pos.DecrementX();
 273:              pos.DecrementY();
 274:          }
 275:          decrease(2);
 276:          return MoveCommand;
 277:      }
 278:      static void print(Position p)
 279:      {
 280:      //    System.out.println("x("+p.x+") y("+p.y+") "+"Distance("+Distance+") 
 281:          speed("+speed+")");
 282:          System.out.println(p.GetX()+" "+p.GetY());
 283:      }
 284:      
 285:      static void decrease(int type)
 286:      {
 287:          if (type == 1) // decrease from ball running
 288:              if(speed/2 == 0)
 289:                  speed = 0;
 290:              else
 291:                  speed = speed - (speed/2);
 292:          else // decrease from collision
 293:          {
 294:              Distance = Distance +1;
 295:              if((Distance%20) == 0)
 296:              speed = speed-5;
 297:          }
 298:      }
 299:  }
 300:   
 301:  class Position
 302:  {
 303:      private int x;
 304:      private int y;
 305:      public Position(int x, int y)
 306:      {
 307:          this.x = x;
 308:          this.y = y;
 309:      }
 310:      public void IncrementX()
 311:      {
 312:          this.x ++;
 313:      }
 314:      public void IncrementY()
 315:      {
 316:          this.y ++;
 317:      }
 318:      public void DecrementX()
 319:      {
 320:          this.x --;
 321:      }
 322:      public void DecrementY()
 323:      {
 324:          this.y --;
 325:      }
 326:      public int GetX()
 327:      {
 328:          return this.x;
 329:      }
 330:      public int GetY()
 331:      {
 332:          return this.y;
 333:      }
 334:  }

No comments:

Post a Comment