在自我中心周围旋转物体?

时间:2013-10-08 09:56:47

标签: ios math cocos2d-iphone linear-algebra

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精灵。锚点是恒星的中心。我怎样才能使恒星沿着自身中心旋转?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

旋转将始终围绕(0,0)旋转,因此您只需将想要成为p的中心的点(0,0)转换为您的角度,然后翻译回来!

编辑:

因此,如果你想要旋转轴,第一个是屏幕的中心p,第二个是你的明星q的中心,旋转角度为P和{ {1}}。

Q