I'm trying to create faithful space acceleration, but with int values it gets ridiculous. I want to set the location of my object using doubles so that it doesn't zoom off forever.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | double xVelocity = 0 , yVelocity = 0 ; double xAcceleration = 0 , yAcceleration = 0 ; double thrust = 0.01 ; public Starship( int rotation) { setRotation(rotation); } public void act() { checkKeys(); moveStars(); checkHit(); weaponFireDelay++; xVelocity = xVelocity + xAcceleration; yVelocity = yVelocity + yAcceleration; this .setLocation(getX() + ( int ) xVelocity, getY() + ( int ) yVelocity); } |
1 2 3 4 5 | if (Greenfoot.isKeyDown( "up" ) || Greenfoot.isKeyDown( "w" )) { xAcceleration = (( 90 - this .getRotation())/ 90 ) * thrust; yAcceleration = ( this .getRotation()/ 90 ) * thrust; } |
1 | this .setLocation(getX() + ( int ) xVelocity, getY() + ( int ) yVelocity); |