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

2017/3/17

Code for making an object orbit another is causing a slow spiral rather than an orbit

macho_horse macho_horse

2017/3/17

#
So I'm making a game where a spaceship (called 'Ship') orbits portal/black hole/thing called 'Skull'. Here's the code; public void act() { if(Greenfoot.isKeyDown("left")) { Skull skull= (Skull) getWorld().getObjects(Skull.class).get(0); turnTowards(skull.getX(), skull.getY()); setRotation(getRotation() - 90); move(-4); // makes sure the player orbits the portal } if(Greenfoot.isKeyDown("right")) { Skull skull= (Skull) getWorld().getObjects(Skull.class).get(0); turnTowards(skull.getX(), skull.getY()); setRotation(getRotation() - 90); move(4); } My problem is that rather than orbiting in a circle the ship orbits in a slow outward spiral.
Super_Hippo Super_Hippo

2017/3/17

#
I think it is easier to understand why this is not forming a circle if you would change the 4 to a higher number. To make it be a circle, it had to move 0 and that is also not working. Try this:
private int rotation = 0; //starting on the right side, adjust as needed

public void act()
{
    int dr = 0;
    if(Greenfoot.isKeyDown("left")) dr--;
    if(Greenfoot.isKeyDown("right")) dr++;
    if (dr != 0)
    {
        rotation += dr;
        Skull skull= (Skull) getWorld().getObjects(Skull.class).get(0);
        setLocation(skull.getX(), skull.getY());
        setRotation(rotation);
        move(100); //distance between middle and the ship //if this is changing, you can get the distance before moving to the skull object
        turn(-90);
    }
}
You need to login to post a reply.