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

2015/10/28

Need help with planets moving in solar system

DanielChaffey DanielChaffey

2015/10/28

#
I have created a program that shows the planets going around the sun. However, at the moment all the planets move together at the same speed and do not move interdependently. Could any one tell me what needs to change in my code? This is the code for my World:
import greenfoot.*;
import greenfoot.World;
import greenfoot.Actor;
import java.awt.Color;
import java.util.List;

public class Space extends World
{
    public static int sunX = 868;
    public static int sunY = 500;
        
    /**
     * Constructor for objects of class Space.
     * 
     */
    public Space()
    {
        
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(2000, 2000, 1);
        Orb.setColor(255, 255, 0);
        Orb.radius = 100;
        addObject(new Sun(), 868, 500);

        /* Planet data
         *  Name     Distance   StartX    StartY  Speed
         *  Earth    300        1168      508      1
         *  Mars     400        1328      508      2
         */

        System.out.println("Creating Earth...");
        Planet Earth = new Planet("Earth", 300, 1168, 508, 1, "blue-draught.png");
        addObject(Earth, 1168, 508);
        
        System.out.println("Creating Mars...");
        Planet Mars = new Planet("Mars", 460, 1328, 508, 1, "red-draught.png");
        addObject(Mars, 1328, 508);
        
        System.out.println("Creating Merkur...");
        Planet Merkur = new Planet("Merkur", 120, 971, 562, 1, "beeper.png");
        addObject(Merkur, 971, 562);
        
        System.out.println("Creating Venus...");
        Planet Venus = new Planet("Venus", 220, 988, 508, 1, "yellow-draught.png");
        addObject(Venus, 1088, 508);
        
        System.out.println("Creating Jupiter...");
        Planet Jupiter = new Planet("Jupiter", 732, 988, 508, 1, "button-red.png");
        addObject(Jupiter, 1600, 508);
        
        //addObject(new Planet(),2428, 508);
        //addObject(new Planet(),3728, 508);
    }
}


//This is the code for my Planet class
import greenfoot.*;
import java.lang.Math.*;
public class Planet extends Actor
{
    String name;
    int distance;
    int x;
    int y;
    int velocity;
    int rotation = 1;
    String image;

    public Planet(String PlanetName, int radius, int xStart, int yStart, int speed, String imageName)
    {
        super();
        name = PlanetName;
        distance = radius;
        x = xStart;
        y = yStart;
        velocity = speed;
        image = imageName;
        setImage("images/" + image);
        rotation++;
        //System.out.println(" Distance=" + distance);  
    }

    public void act()
    {   
        System.out.println("Planet=" + name + " x=" + x + " y=" + y + " Distance=" + distance + " Velocity=" + velocity);
        rotation++;
        x = getNextX(Space.sunX, distance, rotation);
        y = getNextY(Space.sunY, distance, rotation);
        setLocation(x, y);
        Greenfoot.delay(velocity);
    }

    /**
     * Returns next x point on a circle
     **/
    private int getNextX(int xMiddle, int Distance, double Rotation)
    {
        Rotation = Math.toRadians(Rotation);
        double xNext = xMiddle + Math.cos(Rotation) * Distance;
        return (int)Math.round(xNext);
    }

    /**
     * Returns next y point on a circle
     **/
    private int getNextY(int yMiddle, int Distance, double Rotation)
    {
        Rotation = Math.toRadians(Rotation);
        double yNext = yMiddle + Math.sin(Rotation) * Distance;
        return (int)Math.round(yNext);
    }
}

Super_Hippo Super_Hippo

2015/10/28

#
Greenfoot.delay(velocity);
This pauses the whole scenario and not only the current planet. So you should remove that line. Instead, you can either have more points on your circle in this methods (increase rotation by less than 1 in line 80) or you have a timer in the act method, so it is not executed every time. Both depending on the speed of the planet (which is 1 for all planets right now).
You need to login to post a reply.