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

2015/4/23

How to rotate an object orbiting another object without making the object rotate

YosuaZakaria YosuaZakaria

2015/4/23

#
I am trying to find out how to make an orbiting object stay static Start: <- orbiting object then while orbiting <- orbiting object So far I only got this _ _ <- the orbiting object rotate 90 degress How to make the object (in my case a planet image) static and not rotating?
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import greenfoot.*;
 
/**
 * Write a description of class world here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class world extends World
{
    private GreenfootSound backgroundmusic = new GreenfootSound("music.mp3");
    public world()
    {   
        super(3515, 2154, 1);
        prepare();
    }
    public void started() 
    
        backgroundmusic.playLoop(); 
    }
     
    private void prepare()
    {
        // Add your constructor code here.
        title title = new title();
        sun sun = new sun();
        mercury mercury = new mercury();
        venus venus = new venus();
        earth earth = new earth();
        moon moon = new moon();
        mars mars = new mars();
        jupiter jupiter = new jupiter();
        saturn saturn = new saturn();
        uranus uranus = new uranus();
        neptune neptune = new neptune();
        asteroid_belt asteroid_belt = new asteroid_belt();
        ceres ceres = new ceres();
        vesta vesta = new vesta();
 
        // Add your object code here.
        addObject(title, 471, 179);
        addObject(sun, 1758, 1077);
        addObject(mercury, 0, 0);
        addObject(venus, 0, 0);
        addObject(earth, 0, 0);
        addObject(mars, 0, 0);
        addObject(asteroid_belt, 0, 0);
        addObject(jupiter, 0, 0);
        addObject(saturn, 0, 0);
        addObject(uranus, 0, 0);
        addObject(neptune, 0, 0);
 
        // Add your position code here.
        mercury.setRotation(0);
        venus.setRotation(0);
        earth.setRotation(0);
        mars.setRotation(0);
        jupiter.setRotation(0);
        saturn.setRotation(0);
        uranus.setRotation(0);
        neptune.setRotation(0);
        vesta.setRotation(0);
        ceres.setRotation(0);
 
        // Add your action code here.
        mercury.act();
        venus.act();
        earth.act();
        mars.act();
        asteroid_belt.act();
        jupiter.act();
        saturn.act();
        uranus.act();
        neptune.act();
    }
}
and in one of the planets
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
30
31
32
import greenfoot.*;
 
/**
 * Write a description of class mercury here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class mercury extends Actor
{
    int radius = 210;
    int rotational_speed = 1;
    boolean MouseHover = false;
    /**
     * Act - do whatever the mercury wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        setLocation((getWorld().getWidth()/2), (getWorld().getHeight()/2));
        turn(rotational_speed);
        move(radius);
        if(!MouseHover && Greenfoot.mouseMoved(this))
        {
            MouseHover = true; // don't mind these, I'm planning to show a statistic card when user hover on the planet
        }
        if (MouseHover && Greenfoot.mouseMoved(null) && ! Greenfoot.mouseMoved(this))
        {
            MouseHover = false;
        }
    }   
}
YosuaZakaria YosuaZakaria

2015/4/23

#
FIX cause the symbol I use to represent before didn't show up 00 00<- orbiting object I want to make them stay still while orbiting, so after 90 degree orbit it will like this 00 00 <- orbiting object So far I got this 00 8<- orbiting object
danpost danpost

2015/4/23

#
The basic idea for movement can be realized by looking at the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
// instance fields
private int radius;
private int centerX, centerY;
private int angle;
  
public void orbit()
{
    setLocation(centerX, centerY);
    setRotation(angle+1);
    angle = getRotation();
    move(radius);
    setRotation(0);
}
Granted, this uses int values for the distance and the angle and you probably are using some sort of precision movement system. Most systems do not include precision angles as well as distance; however, I did create one that does. It is in my Asteroids w/improved QActor SuperClass scenario. You can adjust the value of QVAL in the QActor class to increase the precision (each unit, pixel or degree, will be divided by its value to be the new unit of measure for movement and rotation).
You need to login to post a reply.