Reputation: 21
I've got a piece of programming coursework to complete in Java.
It's essentially a pinball machine but with no user control, i.e. it isn't a game. The complete game will have 3 of the two pinballs I've created (that's 3 copies of pinball9 and 3 copies of pinball8).
The holes (which are black) are stationary and when a pinball moves over a hole, it's score is reset to 0. If the pinball is the same size as a hole, it shall be removed from the simulation.
The gray balls are bumpers. Each pinball bounces off a bumper and the score of the pinball is increased by 2.
The Machine class essentially controls the game itself, and it is where I create my objects (within the createPinballs() method). The PinballObject class (which inherits from the BallObject) controls the functionality of each pinball. This class contains a move() method which checks if a pinball hits a wall and redraws it everytime it moves. The Pinball9 and Pinball8 classes are the two types of pinballs I'm required to implement, both of which extend the PinballObject class. These classes contain a move() method which just calls the move() method in the PinballObject and also checkCollisionWithPinballs() method which takes three ArrayLists. One for the other pinball, for example the Pinball9's checkCollisionWithPinballs() passes an ArrayList of Pinball8 objects (there are three of each pinball type). They also pass an ArrayList of holes and bumpers. The first for loop checks to see if the current pinball, i.e. if in the class Pinball9 it checks to see if it has collided with any of the Pinball8 objects and vice-versa.
The pinballs do collide, however, they bounce off in the same direction they collided at, however, I need to make it reflect as if it were a realistic collision with a ball.
Currently, this is how I am handling the collision checking:
public void checkCollisionWithPinballs(ArrayList<Pinball8> pinballArray, ArrayList<Hole> holeArray, ArrayList<Bumper> bumperArray) {
for (Pinball8 p : pinballArray) {
int x0 = this.currentXLocation;
int x1 = p.getXPosition();
int y0 = this.currentYLocation;
int y1 = p.getYPosition();
double diff = Math.sqrt((Math.pow(x0 - x1, 2)) + (Math.pow(y0 - y1, 2)));
int dx = x0 - x1;
int dy = y0 - y1;
diff = Math.sqrt(dx * dx + dy * dy);
if (diff <= ((this.getRadius() + p.getRadius()) + 2)) {
// pinball has collided with pinball 8
speedYTravel = -speedYTravel;
speedXTravel = -speedXTravel;
}
}
// CODE FOR HOLES AND BUMPERS CONTINUES BELOW...
}
This is a snippet from the Pinball9 class, the code is exactly the same in the Pinball8 class it just loops through the pinball9 objects.
This code works but the pinballs bounce off at the direction they collided in, I need this to be reflected however I'm not too sure how to go about it.
Below is a screenshot of the working program:
Upvotes: 2
Views: 328
Reputation: 6159
Well you need some vector calculations for that. If you want to have elastic collisions, try representing everything with vectors. Let's say your ball A has a velocity and a direction (that's a vector) and your ball B has another "module" and direction, when they collide you'll want to re-calculate a new module (velocity) based on both balls modules, and a new direction which comes from the addition of both vectors normalized. If you want to have collisions with irregular surfaces, then you should calculate the normal vector of each part of the surface and do the same calculation as if that surface was another ball, using its normal vector.
Upvotes: 1