Make an object orbit around another

For questions about using Classic.

Post » Mon May 04, 2009 11:53 am

Hey, I searched the forum, but couldn't find any answer on this.

Does anyone have an idea how to make an object move in a circle around a defined center.

I looked at the available behaviors, but I'm not sure if any of them is capable of doing just that.

Can anyone shed some light on that?

Thanks :mrgreen:
B
2
G
3
Posts: 10
Reputation: 870

Post » Mon May 04, 2009 12:15 pm

Bullet behavior can do that, just make it move with constant speed and change its angle each tick. So basically it will loop; you just have to find the right amount of rotation.

Or you can change the object's hotspot to be outside the object itself and make it rotate itself. That is a pretty crude solution, though.

Finally, you can use a simple positioning event like in the following pseudocode:

[code:xra9uj10]Set position to Center.X + Radius * cos(Period), Center.Y + Radius * sin(Period)
Add 1 to Period
If Period = 360, set it to 0
Rinse, repeat
[/code:xra9uj10]
The add 1 to Period - 1 can be smaller or larger, depending on how fast you want it to rotate. It would take 360 frames for it to make the full circle.

So it will always rotate around the Center object, no matter what it is doing.

Alternatively, if you find yourself allergic to maths, you can do the following:
1. Create a long sprite, as long as the distance you want the object to orbit at. Its hotspot must be on the left side. Move its image point to the right.
2. Always set that sprite to the center.
3. Rotate the sprite around. You can use the "Rotate" behavior.
4. Always set your rotating object's position to the image point of long sprite.
5. Make the long sprite invisible.
6. Voila!
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

Post » Mon May 04, 2009 1:03 pm

Wow, thanks, man. That pretty much answered all my questions. :D
B
2
G
3
Posts: 10
Reputation: 870

Post » Mon May 04, 2009 4:16 pm

You could use the rotate behavior. Simply add the behavior to the sprite you want to rotate, then set its hotspot to the distance you want it to rotate. You can then add an always event to set its position to what you want it to rotate.
Image Image
B
161
S
48
G
90
Posts: 7,347
Reputation: 66,749

Post » Mon May 04, 2009 7:04 pm

Mipey's eventing above would be better than hot-spot fudging, as it would allow you to rotate the object independently of its orbit. For example, making an option unit in a shmup that would constantly orbit the player, but alway face forwards.

However, I would change two parts of it:

1. Rather than use 'Add 1 to Period', I would try to find some way to use TimeDelta, i.e. 'Add n*TimeDelta to Period. This would work based on how quickly you want it to orbit - 360*TimeDelta would rotate once every second, 180*TD once every two seconds, etc. You could even use a PV, say 'OrbitSpeed' to allow you to control this at runtime.

2. Instead of 'If Period = 360, etc.', I would use 'If Period is greater than 360, subtract 360 from period'. That way you would negate the risk of not exactly equaling 360 (which would be very likely using TimeDelta).

(untested - hope it works!)
B
2
S
2
G
3
Posts: 105
Reputation: 1,510

Post » Mon May 04, 2009 9:34 pm

It works! Cap example here:

viewtopic.php?f=16&t=3548
B
2
S
2
G
3
Posts: 105
Reputation: 1,510

Post » Tue May 05, 2009 2:07 am

Yeah there are about 10 different ways to do everything.
If you wanted to allow for rotation of the orbiter using a behavior, you could add a blank sprite, give it the rotation behavior, then place the hotspot on one end, and an imagepoint on the other. Then set the orbiting sprite's position to the blank's imagepoint, and set the blank to stay with the main sprite.
You could also change the radius on the fly by changing the dimensions of the blank.
Image Image
B
161
S
48
G
90
Posts: 7,347
Reputation: 66,749

Post » Tue May 05, 2009 11:49 am

Thanks all!

This really helped me out!!!
B
2
G
3
Posts: 10
Reputation: 870


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 5 guests