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

2017/11/15

Displaying Gravity Constant

bannigrin bannigrin

2017/11/15

#
Hello, As stated in the title above, I need to display my gravity constant in the bottom middle section of the screen for a homework assignment. Here's the info my teacher provided: Find and change the GRAVITY constant to be a random number between 6.8 and 8.8. Display the gravity constant near the bottom center of the screen. Hint for the display: use the getWorld().showText() method. Experiment with different values and note the effect on the World. Write the effect different GRAVITY values have as comments under the declaration of the GRAVITY constant. I believe I have figured out how to get the Gravity to be a random number between 6.8 & 8.8, but have yet to figure out how to display the gravity. I'll provide my code below:
public class Body extends SmoothMover
{
    private static final double GRAVITY = (6.8+Greenfoot.getRandomNumber(2));
    //unable to get random number as a decimal, not a whole number
    private static final Color defaultColor = new Color(255, 216, 0);
    private double mass;
    
    /**
     * Construct a Body with default size, mass, velocity and color.
     */
    public Body()
    {
        this (20, 300, new Vector(0, 0.0), defaultColor);
    }
    
    /**
     * Construct a Body with a specified size, mass, velocity and color.
     */
    public Body(int size, double mass, Vector velocity, Color color)
    {
        this.mass = mass;
        addToVelocity(velocity);
        GreenfootImage image = new GreenfootImage (size, size);
        image.setColor (color);
        image.fillOval (0, 0, size-1, size-1);
        setImage (image);
    }
    
    /**
     * Act. That is: apply  the gravitation forces from
     * all other bodies around, and then move.
     */
    public void act() 
    {
        applyForces();
        move();
        bounceAtEdge();
        displayGravity();
    }
    
    /**
     * Check whether we have hit the edge of the universe. If so, bounce off it.
     */
    private void bounceAtEdge()
    {
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            setLocation((double)getX(), (double)getY());
            invertHorizontalVelocity();
            accelerate(0.9);
        }
        else if (getY() == 0 || getY() == getWorld().getHeight()-1) {
            setLocation((double)getX(), (double)getY());
            invertVerticalVelocity();
            accelerate(0.9);
        }
    }
  
    /**
     * Apply the forces of gravity from all other celestial bodies in this universe.
     */
    private void applyForces()
    {
        List<Body> bodies = getWorld().getObjects(Body.class);
        
        for (Body body : bodies) 
        {
            if (body != this) 
            {
                applyGravity(body);
            }
        }
        
        // ensure that we don't get too fast: If the current speed is very fast, decelerate a bit.
        if (getSpeed() > 7) 
        {
            accelerate (0.9);  // acceleration with factor < 1 is actually slowing down.
        }
    }
    
    /**
     * Apply the force of gravity of a given body to this one.
     * (Force is applied for one time unit; dt==1.)
     */
    private void applyGravity(Body other)
    {
        double dx = other.getExactX() - this.getExactX(); //distance of x
        double dy = other.getExactY() - this.getExactY(); //distance of y
        Vector accelVector = new Vector (dx, dy); //sets direction correctly; distance still invalid
        double distance = Math.sqrt (Math.pow(dx,2) + (Math.pow(dy,2))); //sets distance
        
        double force = GRAVITY * this.getMass () * other.getMass () / (Math.pow (distance,2)); //sets force
        
        double acceleration = force/this.getMass(); //sets acceleration
       
        accelVector.setLength (acceleration);
        
        addToVelocity (accelVector);
    }
    
    /**
     * Return the mass of this body.
     */
    public double getMass()
    {
        return mass;
    }
    
    public void displayGravity()
    {
        getWorld().showText(GRAVITY);
    }
}
The error message I receive for getWorld().showText(GRAVITY); is method showText in class greenfoot.World cannot be applied to given types; required:java.lang.String,int,int found: double reason: actual and formal arguments lists differ in length Thanks in advance for the help. -Ryan
Super_Hippo Super_Hippo

2017/11/15

#
You need to write in the x and y coordinates, so not only WHAT you want to display, but also WHERE.
bannigrin bannigrin

2017/11/15

#
@Super_Hippo I added coordinates:
public void displayGravity()
        {
            getWorld().showText(GRAVITY, 600, 200);
        }
now am receiving error message: incompatible types: double cannot be converted to java.lang.String
danpost danpost

2017/11/15

#
So, you added coordinates for the second and third argument in the list. The first argument for 'showText' is required to be a String; but GRAVITY is not a String -- it is a 'double' value. It needs to be converted to a String.. You can use 'Double.toString(GRAVITY)' or just '""+GRAVITY'..
bannigrin bannigrin

2017/11/16

#
@danpost Thank you! That fixed it
You need to login to post a reply.