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

2013/5/14

Setting my world

1
2
welleid welleid

2013/5/14

#
Hey guys, i have a question. I'm trying to create a world, by setting some blocs. I want them to be like a circle. So i have a world of 600x600 px, and I want ten blocs to be in a round arranged around the center, just like this image, it's the idea^^, but only one range of blocs. So i tried with a for loop, but i fail miserably.
for(int i = 0; i < 4; i++)
       { 
           Bloc bloc = new Bloc();
           addObject(bloc, 70 + incrementeur*i*20, 500 - i*150 + decrementeur*50 );
           bloc.setRotation(40*i - 90);
       }
This would be the idea, here i was trying to first create one quarter, than the other etc... Thanks a lot guys !
davmac davmac

2013/5/14

#
What exactly is the shape of your Bloc? (maybe post the image)
welleid welleid

2013/5/14

#
oh yes sorry, it's a simple rectangle of 90x30 px (width x height)
davmac davmac

2013/5/14

#
So it's not curved? It's not like the picture above? Can you explain exactly how you want it to look - maybe draw a picture?
welleid welleid

2013/5/14

#
It's not curved at the moment, but i'll do it later, using the same width and height. For now, i just want my blocs to be positioned like in the picture, creating a circle. Thanks a lot !
davmac davmac

2013/5/14

#
So I assume you want twelve blocks, as in the picture above. If the long edge is 90 pixels, let's round up to 100 to allow for some space between them. Then the radius is approximately 100*12 / (2*Pi), + 15 (half the short edge), which is about 205. You need a loop with 12 iterations (one for each block) and the position at each iteration would be: centerx + sin(360/12 * i) * 205 centery - cos(360/12 * i) * 205 This puts the first block above the center, and the rest clockwise from that position. You'd need to set the rotation to (360/12 * i) for each block.
        for (int i = 0; i < 12; i++) {
            int cx = (int)(210 + Math.sin(Math.toRadians(360/12 * i)) * 205);
            int cy = (int)(210 - Math.cos(Math.toRadians(360/12 * i)) * 205);
            Bloc b = new Bloc();
            addObject(b, cx, cy);
            b.setRotation((int)(360/12 * i));
        }
The code above uses 210,210 as the center point.
danpost danpost

2013/5/15

#
Actually, your code was not too far off. Instead of trying to place the blocks at their final location, it would be much easier to place them in the center, rotate them, and then move them the appropriate distance away from the center at the angle of its rotation. If necessary, after the move, turn the object 90 degrees. In the world, create and add the Block objects like this:
int blockCount = 10;
for(int i=0; i<blockCount; i++) {
    Block block = new Block();
    addObject(block, 200, 200);
    block.setRotation(i*360/blockCount);
    block.move(150); // adjust as needed
    // turn(90);      if needed
}
welleid welleid

2013/5/15

#
Thanks a lot ! works perfectly, but now i want the all to turn around the center (animation). How am i suppose to set all those blocs as one object, and then make it turn ?
davmac davmac

2013/5/15

#
You can't set them as one object. You could either make then one object to begin with (i.e. draw the blocks onto a single large image), or you can have them all move at the same time. If you go with the latter approach, they each should know the center point and their own angle from it, and then they can continuously increase the angle by some fixed amount and reposition.
welleid welleid

2013/5/15

#
ok so i did something like this in the Bloc.class
public void act() 
    {
       move(1);
       setRotation(getRotation() + 1);
    }  
But the problem is that it moves too quickly, the circle form doesnt stay. I want it to turn slowly, and always staying in circle. I tried with some getX() and getY(), but the failure is as high as my amount of try. Thanks !
davmac davmac

2013/5/15

#
You need to use the code above to choose the location. Just moving and turning is never going to be accurate enough to maintain a circle formation.
I tried with some getX() and getY(), but the failure is as high as my amount of try.
Perhaps post the code that you did try.
welleid welleid

2013/5/15

#
 int posx = getX();
        int posy = getY();

        rotX = (int)(posx + (Math.sin(Math.toRadians(360/100)) * 205)+1 );
        rotY = (int)(posy + (Math.cos(Math.toRadians(360/100)) * 205)+1 );
        
        setLocation(rotX, rotY);
tried something like that, like I get the position of the bloc (x and y), I add it with some radian calculus and I add a 1 to make it move
danpost danpost

2013/5/15

#
welleid wrote...
the failure is as high as my amount of try
I think that goes without saying, if you are asking for help. You need to put the code that positions each block in a method within the Bloc class, so you can call it repeatedly as the angle from the center-point changes. With your Bloc class code something like this:
import greenfoot.*;

public class Bloc extends Actor
{
    private int centerX, centerY; // the center-point coordinates
    private int angle; // the angle from center-point 
    private int radius; // the distance from center-point
    
    public Bloc(int pointX, int pointY, int alphaAngle, int distance)
    {
        centerX = pointX;
        centerY = pointY;
        angle = alphaAngle;
        radius = distance;
        // create image of block (if needed)
    }

    public void addedToWorld(World world)
    {
        updateLocation(); // position block object
    }

    private void updateLocation()
    {
        setLocation(centerX, centerY); // place at center
        setRotation(angle); // set rotation to angle
        move(radius); // move away from center
        turn(90);     // adjust rotation, if needed
    }

    public void act()
    {
        angle = (angle+1)%360; // incrementally cycle through angles
        updateLocation(); // re-position for this cycle
    }
}
you can create the Bloc objects in your world constructor with
int blockCount = 10; // total number of blocks
int x = 200; // x-coordinate of center-point
int y = 200; // y-coordinate of center-point
int d = 150; // distance from center to position each block
for(int i=0; i<blockCount; i++)
{
    int a = i*360/blockCount; // angle from center-point for this block
    addObject(new Bloc(x, y, a, d), x, y);    
}
welleid welleid

2013/5/15

#
Thanks man !! works perfectly ! but is there any way to make it turn less fastly ? without touching the speed execution of the greenfoot environment ?
danpost danpost

2013/5/15

#
Add an instance int delayTimer:
private int delayTimer;
// then the act method will be
public void act()
{
    delayTimer = (delayTimer+1)%2; // increase last value to slow down more
    if (delayTimer > 0) return; // not time to progress yet
    angle = (angle+1)%360;
    updateLocation();
}
The speeds you can acquire from the above are 1/2, 1/3, 1/4, etc. of the normal speed. If you require something different, we will have to adjust the code even more.
There are more replies on the next page.
1
2