use of math.Vector2d in project SimpleAsteroids by ljialin.
the class MonkeyBall method draw.
@Override
public void draw(Graphics2D g) {
g.fillOval((int) s.x - radius / 2, (int) s.y - radius / 2, radius, radius);
// use a temp reference in case a different thread sets the anchor to null
Vector2d temp = anchor;
g.setStroke(rope);
if (temp != null) {
g.setColor(ropeColor);
g.drawLine((int) s.x, (int) s.y, (int) temp.x, (int) temp.y);
}
}
use of math.Vector2d in project SimpleAsteroids by ljialin.
the class TestMonkeyBall method main.
public static void main(String[] args) {
// make a monkey ball
MonkeyBall monkeyBall = new MonkeyBall(new Vector2d(0, 0), new Vector2d(0, 0));
// see how the position varies without an anchor
// it should fall (i.e. increasing y) with the square of the number of iterations
int nTicks = 10;
for (int i = 0; i < nTicks; i++) {
monkeyBall.update();
System.out.println(i + "\t " + monkeyBall.s);
}
// now put an anchor in
monkeyBall.setAnchor(10, 0);
System.out.println("Anchor at: " + monkeyBall.anchor);
for (int i = 0; i < nTicks; i++) {
monkeyBall.update();
System.out.println(i + "\t " + monkeyBall.s);
}
}
use of math.Vector2d in project SimpleAsteroids by ljialin.
the class ForwardModel method asteroidDeath.
public void asteroidDeath(Asteroid a) {
// if we still have smaller ones to
// work through then do so
// otherwise do nothing
// score += asteroidScore;
a.dead = true;
if (a.index < gameState.params.radii.length - 1) {
// add some new ones at this position
for (int i = 0; i < nSplits; i++) {
Vector2d v1 = a.v.copy().add(rand.nextGaussian(), rand.nextGaussian());
double r = gameState.params.radii[a.index + 1];
Asteroid splitRock = new Asteroid(a.s.copy(), v1, a.index + 1, r);
splitRock.dead = false;
asteroids.add(splitRock);
}
}
incScore(asteroidScore[a.index]);
}
use of math.Vector2d in project SimpleAsteroids by ljialin.
the class ForwardModel method makeAsteroids.
public void makeAsteroids(int nAsteroids) {
// System.out.println("Making nAsteroids: " + nAsteroids);
// asteroids = new ArrayList<>();
Vector2d centre = new Vector2d(width / 2, height / 2);
while (nAsteroids() < nAsteroids) {
// choose a random position and velocity
Vector2d s = new Vector2d(rand.nextDouble() * width, rand.nextDouble() * height);
Vector2d v = new Vector2d(rand.nextGaussian(), rand.nextGaussian());
if (s.dist(centre) > safeRadius && v.mag() > 0.5) {
// Asteroid a = new Asteroid(this, s, v, 0);
// these move in interesting ways ...
// Asteroid a = new LissajousAsteroid(this, s, v, 0);
v.mul(rockSpeed);
double r = gameState.params.radii[0];
Asteroid a = new Asteroid(s, v, 0, r);
this.addAsteroid(a);
}
}
}
use of math.Vector2d in project SimpleAsteroids by ljialin.
the class Ship method tryMissileLaunch.
private void tryMissileLaunch() {
// System.out.println("Release velocity: " + releaseVelocity);
if (releaseVelocity > maxRelease) {
// System.out.println("Missile fired!");
releaseVelocity = Math.max(releaseVelocity, game.params.missileMinVelocity * 2);
Missile m = new Missile(s, new Vector2d(0, 0), game.params.missileTTL, game.params.missileRadius);
releaseVelocity = Math.min(releaseVelocity, maxRelease);
m.v.add(d, releaseVelocity);
// make it clear the ship
// m.s.add(m.v, (r() + game.params.missileRadius) * 0.5 / m.v.mag());
releaseVelocity = 0;
// got it! - it adds it to the old version of the gameState
pending = m;
// used to add the missile to the gameState here, but caused problems in
// the way the reference was being copied, hence removed it
// gameState.add(m);
// System.out.println("Fired: " + m);
// sounds.fire();
} else {
// System.out.println("Failed!");
}
}
Aggregations