This site requires JavaScript, please enable it in your browser!
Greenfoot back
georgette
georgette wrote ...

2016/12/11

Newton's Lab-2

georgette georgette

2016/12/11

#
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;
    }

}
Super_Hippo Super_Hippo

2016/12/11

#
I don't see an 'addForce' method. If you don't have one, then how should it work? I am also unsure how line 71 will ever be true.
danpost danpost

2016/12/11

#
Line 68 seems to be an issue. You are creating a list of Body objects and assigning body objects to it. Since one is not the other (one starts with an uppercase letter and the other with lowercase), I cannot see how it would work at all. @Super_Hippo, I believe that some versions of the SmoothMover class have an 'addForce' method
georgette georgette

2016/12/11

#
Thanks guys! You're right, it'll never work this way. The bulk of this code belongs in my Body class not the Ship class.
You need to login to post a reply.