Advertisement
pastezan

Ball_Clean

Jan 16th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. import javafx.scene.shape.Circle;
  4.  
  5. public class Ball {
  6.     //equation of the circle: r^2 = (x-h)^2 + (y-h)^2
  7.     double h = randomSpawnX();
  8.     double k = 0;
  9.     double radius = 15;
  10.     double velocityOld[] = new double[2];
  11.     double posOld[] = new double[2]; //h will be pos[0] and k will be pos[1]
  12.     double posNew[] = new double[2]; //h will be pos[0] and k will be pos[1]
  13.     double accel = 9.81;
  14.    
  15.    
  16.    
  17.     public Ball() {
  18.         velocityOld[0] = 0;
  19.         velocityOld[1] = 0; //so that the ball doesn't fall too fast in the beginning
  20.         posOld[0] = h;
  21.         posOld[1] = k;
  22.     }
  23.    
  24.    
  25.  
  26.    
  27.    
  28.    
  29.    
  30.    
  31.    
  32.    
  33.     //***** C A L C U L A T I O N    M E T H O D S *****//
  34.    
  35.     //get the next X position
  36.     public double getNextCenterX(double deltaT) {
  37.         double answer;
  38.         double Vnew;
  39.         posOld[0] =
  40.        
  41.         Vnew = accel*deltaT+velocityOld[0];
  42.         answer = 1/2*(accel*deltaT*deltaT)+Vnew*deltaT+posOld[0];
  43.        
  44.         posNew[0] = answer;
  45.         return posNew[0];
  46.  
  47.     }
  48.    
  49.     //get the next Y position
  50.     public double getNextCenterY(double deltaT) {
  51.         double answer;
  52.         double Vnew;
  53.         posOld[1] =
  54.        
  55.         Vnew = accel*deltaT+velocityOld[1];
  56.         answer = 1/2*(accel*deltaT*deltaT)+Vnew*deltaT+posOld[1];
  57.        
  58.         posNew[1] = answer;
  59.         return posNew[1];
  60.     }
  61.    
  62.     //make posNew into posOld
  63.     public void updatePos() {
  64.         posOld[0]=posNew[0];
  65.         posOld[1]=posNew[1];
  66.     }
  67.    
  68.    
  69.    
  70.    
  71.    
  72.    
  73.    
  74.    
  75.    
  76.    
  77.    
  78.    
  79.    
  80.    
  81.    
  82.     //generate random numbers
  83.     //algorithm from: https://stackoverflow.com/questions/3680637/generate-a-random-double-in-a-range
  84.     //this method gets a random position for the X center of the circle
  85.     public double randomSpawnX() {
  86.         double start = 0;
  87.         double end = 400;
  88.         double random = new Random().nextDouble();
  89.         double result = start + (random * (end - start));
  90.         return result;
  91.     }
  92.     //this method gets a random velocity for the
  93.     public double randomVelocity() {
  94.         double start = 0;
  95.         double end = 5;
  96.         double random = new Random().nextDouble();
  97.         double result = start + (random * (end - start));
  98.         return result;
  99.     }
  100.    
  101.    
  102.    
  103.    
  104.    
  105.    
  106.    
  107.    
  108.    
  109.    
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement