Search code examples
iosobjective-ccocos2d-iphone

How to continuously run my pendulum equation in cocos2d (Objective-C)?


I got a working equation for the rotation angle of my pendulum as seen in the code below:

const float gravity = 9.8;

float L = 0.01;

double CurrentTime = CACurrentMediaTime();

double angle = 120 * sin(sqrt((gravity/L)*CurrentTime));

But what I can't seam to figure out is how to make the sprite rotate continuously so that it acts just like a pendulum. I only make it move to the current value of the angle at the moment I click run, then it stopes when it reaches that position. Here is what I tried to do but only gave the result just explained:

[_claw runAction:[CCActionRepeatForever actionWithAction:[CCActionRotateTo actionWithDuration:3.0 angle:angle]]];

So how do I make it rotate to the angle value continuously, so that it swings back and forth? Any help is highly appreciated :D


Solution

  • Try this;

    id rotation = [CCActionRotateTo actionWithDuration:3.0 angle:angle];
    
    [_claw runAction:[CCActionRepeatForever actionWithAction:[CCSequence actions:rotation, [rotation reverse], nil]]];
    

    If reverse method not found

    id rotation = [CCActionRotateTo actionWithDuration:3.0 angle:angle];
    id reverseRotation = [CCActionRotateTo actionWithDuration:3.0 angle:_claw.rotation];
    [_claw runAction:[CCActionRepeatForever actionWithAction:[CCSequence actions:rotation, reverseRotation , nil]]];