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

2016/3/7

Make an actor rotate around another actor

Cameronte10 Cameronte10

2016/3/7

#
So in this project i'm trying to get the enemy to travel around the character but don't know how to do it. Currently all i have is:
turnTowards(Player.main.getX(), Player.main.getY());
what do i add to make it spin around the Player?
danpost danpost

2016/3/7

#
There are several ways to accomplish this. Some are more accurate and/or versatile than others. One of the simplest ways is to just turn a degree or more each act cycle and then set the location by starting at the location of the actor rotating around, turn 90 degrees, move some distance away and un-turn 90 degrees. The speed is determined by the change in angle each act cycle and the orbital distance:
turn(1); // adjust 'speed' value as needed
setLocation(Player.main.getX(), Player.main.getY());
turn(-90);
move(100); // adjust 'orbital distance' value as needed
turn(90);
Unfortunately, this might work well for short distances and faster orbits; but, it does look very good with longer and slower orbits. To improve on this we can move and then turn:
move(1); // adjust 'speed' value as needed
turnTowards(Player.main.getX(), Player.main.getY());
setLocation(Player.main.getX(), Player.main.getY());
turn(180);
move(100); // adjust 'orbital distance' value as needed
turn(90);
By the way, you should experiment with these to see what orbital paths are taken and how they appear visually. For more accurate movement, you would need to keep track of the angle and the change in angle for each move as double type values using instance fields and use trig functions to calculate the new location coordinates each act cycle; this is, of course, more advanced in nature.
Cameronte10 Cameronte10

2016/3/7

#
The enemy only seems to move when the Player is still. Is there a way to fix that?
danpost danpost

2016/3/7

#
Cameronte10 wrote...
The enemy only seems to move when the Player is still. Is there a way to fix that?
We cannot fix what we cannot see. Please show related code (Player class and class of enemy).
Cameronte10 Cameronte10

2016/3/9

#
public class Enemy extends Enemies
{
    private int timer=0;
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
   
       
        move(1); // adjust 'speed' value as needed
turnTowards(Player.main.getX(), Player.main.getY());
setLocation(Player.main.getX(), Player.main.getY());
turn(180);
move(100);
turn(90);
        
        if(timer>0)timer--;
if(timer==0)
{
    
    Magic1 bullet = new Magic1();
    getWorld().addObject( bullet, getX(), getY());
    bullet.setRotation(getRotation() +90);
    timer=60;
}
    }    

}
private int timer=0; public static Player main; public Player() { main = this; } /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.isKeyDown("left")) { turn(-3); } if(Greenfoot.isKeyDown("right")) { turn(3); } if(Greenfoot.isKeyDown("up")) { move(3); } if(Greenfoot.isKeyDown("down")) { move(-3); } if(timer>0)timer--; if(timer==0 && Greenfoot.isKeyDown("space")) { Magic1 bullet = new Magic1(); getWorld().addObject( bullet, getX(), getY()); bullet.setRotation(getRotation()); timer=120; } } }
danpost danpost

2016/3/9

#
I would guess that the distance you are moving the enemy is not enough to change the angle by a full degree, so it moves back to where it started. Try increase the speed from 'move(1)' to something higher.
You need to login to post a reply.