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

2014/11/20

Moving different actors (actors a) to a location closely around new actor b

1
2
habidex habidex

2014/11/20

#
I am new in programming using greenfoot, I want to write a code that would make some actors to move to another different actor and they should be around the actor in a circular form. Plz any idea and guide?
Jonas097 Jonas097

2014/11/20

#
If you want to move A towards B: position of object B minus position of object A (both as vector; take the one in my project if you want: Vector2f) then normalize it multiply with your speed and give this value to A for moving This is at least how I do it.
danpost danpost

2014/11/21

#
I realize that the response Jonas097 gave was cryptic (even for me). So, I will try to explain an easier way to go about it. If each circling actor gets it angle to the actor to be circled, then sets its rotation to 90 degrees from that angle, then turns slightly toward the actor and finally moves, it will approach it until an equilibrium is reached between the change in angle and the distance between the actor. My Orbital Effects scenario demonstrates this type of movement. You can view the classes by running the scenario. The Centre class creates an actor that follows the mouse. Another class is found inside that class called Orbiter, which creates the objects the tend to orbit the Centre object.
Jonas097 Jonas097

2014/11/21

#
Oh. I misread the question. I don't really know what you mean by circling around. Maybe calculate the angle between the two objects and then add a small amount in either plus or minus. Set this angle for A. Then move. There are built-in methods for this I think.
habidex habidex

2014/11/21

#
To make small actors situated around the other actor
danpost danpost

2014/11/21

#
habidex wrote...
To make small actors situated around the other actor
Do you want these small actors to move toward and then station themselves around the central actor? That is, once they are a certain distance away, they stop? Do you want these small actors to space themselves around the central actor evenly (that would be a bit more complicated to accomplish, however -- unless each small actor was given a specific position to station itself at)?
habidex habidex

2014/11/21

#
Yes I want these small actors to move toward and then station themselves around the central actor. I have more than one central actor and many small actors, I want them to distribute themselves to those big actors. For example, if I have nine small actors and 3 big actors, I want 3 actors each to move to each big actor. Even if the actors would be given position to station themselves, no problem. Thanks and waiting for ur usual assistance
danpost danpost

2014/11/21

#
You should be able to create these small actors, giving them which big actor to go to and what position at which it should be around the big actor. Something like this in your World subclass constructor:
for (int n=0; n<3; n++)
{
    BigGuy bigGuy = new BigGuy();
    addObject(bigGuy, /* whatever x and y */);
    for (int c=0; c<3; c++)
    {
        addObject(new SmallGuy(bigGuy, c), /* whatever x and y */);
    }
}
The constructor in the SmallGuy class needs to accept these parameters and save them for later use, so in the SmallGuy class:
// instance fields
private Actor bigGuy;
private int position;
// the start of the constructor
public SmallGuy(Actor actor, int pos)
{
    bigGuy = actor;
    position = pos;
Now, the act method can use 'bigGuy.getX()' and 'bigGuy.getY()' to approach the actor and 'pos' to settle into its position.
habidex habidex

2014/11/22

#
Thanks! But how can I control different object of the same class (Actors). Because they all respond to the command at the same time, I wanna have control over them individually. Thanks
danpost danpost

2014/11/22

#
I am not quite sure I understand your question. Usually only one character is controlled by the user and sometimes two for a two-player game. But, it appears you want to have control over multiple characters. Maybe you should explain what you are trying to accomplish with more detail. Also, showing your current code might help.
danpost danpost

2014/11/22

#
I think what you need in the Actors class is a 'static' field to hold the currently active actor (that one that has focus for controlling). Usually, with 'focusable' objects, the image brightens or alters in some way to show it is the one being focused on at the moment -- you may or may not want to incorporate something like that. All controllable actions should check this field to see if it is the active actor:
if (activeActor == this)
before performing that action. If all the Actors objects are to remain totally inactive except for the active one, you can have something like the following as the first line in the act method of the class:
if (activeActor != this) return;
habidex habidex

2014/11/22

#
What I really want to do is to design a program that would help in automatic group generator. An actor for group and the other for students to be grouped. We can have more than one group and more than one students. The students should be read from a file and be represented as a symbol on the background. When running the program, the student symbols should move in a random form and get circled around the available groups. So I already know how to generate random numbers to tag with each group so that they would be assigned to the students. The students can now move in a random order to different locations. This I think I should have control on each of the students so that I can specify their respective direction in motion.
danpost danpost

2014/11/22

#
Maybe the problem is that you do not realize that once the Student object is assigned a Group object to go to, their respective directions are locked in (that is, if they are not at the group, move to that group). Please show your Student class code (there is a 'code' link below the reply box to insert code into your posts).
habidex habidex

2014/11/22

#
I dont have much in Student class because I added them through the subclass of World. I just want to control them in their respective class. This is all I have in Student class:
public class Student extends Actor
{
public void act() 
    {
        //if(Greenfoot.mousePressed(this)){
            move(8);
            setRotation(Greenfoot.getRandomNumber(360));
            for(int i=0; i<5; i++)
            {
                //            setRotation(Greenfoot.getRandomNumber(360));
                int a=Greenfoot.getRandomNumber(600);
                int b=Greenfoot.getRandomNumber(600);
                turnTowards(a,b);
            }
            //        turnTowards(200,300);
        //}    
    }
}
They only move together at once and move to the same location
danpost danpost

2014/11/22

#
First, the Student class can be reduced to this:
public class Student extends Actor
{
    public void act()
    {
        move(8);
        setRotation(Greenfoot.getRandomNumber(360));
    }
}
The rest of the code does nothing but set a different random rotation to the actor (which is already done by line 6 of this post). Please, put in words for me what exactly a single student should be doing from the time it is placed into the world to ... whenever (whether removed or the game ends or whatever). With the code you gave for the students, they will most probably wiggle about within the same general location, while slowly migrating around the world. As is, there are no conditions placed on the movement of your Student objects. You can, however, still have the control in the Student class using the proper conditions on moving.
There are more replies on the next page.
1
2