Java Reference
In-Depth Information
body.adjustVelocity(new Vector2f(Math.cos(theta)*startingSpeed,
insert body into bodies;
public override function update():Void{
if (stepsLeft <= 0){
var ratio = stepsLeft/totalSteps;
if (ratio < 0){
ratio = 0;
opacity = fadeInterpolator.interpolate(0.0, 1.0, ratio) as Number;
translateX = bodies[0].getPosition().getX();
translateY = bodies[0].getPosition().getY();
rotate = Math.toDegrees(bodies[0].getRotation());
Listing 10-4 shows the class PhysicsParticle , which extends Group , WorldNode , and Particle . The
init function of PhysicsParticle adds an ImageView to the content . A Body is also created with the same
location and rotation as the PhysicsParticle , and it's given a velocity based on the startingDirection
and startingSpeed .
The update function is called after each step of the world object, and the location and rotation of the
PhysicsParticle is adjusted to match the Body . The lifecycle of the PhysicsParticle is also handled by
the function update . The value of stepsLeft is reduced by one, and if it reduces to zero, the entity is
removed from the scene and the world.
If fadeInterpolator is set, the opacity of the PhysicsParticle is adjusted. The actual opacity is
calculated by figuring out what ratio of the PhysicsParticle's steps have expired. This ratio is then fed
into the fadeInterpolator to get the final result. The spark particles in this example use a spline
interpolator with its value set so that most of the fading is put off until the end of the lifecycle.
Emitters as Bodies
The next example uses only a few bodies, but each body specifies the location of a more traditional
particle emitter. As the emitters move about the screen, the particles they create basically stay in place.
The advantage of this technique is that the number of bodies in the simulation is greatly reduced, so
there are performance gains.
Figure 10-3 shows a number of fireballs bouncing down a field of pegs. As the fireball moves, a
particle is created that drifts slightly upward, giving each fireball a trail effect—something required by
Search WWH ::

Custom Search