简体   繁体   中英

Make object move in a circle

I'm a beginner at programming, and I've been trying to make an object orbit around another object (or just move in a circle). But I haven't succeeded very well. Any ideas?

You need some constants to specify radius and speed:

const float speed = 100.0f;
const float radius = 50.0f;

you also need some variable to store angle:

float angle;


- (void)updateObject:(NSTimeInterval)dt
{
    angle += speed * dt;
    angle = fmodf(angle, 360.0f);

    float x = cosf(DEGREES_TO_RADIANS(angle)) * radius;
    float y = sinf(DEGREES_TO_RADIANS(angle)) * radius;

    float newXPosition = _yourSprite.position.x + x;
    float newYPosition = _yourSprite.position.y + y;

    //Assign the values to your sprite
    _yourSprite.position = ...
}

Try connecting two nodes with SKPhysicsJointLimit , with the first node not movable (maybe not dynamic), set the linear damping of the second node to zero and disable gravitation forces on it. It also should not collide with any other object, of course.When the joint is stretched to its maximum and you apply an Impulse vertical to the connection between the two objects, the object should start orbiting around the other one.

I have not tested this one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM