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

2020/12/5

How can I make an actor orbit another actor?

1
2
ek11 ek11

2020/12/5

#
Hello, If at all possible I'd like to use r*Math.sin(degree in radians) and r*Math.cos(degree in radians) to calculate both the x and y in order for one object to orbit another.
danpost danpost

2020/12/5

#
ek11 wrote...
If at all possible
Yes, it is possible.
I'd like to use r*Math.sin(degree in radians) and r*Math.cos(degree in radians) to calculate both the x and y in order for one object to orbit another.
What have you tried? and what issues have you come across?
ek11 ek11

2020/12/5

#
danpost wrote...
ek11 wrote...
If at all possible
Yes, it is possible.
I'd like to use r*Math.sin(degree in radians) and r*Math.cos(degree in radians) to calculate both the x and y in order for one object to orbit another.
What have you tried? and what issues have you come across?
I tried using setLocation in the actor class that I want orbiting using those formulas but honestly I'm just lost and unsure how to go about implementing this. All I have is private int radius; int x = (int)(radius*Math.sin(radians)); //nothing in radians yet int y = (int)(radius*Math.sin(radians)); //nothing in radians yet declared thus far, I'm still fairly new to greenfoot so any help or places i could be pointed to would be greatly appreciated.
danpost danpost

2020/12/5

#
ek11 wrote...
I tried using setLocation in the actor class that I want orbiting using those formulas but honestly I'm just lost and unsure how to go about implementing this.
Things you will need: * a reference to the actor to be orbited (as local variable or instance field)
Actor orbited = ...;
* an orbiting speed (angular) (as instance field)
private int speed = ...;; // in degrees
// or
private double speed = ...;; // in radians
* the radial distance to orbit at (which you have shown you have) Are you required to use sin and cos in your code or is the orbiting behavior the only goal here?
ek11 ek11

2020/12/5

#
danpost wrote...
ek11 wrote...
I tried using setLocation in the actor class that I want orbiting using those formulas but honestly I'm just lost and unsure how to go about implementing this.
Things you will need: * a reference to the actor to be orbited (as local variable or instance field)
Actor orbited = ...;
* an orbiting speed (angular) (as instance field)
private int speed = ...;; // in degrees
// or
private double speed = ...;; // in radians
* the radial distance to orbit at (which you have shown you have) Are you required to use sin and cos in your code or is the orbiting behavior the only goal here?
I am required to do so, I have a reference now, the orbiting speed and the distance now.
danpost danpost

2020/12/5

#
ek11 wrote...
I am required to do so, I have a reference now, the orbiting speed and the distance now.
If orbital speed is in radians, then:
radians += speed;
// your two lines above
setLocattion(orbited.getX()+x, orbited.getY()+y);
ek11 ek11

2020/12/5

#
danpost wrote...
ek11 wrote...
I tried using setLocation in the actor class that I want orbiting using those formulas but honestly I'm just lost and unsure how to go about implementing this.
Things you will need: * a reference to the actor to be orbited (as local variable or instance field)
Actor orbited = ...;
* an orbiting speed (angular) (as instance field)
private int speed = ...;; // in degrees
// or
private double speed = ...;; // in radians
* the radial distance to orbit at (which you have shown you have) Are you required to use sin and cos in your code or is the orbiting behavior the only goal here?
Thank you for the help. Where exactly would I reference the actor to be orbited or what might it look like? I thought I had it but didn't.
danpost danpost

2020/12/5

#
ek11 wrote...
Where exactly would I reference the actor to be orbited or what might it look like? I thought I had it but didn't.
Retain the reference in class of orbiter. You can have world pass it in into its constructor.
ek11 ek11

2020/12/6

