您想要彼此绕着轨道运行的物体,以及在它们自己的轴上自旋/旋转的物体吗?像这样的东西?
旋转测试.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