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

2016/6/25

Make an actor orbiting another actor

MacG MacG

2016/6/25

#
Hello, I'm a newbie on greenfoot.. I want to make an actor to make a circle move on other actor.. I've discover how to make an actor to make a circle move, but, It's move circle on the whole world, not orbiting another actor. So, I want to know how to make an actor orbiting another actor ? Any response apriciated :D
danpost danpost

2016/6/25

#
One way is to turn the actor slightly and relocate the actor at the center of the circle; then turn 90 degrees and move the radial distance required from the center and un-turn 90 degrees.
MacG MacG

2016/6/27

#
I don't know how to do that on greenfoot, can you give me a code sample danpost ? btw, thank you for responing my question :D
danpost danpost

2016/6/27

#
MacG wrote...
I don't know how to do that on greenfoot, can you give me a code sample danpost ? btw, thank you for responing my question :D
// with instance fields
private int centerX, centerY; // for center point of circle made by orbital path
private int radius; // for distance from center point

// in act
turn(1); // adjust value as needed
setLocation(centerX, centerY);
turn(90); // face new location to be
move(radius); // move to location
turn(-90); // face direction of orbital path
What is changing is the angle around its orbit. There is no speed but that given by the change in angle. For help in setting the appropriate values for the fields, you will need to show the class code; as well, you will need to supply code for creating and adding the related actors into the world. If the actor that is being orbited moves, then it would be better to save a reference to it and use 'getX' and 'getY' on it instead of using the 'centerX' and 'centerY' fields.
danpost danpost

2016/6/28

#
I realize that the code I gave is very restrictive as far as what "speeds" the orbiter can take and what positions are allowed along its orbital path. To improve on it, more complicated code is required. As a result, I created a basic Orbiter class which allows an actor to orbit a point or another actor. If you wish, I could upload a sample scenario with source (might be later on tonight or tomorrow, however). What will be uploaded is, like I said, a basic class. I will subsequently be making improvements to make it more versatile (allowing changes in speed, orbital distance and orbiting state) as well as giving it more precise movement.
MacG MacG

2016/6/29

#
may I need to give my code ? If yes, which code do I need to copy ? Because I really new in GreenFoot :D
danpost danpost

2016/6/29

#
MacG wrote...
may I need to give my code ? If yes, which code do I need to copy ? Because I really new in GreenFoot :D
Not unless you have more issues. Again, you would have to supply any related code and give a brief explanation as to what should happen and what is currently happening that should not. My last post was just letting you know that I created a class that give actors the ability to orbit around a specific point or a specific actor. It is better than the code given above because the orbiter is more precisely moved and placed (the orbit is more precisely defined). It has nothing (currently) to do with your project. But it will be available (if you request to see it) to download and look at -- and you could even implement it into a project similar to the one you already have (I would suggest you save what you currently have and try to implement the Orbiter class into a copy of the project, if you decide to try it out).
You need to login to post a reply.