#
danpost wrote...
ek11 wrote...
Where exactly would I reference the actor to be orbited or what might it look like? I thought I had it but didn't.
Retain the reference in class of orbiter. You can have world pass it in into its constructor.
Ok, I think I understand what you're saying but unsure how to properly do it. What I've been trying to do is use the formulas to create an orbit around the "Earth" using the moon. The radians is just the angle at which the moon orbits. public class Moon extends Actor { private int radius; double x = (double)(radius*Math.sin(.785)); double y = (double)(radius*Math.cos(.785)); private double speed = .5; public void act() { setLocation(orbited.getX()+x, orbited.getY()+y); } }
danpost danpost

2020/12/6

#
ek11 wrote...
public class Moon extends Actor { private int radius; double x = (double)(radius*Math.sin(.785)); double y = (double)(radius*Math.cos(.785)); private double speed = .5; public void act() { setLocation(orbited.getX()+x, orbited.getY()+y); } }
The radius needs a value. The x and y need to be calculated every act cycle. The radians needs to continuously change by speed, which is way too big. Maybe 0,02 might be more the speed required to orbit at a moderate pace.
ek11 ek11

2020/12/6

#
danpost wrote...
ek11 wrote...
public class Moon extends Actor { private int radius; double x = (double)(radius*Math.sin(.785)); double y = (double)(radius*Math.cos(.785)); private double speed = .5; public void act() { setLocation(orbited.getX()+x, orbited.getY()+y); } }
The radius needs a value. The x and y need to be calculated every act cycle. The radians needs to continuously change by speed, which is way too big. Maybe 0,02 might be more the speed required to orbit at a moderate pace.
Oh Okay, Thank you. How might I calculate the act per cycle?
danpost danpost

2020/12/6

#
ek11 wrote...
How might I calculate the act per cycle?
This refers to what should be in act method:
danpost wrote...
If orbital speed is in radians, then:
radians += speed;
// your two lines above
setLocation(orbited.getX()+x, orbited.getY()+y);
ek11 ek11

2020/12/6

#
ek11 wrote...
danpost wrote...
ek11 wrote...
public class Moon extends Actor { private int radius; double x = (double)(radius*Math.sin(.785)); double y = (double)(radius*Math.cos(.785)); private double speed = .5; public void act() { setLocation(orbited.getX()+x, orbited.getY()+y); } }
The radius needs a value. The x and y need to be calculated every act cycle. The radians needs to continuously change by speed, which is way too big. Maybe 0,02 might be more the speed required to orbit at a moderate pace.
Oh Okay, Thank you. How might I calculate the act per cycle?
Here's my code now public class Moon extends Actor { private int radius = 150; private double radians = .02; private double speed; private actor? orbited = 2; public void act() { radians += speed; double x = (double)(radius*Math.sin(radians)); double y = (double)(radius*Math.cos(radians)); setLocation(orbited.getX()+x, orbited.getY()+y); } } Thank you, I guess my only question is what or how to reference the actor now?
danpost danpost

2020/12/6

#
ek11 wrote...
what or how to reference the actor now?
danpost wrote...
Retain the reference in class of orbiter. You can have world pass it in into its constructor.
That is, pass the earth from world to the moon's constructor.
ek11 ek11

2020/12/6

#
danpost wrote...
ek11 wrote...
what or how to reference the actor now?
danpost wrote...
Retain the reference in class of orbiter. You can have world pass it in into its constructor.
That is, pass the earth from world to the moon's constructor.
Can you give me an example of what that'd look like? my current code looks like MOON public class Moon extends Actor { /* FIELDS */ private int radius = 150; private double radians = .02; private double speed; /* CONSTRUCTORS */ public Moon() { } /* METHODS*/ public void act() { radians += speed; double x = (double)(radius*Math.sin(radians)); double y = (double)(radius*Math.cos(radians)); setLocation(orbited.getX()+x, orbited.getY()+y); } } GAME WORLD public class GameWorld extends World { private Moon moon; //moon reference to the moon object /** * Constructor for objects of class MyWorld. * */ public GameWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(700, 600, 1); prepare(); }
There are more replies on the next page.
1
2