Class SpriteAsteroid

java.lang.Object
  |
  +--Sprite
        |
        +--SpriteAsteroid

class SpriteAsteroid
extends Sprite

Our jagged asteroid sprites, they come in two sizes.


Field Summary
(package private) static double ageDoneStormpause
          The number of updates to pause between waves of asteroids.
(package private) static double agegame
          The age of the whole flock of asteroids
(package private)  boolean asteroidIsSmall
          Flag used to distinguish between big and small asteroids.
(package private) static int asteroidsLeft
          Number of asteroids remaining.
(package private) static int BIG_POINTS
          Points for shooting a big asteroid.
(package private)  boolean darting
          Use this to allow temporary speedup when running away from a bullet.
(package private) static int FRAG_COUNT
          Number of fragments to split an asteroid into.
(package private) static int MAX_SIDES
          Max number of asteroid sides.
(package private) static int MIN_SIDES
          Min number of asteroid sides.
(package private) static int SMALL_POINTS
          Points for shooting a small asteroid.
 
Fields inherited from class Sprite
acceleration, active, age, ageDone, angle, angularAcceleration, bounce, edgecolor, fillcolor, fillflag, game, mass, maxspeed, ownerApp, position, positiontransform, radius, shape, shapetransform, soundplaying, velocity
 
Constructor Summary
SpriteAsteroid(RealPixelConverter rtopc)
           
 
Method Summary
 boolean advance(double dt)
          Give the asteroids an ability to speed up for darting.
 void bounceOff(SpriteAsteroid asteroid)
          The idea in this algorithm is that the caller asteroid C and the argument asteroid A will do some exchange of their velocities.
 void collideAsteroids()
          Check if you're colliding with any other asteroids, and if you are, update your velocity and position accordingly.
 void reset(RealPixelConverter rtopc)
          Creates a random jagged shape and movement.
 void split(int fragcount, RealPixelConverter rtopc)
          Create one or more smaller asteroids from a larger one using inactive asteroids.
 void update(RealPixelConverter rtopc)
          If hit by bullet, kill asteroid and advance score.
 
Methods inherited from class Sprite
draw, explode, fixRadius, isColliding, isTouching, loopsound, mass, radius, randomizeFillcolor, render, setFakeRadius, stop, wrapandbounce
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

MIN_SIDES

static final int MIN_SIDES
Min number of asteroid sides. 9.

MAX_SIDES

static final int MAX_SIDES
Max number of asteroid sides. 13.

BIG_POINTS

static final int BIG_POINTS
Points for shooting a big asteroid. 25.

SMALL_POINTS

static final int SMALL_POINTS
Points for shooting a small asteroid. 50.

FRAG_COUNT

static final int FRAG_COUNT
Number of fragments to split an asteroid into.

asteroidIsSmall

boolean asteroidIsSmall
Flag used to distinguish between big and small asteroids.

asteroidsLeft

static int asteroidsLeft
Number of asteroids remaining.

ageDoneStormpause

static double ageDoneStormpause
The number of updates to pause between waves of asteroids.

agegame

static double agegame
The age of the whole flock of asteroids

darting

boolean darting
Use this to allow temporary speedup when running away from a bullet.
Constructor Detail

SpriteAsteroid

public SpriteAsteroid(RealPixelConverter rtopc)
Method Detail

reset

public void reset(RealPixelConverter rtopc)
Creates a random jagged shape and movement. Places asteroid at an edge of the screen. Randomizes the fillcolor.
Overrides:
reset in class Sprite

split

public void split(int fragcount,
                  RealPixelConverter rtopc)
Create one or more smaller asteroids from a larger one using inactive asteroids. The new asteroids will be placed in the same position as the old one but will have a new, smaller shape and new, randomly generated movements.

advance

public boolean advance(double dt)
Give the asteroids an ability to speed up for darting.
Overrides:
advance in class Sprite

bounceOff

public void bounceOff(SpriteAsteroid asteroid)
The idea in this algorithm is that the caller asteroid C and the argument asteroid A will do some exchange of their velocities. We'll think of them as pointlike, so that all that matters it the components of their velocities which lie along the line connecting their two positions. We calculate these two vectors as C's give and A's receive vector. The way this works is that (a) the momentum is conserved and (b) the energy is conserved. We get (a) Ma*newVa + Mb*newVb == Ma*Va + Mb*Vb, and (b) (1/2)*((Ma*newVa^2 + Mb*newVb^2)) == (1/2)*(Ma*Va^2 + Mb*Vb^2). This is the intersection of a line and an ellipse, which is two points. I ran this through Mathematica and came up with the trivial solution newVa = Va and newVb = Vb and the one we'll use: newVa = (Ma*Va - Mb*Va + 2*Mb*Vb) / (Ma + Mb) new Vb =( 2*Ma*Va - Ma*Vb + Mb*Vb)/ (Ma + Mb). If I divide both numerators and denominators by Ma, and call Mb/Ma "massratio" I get newVa = (Va - massratio*Va + 2*massratio*Vb) / (1 + massratio) newVb =( 2*Va - Vb + massratio*Vb)/ (1 + massratio). Simplifying a little more, we get newVa = ((1-massratio)*Va + 2*massratio*Vb) / (1 + massratio) newVb =( 2*Va + (massratio - 1)*Vb)/ (1 + massratio). Note that if massratio is 1, then this is simply newVa = Vb and newVb = Va. If a has a huge (infinite) mass compared to b, then massratio is about 0 and we get newVa = Va, newVb = 2Va - Vb, so if a is motionless, this is a simple bounce. We want contact point to be on the line between the two centers. Rather than making it the midpoint, we weight it so that it divides this line in the same ratio as radius and pother->radius(). That is, we need to pick a sublength "contactdistance" of the "distance" length between the two centers so that radius/radius+otheradius = contactdistance/distance. Multiply both sides of this equation to solve for contactdistance.

collideAsteroids

public void collideAsteroids()
Check if you're colliding with any other asteroids, and if you are, update your velocity and position accordingly. Do the symmetric update to the other guy's veloicty and postion at the same time. Always just check against the guys with index greater than you, that way we only handle each pair once. This all takes time, and we can toggle it off with the AsteroidsGame collideflag.

update

public void update(RealPixelConverter rtopc)
If hit by bullet, kill asteroid and advance score. If asteroid is large, make some smaller ones to replace it. We'll avoid the bullets here by setting acceleration.
Overrides:
update in class Sprite