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

2023/8/21

length - width height

1
2
danpost danpost

2023/8/28

#
ronald wrote...
<< Code Omitted >>
You could divide by a higher value at line 12. As an alternative, you could take the image of pendulum in new world and apply it to the one in the old world; then replace the pendulum in the new world with the one from the old world.
ronald ronald

2023/8/29

#
I looked around the forum a bit. from what I understand, it allows to keep a contact between the two worlds, to ensure that the two worlds communicate and therefore my question is it mandatory to create a new class in world?
PendulumWorld4 oldWorld = (PendulumWorld4) getWorld();
 PendulumWorld4_1 newWorld = new PendulumWorld4_1();
 newWorld.setWorld(oldWorld);
 Greenfoot.setWorld(newWorld);
danpost danpost

2023/8/29

#
ronald wrote...
from what I understand, it allows to keep a contact between the two worlds, to ensure that the two worlds communicate and therefore my question is it mandatory to create a new class in world?
No new class; communication only need be one way. Just make the adjustment when a secondary world is created (when 'up'/'down' key is pressed): This was given for a member of the MyWorld class previously:
public void act() {
    String key = Greenfoot.getKey();
    if (key != null) {
        // actions to change world size
        int change = 0;
        if ("up".equals(key)) change = 1;
        if ("down".equals(key)) change = -1;
        if (change != 0) {
            int halfHeight = getHeight()/2+change;
            if (halfHeight>= 100 && halfHeight<= 300) {
                Greenfoot.setWorld(new MyWorld(halfHeight));
            }
        }
        // other key related actions (preferably here)
    }
}
Replace line 11 with the following:
World w_new = new MyWorld(halfHeight);
Pendulum_4 p_new = (Pendulum_4) w_new.getObjects(Pendulum_4.class).get(0);
Pendulum_4 p_old = (Pendulum_4) this.getObjects(Pendulum_4.class).get(0);
p_old.setImage(p_new.getImage());
w_new.addObject(p_old, p_new.getX(), p_new.getY());
w_new.removeObject(p_new)
Greenfoot.setWorld(w_new);
This moves the first pendulum object created along to each new world, updating its image each time the world changes.
ronald ronald

2023/8/29

#
ok it only works one way I'm thinking of scaling but hey, I'm wondering, because I tried to do this from the length between the two actor and world classes, obviously it didn't work but with the code you gave me, it makes sense to do this from the pendulum image I never thought from the image sometimes I have so many ideas that it doesn't work or I'm doing it wrong to code I will try to see a little I'll let you know for the future
ronald ronald

2023/8/29

#
before doing stupid things I find out from you I saw your scenario world scaling demo I think it's kinda the same may need another class of zoom but how to adapt it to the change when I click on up ow down THANKS
danpost danpost

2023/8/30

#
ronald wrote...
I saw your scenario world scaling demo I think it's kinda the same may need another class of zoom but how to adapt it to the change when I click on up ow down
It might be best to just pass the initially created pendulum from world to world. You can keep a reference to the pendulum, plus the world's scalar value, with static fields in your MyWorld class. Then, adjust the Pendulum_4 class code to update its image when added to a world. Also, it is not good to have the image of the pendulum drawn every act step. Better is to have one image (per size) and just rotate it as needed. That is, let the pendulum swing as it should. I was messing around with it and came up with the following classes: MyWorld class:
import greenfoot.*;

public class MyWorld extends World
{
    public static int SIZE = 300;
    public static Pendulum pendulum;
    
    public MyWorld()
    {
        super(SIZE*3, SIZE*2, 1);
        GreenfootImage bg = getBackground();
        bg.setColor(Color.BLUE);
        bg.fillRect(0, 0, SIZE*3, SIZE/2);
        bg.setColor(Color.GREEN);
        bg.fillRect(0, SIZE*3/2, SIZE*3, SIZE/2);
        if (pendulum == null) pendulum = new Pendulum();
        addObject(pendulum, SIZE*3/2, SIZE/2);
    }
    
    public void act() {
        int change = 0;
        String key = Greenfoot.getKey();
        if ("up".equals(key)) change++;
        if ("down".equals(key)) change--;
        if (change == 0) return;
        change *= 4;
        if (SIZE+change < 100 || SIZE+change > 300) return;
        SIZE += change;
        Greenfoot.setWorld(new MyWorld());
    }
}
Pendulum class:
import greenfoot.*;

public class Pendulum extends Actor
{
    private double angle = Math.PI/2;
    private int length;
    double angleAccel;
    double angleVelocity;
    double dt = 0.2;
     
    public Pendulum() {
        length = MyWorld.SIZE;
        GreenfootImage img = new GreenfootImage(length/5, length*11/5);
        img.fillOval(length/20, length*21/20, length/10, length/10);
        img.drawLine(length/10, length*11/10, length/10, length*2);
        img.fillOval(0, length*19/10, length/5, length/5);
        setImage(img);
    }
    
    public void act() {
       angleAccel = -9.81 / length * Math.sin(angle);
       angleVelocity += angleAccel * dt;
       angle += angleVelocity * dt;
       setRotation((int)(angle*180/Math.PI));
    }
}
ronald ronald

2023/8/30

#
First of all thank you for the pendulum code that's what I feared, a pendulum is moving, it's not good, it's more difficult to handle, i.e. changing size while moving... that's what I said to myself when I compared your scaling world demo scenario which is stable and a moving pendulum otherwise I thank you again
You need to login to post a reply.
1
2