fysiktest
The snippet can be accessed without any authentication.
Authored by
Oskar Savinainen
Edited
Particle.java 1.40 KiB
package fysiktest;
import java.awt.Graphics2D;
import java.awt.Color;
public class Particle {
private double vx;
private double vy;
private double r;
private Color color;
private double friction;
private double gravity;
private double x;
public double getX() {
return x;
}
private double y;
public double getY() {
return y;
}
public Particle(double x, double y, double r, Color color) {
this.x = x;
this.y = y;
this.r = r;
this.color = color;
this.vx = -3;
this.vy = 45;
this.friction = 0.75;
this.gravity = 9.81/10;
}
public void update() {
x += vx;
y += vy;
vy += gravity;
checkCollisions();
}
public void checkCollisions() {
if (x - r < 0) {
x = r;
vx = -(vx * friction);
} else if (y - r < 0) {
y = r;
vy = -(vy * friction);
} else if (x + r > 800) {
x = 800 - r;
vx = -(vx * friction);
//bollen är på marken
} else if (y + r > 800) {
double oldY = y;
y = 800 - r;
vy = -(vy * friction);
vx = 0.99 * vx;
if(Math.abs(vx) < 0.1) {
vx = 0;
//sluta rulla om hasigheten är för låg
}
if (Math.abs(y - oldY) < 1) {
vy = 0;
//Om skillnaden i y-led blir liten så ska bollen sluta röra sig i y-led
}
}
}
public void render(Graphics2D g) {
g.setColor(color);
g.fillOval((int) Math.round(x - r), (int) Math.round(y - r), (int) Math.round(2 * r), (int) Math.round(2 * r));
}
}
Please register or sign in to comment