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

2018/3/2

About non-center Rotation

1
2
kodokulo kodokulo

2018/3/2

#
I have created a see-saw in a World consists of a rigid bar and a fulcrum. The fulcrum can be moved by mouse drag. So i wanted to ask whether i can make the rigid bar realistic and if the fulcrum is moved unbalanced, the rigid bar fall to the longer hand side. Any idea?
danpost danpost

2018/3/2

#
You can create non-center rotation by rotating an actor with an image drawn on a larger image where the point of rotation is drawn in the center.
kodokulo kodokulo

2018/3/4

#
so, i wanted to simulate that my rigid bar always falls to the longer side. Is that mean every time i drag on the fulcrum i have to change my image to a different sized one?
danpost danpost

2018/3/4

#
kodokulo wrote...
so, i wanted to simulate that my rigid bar always falls to the longer side. Is that mean every time i drag on the fulcrum i have to change my image to a different sized one?
My response was based mainly on the title of the thread. Your issue was not stated in a clear and concise manner; so, any specific help is not possible. I would think that in your case a complete illustration of what you were intending would be necessary. You may even need to share what you have in the gallery of this site with source for review.
kodokulo kodokulo

2018/3/7

#
danpost wrote...
kodokulo wrote...
so, i wanted to simulate that my rigid bar always falls to the longer side. Is that mean every time i drag on the fulcrum i have to change my image to a different sized one?
My response was based mainly on the title of the thread. Your issue was not stated in a clear and concise manner; so, any specific help is not possible. I would think that in your case a complete illustration of what you were intending would be necessary. You may even need to share what you have in the gallery of this site with source for review.
so this is how my current position is when the fulcrum moved like this i wanted the bar to turn pivoting that fulcrum here is my current code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int width = bidang.getImage().getWidth();
       int height = bidang.getImage().getHeight();
       int pseudoX = bidang.getPseudoX();
        
       GreenfootImage base = new GreenfootImage(width + Math.abs(pseudoX),height);
       GreenfootImage addition = new GreenfootImage(Math.abs(pseudoX),height);//additional transparent image
       addition.drawRect(0,0,addition.getWidth()-1,addition.getHeight()-1);
       if(pseudoX < 0){
           base.drawImage(bidang.getImage(),0,0);
           base.drawImage(addition,width,0);
           bidang.setImage(base);
          
       }
       else{
           base.drawImage(addition,0,0);
           base.drawImage(bidang.getImage(),addition.getWidth(),0);
           bidang.setImage(base);
           
       }
danpost danpost

2018/3/7

#
Sorry -- not able to view images (message being "not available" -- for several possible reasons including "removed", "not authorized", etc Second attempt -- "Something went wrong (Error 1007; try again or refresh)". Third attempt, same thing.
kodokulo kodokulo

2018/3/7

#
Sorry for the previous post this is the first one this is the second one
danpost danpost

2018/3/7

#
Something like this should work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import greenfoot.*;
 
public class Bar extends Actor()
{
    private Actor fulcrum;
    private int centerX, fulcrumX;
    private GreenfooImage baseImage;
     
    protected void addedToWorld(World world)
    {
        fulcrum = (Actor)world.getObjects(Fulcrum.class).get(0);
        fulcrumX = fulcrum.getX();
        centerX = fulcrumX;
        baseImage = getImage();
    }
     
    private void updateImage()
    {
        GreenfootImage img = new GreenfootImage(baseImage.getWidth+2*(int)Math.abs(centerX-fulcrum.getX()), baseImage..getHeight());
        img.drawImage(baseImage, (img.getWidth()-baseImage.getWidth())/2, 0);
        setImage(img);
        setLocation(fulcrum.getX(), getY());
    }
     
    public void act()
    {
        if (fulcrumX != fulcrum.getX()) updateImage();
    }
}
kodokulo kodokulo

2018/3/8

#
i've tried the code, corrected typo and method, still didn't work. the bar still follows the fulcrumand it keeps being in the center of rotation in the statement
1
GreenfootImage img = new GreenfootImage(baseImage.getWidth+2*(int)Math.abs(centerX-fulcrum.getX()), baseImage..getHeight());
what does the 2*(int)Math.abs(centerX-fulcrum.getX()) do?
danpost danpost

2018/3/8

#
kodokulo wrote...
in the statement
1
GreenfootImage img = new GreenfootImage(baseImage.getWidth+2*(int)Math.abs(centerX-fulcrum.getX()), baseImage..getHeight());
what does the 2*(int)Math.abs(centerX-fulcrum.getX()) do?
It increases the size of the image so that the bar can be drawn off-center -- which apparently I forgot to do. Change line 20 to ??? no, replace lines 19 and 20 with the following:
1
2
3
int xOffset = centerX-fulcrum.getX();
GreenfootImage img = new GreenfootImage(baseImage.getWidth+2*(int)Math.abs(xOffset), baseImage..getHeight());
img.drawImage(baseImage, (img.getWidth()-baseImage.getWidth())/2+xOffset, 0);
I think I got the last sign right.
kodokulo kodokulo

2018/3/9

#
it works well now, thanks a lot.
kodokulo kodokulo

2018/3/9

#
anyway, why is it has to be '2'? just curious
danpost danpost

2018/3/9

#
kodokulo wrote...
anyway, why is it has to be '2'? just curious
In order to get the whole bar in the image with the center of rotation in the right place.
kodokulo kodokulo

2018/3/27

#
i got a bug,, when i change the fulcrum from not the center to the center of the bar, the bar is not centering. i tried to turn it and it is not turning from the center. what should i do?
danpost danpost

2018/3/27

#
kodokulo wrote...
i got a bug,, when i change the fulcrum from not the center to the center of the bar, the bar is not centering. i tried to turn it and it is not turning from the center. what should i do?
You should at least show what code you currently have in the Bar class.
There are more replies on the next page.
1
2