Make an object orbit around another

This forum is currently in read-only mode.
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • 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

  • 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:

    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!
  • Wow, thanks, man. That pretty much answered all my questions.

  • 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.

  • 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!)

  • It works! Cap example here:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

  • Thanks all!

    This really helped me out!!!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)