this is where im currently at
private void move () {
if (x + angleX < 0) {
angleX = vx;
} else if ( + angleX > getWidth() - 50) {
angleX = -vx;
} else if (y + angleY < 0) {
angleY = vy;
} else if (y + angleY > getHeight() - 50) {
angleY = -vy;
}
x = x + angleX;
y = y + angleY;
}
I would like to implement gravity with this formula
x = x + t*vx;
y = y + tvy - G(t*t/2.0);
where gravity is 10.
Currently it just moves the ball up and down.
Any help is appreciated.
Whole source:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Color;
public class App extends JPanel {
int x = 165, y = 0;
int angleX = 1, angleY = 1;
int G = 10;
int vx = 5;
int vy = 1;
private void move () {
if (x + angleX < 0) {
angleX = vx;
} else if ( + angleX > getWidth() - 50) {
angleX = -vx;
} else if (y + angleY < 0) {
angleY = vy;
} else if (y + angleY > getHeight() - 50) {
angleY = -vy;
}
x = x + angleX;
y = y + angleY;
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.fillOval(x, y, 50, 50); // 50x50 boll
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Studsande boll");
App app = new App();
frame.add(app);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
app.move();
app.repaint();
Thread.sleep(10);
}
}
}