how would i do this? just as if a planet was being orbited by a satellite... restrict distance? is there a way to measure distance? plz anything helps!
You would have to constrain the position of your orbiting (satellite) actor to a set distance from your main (anchor) actor.. Using sin/cos you can calculate this. First you need to store the x and y coordinates of the main actor in game attributes (unless it's static, but let's assume not):
constrain self.position.X to game.anchorX constrain self.position.Y to game.anchorY
Then, in our satellite actor we need to calculate our X and Y position based on two things: 1. the distance from the anchor 2. The angle at which we're orbiting.. For example, if we're out to the top-right of the actor we might be at a 45 degree angle, but at the top left we're at a 135 degree angle.
An easy way to make this dynamic is to have the satellite actor have an attribute called angle and magnitude, and we'll position this actor based on the angle using contraints. Then, we can change the angle and magnitude to move the actor around the anchor and further/closer to the anchor, respectively.
so create self.angle and self.magnitude in the satellite actor then add these rules:
constrain self.position.X to game.anchorX + self.magnitude * sin(-self.angle) constrain self.position.Y to game.anchorY + self.magnitude * cos(-self.angle)
Forgive me if I have my negatives/positives mixed up. It may be a positive self.angle, I can't remember off the top of my head. So if the position is not at the right angle, just reverse that sign and try again.
I should add, you want to set self.angle and self.magnitude in the satellite actor. So if the satellite is supposed to be 100 pixels from the anchor, set magnitude to 100. If it's supposed to be rotating around the actor, you can use a timer to make it rotate:
timer - every .1 seconds - change attribute self.angle to self.angle + 1
That would make it rotate 10 degrees every second, so it would do one revolution every 36 seconds.
Comments
constrain self.position.X to game.anchorX
constrain self.position.Y to game.anchorY
Then, in our satellite actor we need to calculate our X and Y position based on two things: 1. the distance from the anchor 2. The angle at which we're orbiting.. For example, if we're out to the top-right of the actor we might be at a 45 degree angle, but at the top left we're at a 135 degree angle.
An easy way to make this dynamic is to have the satellite actor have an attribute called angle and magnitude, and we'll position this actor based on the angle using contraints. Then, we can change the angle and magnitude to move the actor around the anchor and further/closer to the anchor, respectively.
so create self.angle and self.magnitude in the satellite actor then add these rules:
constrain self.position.X to game.anchorX + self.magnitude * sin(-self.angle)
constrain self.position.Y to game.anchorY + self.magnitude * cos(-self.angle)
Forgive me if I have my negatives/positives mixed up. It may be a positive self.angle, I can't remember off the top of my head. So if the position is not at the right angle, just reverse that sign and try again.
timer - every .1 seconds - change attribute self.angle to self.angle + 1
That would make it rotate 10 degrees every second, so it would do one revolution every 36 seconds.