private ProjectileTracker tracker ;
tracker = new ProjectileTracker( vx, vy ) ; yourSprite.setTracker( tracker ) ;Note: you can obviously use different names than "tracker" and "yourSprite"
tracker.bounce( yourSprite, anotherSprite ) ; // order of the sprites is VERY IMPORTANT tracker.advanceTime( timePassed ) ; // NOTE: timePassed is gotten from advanceFrame yourSprite.translate( tracker.getTranslation( ) ) ; yourSprite.scale( tracker.getScaleFactor( ) ) ; yourSprite.rotate( tracker.getRotationAddition( ) ) ;
// bounce each sprite off the other tracker.bounce( yourSprite, anotherSprite ) ; // order of the sprites is VERY IMPORTANT anotherTracker.bounce( anotherSprite, yourSprite ) ; // order of the sprites is VERY IMPORTANT // update trackers tracker.advanceTime( timePassed ) ; // NOTE: timePassed is gotten from advanceFrame anotherTracker.advanceTime( timePassed ) ; // update one sprite yourSprite.translate( tracker.getTranslation( ) ) ; yourSprite.scale( tracker.getScaleFactor( ) ) ; yourSprite.rotate( tracker.getRotationAddition( ) ) ; // update the other anotherSprite.translate( anotherTracker.getTranslation( ) ) ; anotherSprite.scale( anotherTracker.getScaleFactor( ) ) ; anotherSprite.rotate( anotherTracker.getRotationAddition( ) ) ;
tracker.setVelocity( vx, vy ) ;where vx and vy are again some sort of double variables or literals
That's how to use a tracker, but how do they work? They really are not terribly complicated. Look at the ProjectileTracker.java to download at the top of this page and find the advanceTime method in it. It is really quite simple: update the current translation based on the velocity and the time that has passed and also rotate (if necessary). velocity*timePassed yields a distance moved for the amount of time that has passed, which is something you have learned or will learn in physics. The other important method is getTranslation( ), which returns the current translation of the tracker. So, behind the scenes, when FANG is drawing a new frame, first advanceTime gets called on all trackers so that they update their translations. Next, it calls getTranslation on all of them and moves their associated sprites by that amount. It uses the translation to move a sprite the tracker is associated with the same way we learned to move a sprite by using "sprite.translate( deltaX, deltaY );"
Bouncing is a little more complicated and depends on finding the surface normal of the surface you are bouncing off of. This can be easy (for say a rectangle) or complicated (a circle). Once you know this normal, bouncing is a matter of reflecting the vector of the bouncee's motion around this normal and sending the bouncee off in this reflected vector's direction. Hard to explain without a picture. I'd try searching the internet if you want to know more.