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
子画面。锚点是星体的中心。如何使星星也沿着自心旋转?
您想要围绕彼此旋转的对象,以及沿自己的轴旋转/旋转?像这样的东西?
旋转测试.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
旋转
将始终围绕(0,0)
旋转,因此您只需平移p
要成为中心的点,就可以(0,0)
的角度旋转,然后平移回来!
编辑:
因此,如果您想旋转轴,第一个是屏幕的中心p
,第二个是星星q
的中心,旋转角度P
和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