Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import javafx.scene.shape.Circle;
- public class Ball {
- //equation of the circle: r^2 = (x-h)^2 + (y-h)^2
- double h = randomSpawnX();
- double k = 0;
- double radius = 15;
- double velocityOld[] = new double[2];
- double posOld[] = new double[2]; //h will be pos[0] and k will be pos[1]
- double posNew[] = new double[2]; //h will be pos[0] and k will be pos[1]
- double accel = 9.81;
- public Ball() {
- velocityOld[0] = 0;
- velocityOld[1] = 0; //so that the ball doesn't fall too fast in the beginning
- posOld[0] = h;
- posOld[1] = k;
- }
- //***** C A L C U L A T I O N M E T H O D S *****//
- //get the next X position
- public double getNextCenterX(double deltaT) {
- double answer;
- double Vnew;
- posOld[0] =
- Vnew = accel*deltaT+velocityOld[0];
- answer = 1/2*(accel*deltaT*deltaT)+Vnew*deltaT+posOld[0];
- posNew[0] = answer;
- return posNew[0];
- }
- //get the next Y position
- public double getNextCenterY(double deltaT) {
- double answer;
- double Vnew;
- posOld[1] =
- Vnew = accel*deltaT+velocityOld[1];
- answer = 1/2*(accel*deltaT*deltaT)+Vnew*deltaT+posOld[1];
- posNew[1] = answer;
- return posNew[1];
- }
- //make posNew into posOld
- public void updatePos() {
- posOld[0]=posNew[0];
- posOld[1]=posNew[1];
- }
- //generate random numbers
- //algorithm from: https://stackoverflow.com/questions/3680637/generate-a-random-double-in-a-range
- //this method gets a random position for the X center of the circle
- public double randomSpawnX() {
- double start = 0;
- double end = 400;
- double random = new Random().nextDouble();
- double result = start + (random * (end - start));
- return result;
- }
- //this method gets a random velocity for the
- public double randomVelocity() {
- double start = 0;
- double end = 5;
- double random = new Random().nextDouble();
- double result = start + (random * (end - start));
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement