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

2018/11/6

what the heck am I doing wrong?? (netwon project)

Toodumbtocode Toodumbtocode

2018/11/6

#
Ok I finished the code but the thing is still not rotating. i have compared my code to other people's and I did everything that the book said I should do and it's still not rotating!! please help!! It says no syntax errors for me by the way.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

import java.util.*;
/**
 * A 'Body' is any kind of object in space that has a mass. It could be a star, or a planet, 
 * or anything else that floats around in space.
 * 
 * @author Michael Kolling 
 * @version 0.1
 */
public class Body extends SmoothMover
{
    // constants
    private static final double GRAVITY = 5.8;
    private static final Color defaultColor = new Color(255, 216, 0);
    
    // fields
    private double mass;
    
    
    /**
     * Construct a Body with default size, mass, movement and color.
     */
    public Body()
    {
        this (20, 300, new Vector(0, 1.0), defaultColor);
    }
    
    /**
     * Construct a Body with a specified size, mass, movement and color.
     */
    public Body(int size, double mass, Vector movement, Color color)
    {
        mass = mass;
        addForce(movement);
        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();
        
        // To be done - not yet implemented
    }
    
    /**
     * Return the mass of this body.
     */
    public double getMass()
    {
        return mass;
    }
     private void applyForces()
     {
         
         List<Body> bodies = getWorld().getObjects(Body.class);
         for (Body body : bodies)
          {
         if (body != this)
          {
           applyGravity (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);
    }
}
Super_Hippo Super_Hippo

2018/11/6

#
Change line 34 to:
this.mass = mass;
Otherwise, you will set the variable mass which is passed to the method to itself, the variable you create in line 18 stays untouched (0) and a body with no mass has no influence in the applyGravity method. However, in line 83, you divide by this mass which should be 0 at this point. I think this should result in an error even though the dividend is 0, too.
Toodumbtocode Toodumbtocode

2018/11/7

#
oh my goodness, thank you so much. I just felt a wave of relief come over me. Can't believe I forgot to add that!
You need to login to post a reply.