I am trying to create ball collisions using two ball sprites. When one ball collides its speed should depend on the other balls initial speed, so in the sprites I need to call in the speeds of the other ball, but I can't figure out how to get past the non-static variable message. This is the line with the error
xa = BIGBALLspeedx()*(BIGBALLmass()-1)+(BallTest.ball.BALLspeedx()*2*1);
Here is my code for one of the sprites and the main package 'BallTest':
package balltest;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import static java.lang.Math.sqrt;
public class BigBall {
private final int DIAMETER = 80;
int x = (int )(Math.random() * 200 + 15 );
int y = (int )(Math.random() * 200 + 15 );
int xa = (int )(Math.random() * 4 );
int ya = (int )(1 );
private BallTest game;
public BigBall(BallTest game) {
this.game= game;
}
public void move() {
if (x + xa < 0)
xa = 1;
if (x + xa > game.getWidth() - DIAMETER)
xa = -1;
if (y + ya < 0)
ya = 1;
if (y + ya > game.getHeight() - DIAMETER)
ya=-1;
if (y + ya < 1)
ya=1;
if (50>sqrt(xDIFF()*xDIFF()+yDIFF()*yDIFF()) && -50<sqrt(xDIFF()*xDIFF()+yDIFF()*yDIFF())) {
xa = BIGBALLspeedx()*(BIGBALLmass()-1)+(BallTest.ball.BALLspeedx()*2*1);
}
x = x + xa;
y = y + ya;
}
public int BIGBALLx() {
return x+40;
}
public int BIGBALLy(){
return y+40;
}
public int BIGBALLspeedx(){
return xa;
}
public int BIGBALLspeedy(){
return ya;
}
public int BIGBALLmass(){
return 5;
}
public int xDIFF(){
return game.ball.BALLx()-game.BigBall.BIGBALLx();
}
public int yDIFF(){
return game.ball.BALLy()-game.BigBall.BIGBALLy();
}
public void paint2(Graphics2D g) {
g.fillOval(x, y, DIAMETER, DIAMETER);
}
public Rectangle getBounds() {
return new Rectangle(x, y, DIAMETER, DIAMETER);
}
}
Here's BallTest
code:
package balltest;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import static java.awt.image.ImageObserver.ABORT;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
*
* @author F27834
*/
public class BallTest extends JPanel {
Ball ball = new Ball(this);
BigBall BigBall = new BigBall(this);
private void move() {
ball.move();
BigBall.move();
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d);
g.setColor(Color.BLUE);
Graphics2D g3d = (Graphics2D) g;
BigBall.paint2(g3d);
}
public void gameOver() {
JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.YES_NO_OPTION);
System.exit(ABORT);
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Mini Tennis");
BallTest game = new BallTest();
frame.add(game);
frame.setSize(300, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
game.move();
game.repaint();
Thread.sleep(10);
}
}
}