Hello.. my code will not compile. The issues seem to be with addForce(movement) and !=this, as far as I can tell they are coded within their respective methods. Please help me figure out what I'm doing wrong. Thank you!
public class Ship extends SmoothMover
{
private GreenfootImage Ship;
private static final double GRAVITY = 7.5;
private static final Color defaultColor = new Color(255, 216, 0);
private double shipSpeed;
private double mass;
private int size;
/**
* Act - do whatever the Ship wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
applyForces();
move();
shipSpeed += GRAVITY; //add constant acceleration due to gravity.
setLocation(getX(), getY()+shipSpeed);
//applyGravity();
}
/**
* Construct a ship with default size, mass, velocity and color.
*/
public Ship()
{
//Vector initial = new Vector(Greenfoot.getRandomNumber(360), .6);
//setRotation(100);
this (20, 300, new Vector(0, 0.0), defaultColor);
}
/**
* Add new ship to the center of world
*/
public void shipScenario()
{
World myWorld = getWorld();
Ship ship = new Ship();
myWorld.addObject(ship, 40, 30);
}
/**
* Construct a ship with specifified size, mass, velocity and color.
*/
public Ship(int size, double mass, Vector movement, Color color)
{
this.mass = mass;
this.size = size;
addForce(movement);
//addToVelocity(velocity);
GreenfootImage image = new GreenfootImage (size, size);
image.setColor (color);
image.fillOval (0,0, size-1, size-1);
setImage(image);
}
/**
* Apply the forces of gravity.
*/
private void applyForces()
{
List<Body> bodies = getWorld().getObjects(body.class);
for (Body body : bodies)
{
if (body != this)
{
applyGravity (body);
}
}
}
/**
* Apply the gravity force of a given body.
*
*/
private void applyGravity(Body other)
{
double dx = other.getExactX() - this.getExactX();
double dy = other.getExactY() - this.getExactY();
Vector force = new Vector (dx, dy);
double distance = Math.sqrt (dx*dx + dy*dy);
double strength = GRAVITY * this.mass * other.mass / (distance * distance);
double acceleration = strength / this.mass;
force.setLength (acceleration);
addForce(force);
}
/**
* Return mass of body.
*/
public double getMass()
{
return mass;
}
}
