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

2013/4/14

Making a Piñata

Artaphawp Artaphawp

2013/4/14

#
Hello! Can somebody help me with moving a piñata in Greenfoot or in simple words making a pendulum that just swings around? Thanks in advance! :D
danpost danpost

2013/4/14

#
You might get some insight by viewing the code of my Pendulum Demo scenario.
Artaphawp Artaphawp

2013/4/14

#
Thank you! :) I've already checked your code. Can you help me with this problem?
 int iterador = 1;
    boolean bandera = false;
    public void act() 
    {
        while(!bandera)
        {
           negativeBalance(); 
           Greenfoot.delay(50);
           if(iterador == 90)
           {
               bandera = true;
           }
        }
        while(bandera)
        {
            positiveBalance(); 
            Greenfoot.delay(50);
            if(iterador == -90)
            {
               bandera = false; 
            }
        }
    }
     
    
    public void negativeBalance()
    {
        turn(1);
        iterador++;  
    }
    
    public void positiveBalance()
    {
        turn(-1);
        iterador--;
    }    
This is what I´ve used for "moving" my piñata. It´s just rotating, I know. But the trouble is, how can I delay the movement without using delay() or setSpeed() ? It just acts too fast :(
danpost danpost

2013/4/14

#
Well, you do not want to use 'delay' because the whole scenario will delay for the duration; and you do not want to use 'setSpeed' because all characters will still move the same speed 'with respect to the pendulum motion'. What you want to do is use a sine wave to control the swing of the pendulum (or a cosine wave). Both return values that vary from one to negative one, and them back to one. We can use that to regulate the swing of the pendulum. All we need is the range of the swing and a counter to iterate through the angles of a circle whose cosine and/or sine can be multiplied by the range to get the current angle of the pendulum (if the range was 60, then the angle will max out at -60 and 60 degrees from either side of just hanging straight down). How fast you iterate through the angles of the circle determine the frequency of the pendulum. The counter should increment from zero to 359, then jump back to zero to start again. The code for that is simply:
counter = (counter + frequency) % 360;
where frequency is a number that returns no remainder when dividing it into 360 (you will probably use either 4, 5, or 6 for the speed you want).
danpost danpost

2013/4/14

#
Crudely written, the code would look something like the following:
// using these instance fields
private static final int FREQ=5;
private int counter;
private double range;
// in the act method or method it calls
counter = (counter + FREQ) % 360;
double factor = Math.sin(Math.PI*(double)counter/180.0);
setRotation((int)(factor*range));
As an added effect, the next line could be:
range *=0.9; // some value just under one
this will cause the pendulum to eventually settle. However, starting a new swing will require some work determining what the counter value should be changed to when setting the new range.
You need to login to post a reply.