1
votes

This has really been a hassle, but I have two sprites, both of them are 17 pixel long arms. Both of them have anchor points at ccp(0.5f,0.0f); and what I want is for, when arm1 rotates, for arm2's CGPoint to be equal to the opposite end of the anchorpoint of arm1. Like, at a 45 degree angle, the CGPoint would just be ccp (arm1.position.y, arm1.position.x + 17);

So I update the rotation for arm1 in my ccTime function, and it calls another method to do the math for the angle rotation. Basicall what happens is... arm2 rotates reaaaally fast around in the correct circular area, meaning something is right, but the rotation is just super fast.

-(void) callEveryFrame:(ccTime)dt{
    //if the user is holding down on the screen, arm1 rotates.
    if(held == true){
        _theArm2.position = [self fixAngle];
        timer = timer + .0166; //this gets reset after touchesEnded.
        _theArm1.rotation = _theArm1.rotation - (timer * 10);
}
-(CGPoint)fixAngle{
    CGFloat theAngle = _theArm1.rotation;
//I'm not to sure how the degrees works in cocos2d, so I added 90 to the angle of rotation's original position, and it works until the rotation variable changes.
    CGFloat thyAngle = theAngle + 90; 

    CGFloat theMathx = (17*cosf(thyAngle)); //17 is the length of the arm
    CGFloat theMathy = (17*sinf(thyAngle));
    theMathx = theMathx + 100;//these 2 updates just change the position, because arm1's 
    theMathy = theMathy + 55; //CGpoint is located at ccp(100,57)

    return CGPointMake(theMathx, theMathy);
}

Sorry if the code is... bad. I'm relatively new to programming, but everything works except the stupid arm2 likes to rotate really fast in a circle.

I will love whoever solves my problem for the rest of my/their lives.

1
I think that the rotation is in radians, not degrees. Id start there. - hspain

1 Answers

3
votes

EDIT:

Per discussion on this thread, it looks like you are using degree where radians should be used, but not where I thought. Try this:

CGFloat theMathx = (17*cosf(CC_DEGREES_TO_RADIANS(thyAngle))); //17 is the length of the arm
CGFloat theMathy = (17*sinf(CC_DEGREES_TO_RADIANS(thyAngle)));

Try using this to add 90 degrees to the rotation. Cocos2D uses radians instead of degrees: