0
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)));

star上面是调用每一帧时沿窗口中心点旋转的精灵。锚点是星的中心。我怎样才能让星星也沿着自身中心旋转?

4

2 回答 2

1

您想要彼此绕着轨道运行的物体,以及在它们自己的轴上自旋/旋转的物体吗?像这样的东西?

旋转测试.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

旋转测试.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
于 2013-10-08T14:51:01.487 回答
0

旋转将始终围绕旋转,(0,0)因此您只需平移p要成为中心的点以(0,0)按角度旋转,然后再平移回来!

编辑:

所以如果你想有旋转轴,第一个是屏幕的中心,p第二个是你的星星的中心,q旋转角度PQ.

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
于 2013-10-08T12:25:32.507 回答