0
votes
star.rotation += star.speed;
float angleRadian = star.rotation * PI_DIV_180;
star.position = ccp(windowSize.width/2 + (star.radius * sinf(angleRadian)), windowSize.height/2 + (star.radius * cosf(angleRadian)));

Above is rotating star sprite along window center point when calling for every frame. Anchor point is center of star. How can I make the star to rotate along self center as well?

2
self center, you mean rotate the star around its center? Isn't that what you're doing with star.rotation += star.speed?LearnCocos2D
yes I mean around its center. It's not what I'm doing now. I'm currently performing translation AND rotation in order to rotate star around windowSize center. Like Earth is rotating around sun and around its own axis is what I'm trying to achieve.Pablo
If you translate the star to (x,y), does its center go to (x,y)?Beta
@Beta the center relative to window goes to x,y. The center relative to star remains unchanged.Pablo
perhaps I'm misunderstanding you, but it seems like you are trying to use the star's rotation as a part of it's position, but you want to decouple the relationship between the rotation and position... so why don't you just create another variable that represents it's relativeAngle or similar, and use rotation as it is, which is the (local) rotational angle?Mark

2 Answers

1
votes

You want objects that orbit around each other, as well as spin/rotate on their own axis? Something like this?

RotationTest.h

@interface RotationTest : CCLayer
{
    CCSprite *star;
    CCSprite *planet;
    CCSprite *satellite;

    float dist_star_planet;
    float dist_planet_satellite;

    float relative_angle_star_planet;
    float relative_angle_planet_satellite;
}

@end

RotationTest.m

#import "RotationTest.h"

@implementation RotationTest

-(id) init
{
    if( (self=[super init]))
    {
        CGSize s = [[CCDirector sharedDirector] winSize];

        star = [CCSprite spriteWithFile:@"Icon.png"];
        planet = [CCSprite spriteWithFile:@"Icon.png"];
        satellite = [CCSprite spriteWithFile:@"Icon.png"];

        [self addChild: star];
        [self addChild:planet];
        [self addChild:satellite];

        star.position = ccp(s.width / 2.0f, s.height / 2.0f);
        planet.position = ccpAdd(star.position, ccp(100.0f, 0.0f));
        satellite.position = ccpAdd(planet.position, ccp(50.0f, 0.0f));

        star.scale = 0.5f;
        planet.scale = 0.35f;
        satellite.scale = 0.2f;

        dist_star_planet = ccpDistance(planet.position, star.position);
        dist_planet_satellite = ccpDistance(satellite.position, planet.position);

        relative_angle_star_planet = 0.0f;
        relative_angle_planet_satellite = 0.0f;

        [self scheduleUpdate];
    }
    return self;
}

-(void) update:(ccTime) dt
{
    star.rotation += 1.0f; // degs
    planet.rotation -= 1.0f;
    satellite.rotation += 2.0f;

    relative_angle_star_planet += 0.01f; // rads
    relative_angle_planet_satellite -= 0.01f;

    planet.position = ccpAdd(star.position, ccp(dist_star_planet * sinf(relative_angle_star_planet), dist_star_planet * cosf(relative_angle_star_planet)));
    satellite.position = ccpAdd(planet.position, ccp(dist_planet_satellite * sinf(relative_angle_planet_satellite), dist_planet_satellite * cosf(relative_angle_planet_satellite)));
}

@end
0
votes

A rotation will always rotate around (0,0) so you just have to translate the point p you want to be the center to (0,0) roate by your angle and then translate back!

Edit:

So if you want to have to rotation axis, the first being the center of your screen p and the second beeing the center of your star q with rotation angles P and Q.

Set Star to (0,0) rotate by Q
Translate Star by the distance you want around p
Rotate by P
Translate (0,0) to p