Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否也围绕自中心旋转对象?_Ios_Math_Cocos2d Iphone_Linear Algebra - Fatal编程技术网

Ios 是否也围绕自中心旋转对象?

Ios 是否也围绕自中心旋转对象?,ios,math,cocos2d-iphone,linear-algebra,Ios,Math,Cocos2d Iphone,Linear Algebra,在调用每一帧时,上面是沿窗口中心点旋转的星形精灵。锚点是恒星的中心。如何使恒星也沿着自身中心旋转?旋转总是围绕(0,0)旋转,所以你只需将点p转换为(0,0)的中心,然后按你的角度旋转,然后再转换回来 编辑: 因此,如果你想旋转轴,第一个是屏幕的中心p,第二个是恒星的中心q,旋转角度p和q star.rotation += star.speed; float angleRadian = star.rotation * PI_DIV_180; star.position = ccp(windowS

在调用每一帧时,上面是沿窗口中心点旋转的
星形
精灵。锚点是恒星的中心。如何使恒星也沿着自身中心旋转?

旋转总是围绕
(0,0)
旋转,所以你只需将点
p
转换为
(0,0)
的中心,然后按你的角度旋转,然后再转换回来

编辑:

因此,如果你想旋转轴,第一个是屏幕的中心
p
,第二个是恒星的中心
q
,旋转角度
p
q

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

您希望对象围绕彼此旋转,以及围绕自己的轴旋转/旋转?像这样的

旋转试验

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
旋转试验

@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

自中心,你是说绕着中心旋转恒星?这不就是你对恒星所做的吗。旋转+=恒星。速度?是的,我的意思是围绕它的中心。这不是我现在正在做的。我目前正在执行平移和旋转,以便围绕WindowsSize中心旋转星形。就像地球绕着太阳旋转,绕着它自己的轴旋转,这就是我想要达到的目标。如果你把恒星转换成(x,y),它的中心会转到(x,y)吗?@Beta相对于窗口的中心会转到x,y。相对于恒星的中心保持不变。也许我误解了你,但你似乎试图将恒星的旋转作为其位置的一部分,但你想分离旋转和位置之间的关系。。。那么,为什么不创建另一个变量来表示它的相对角度或类似的角度,并按原样使用旋转,即(局部)旋转角度?我不知道我是否理解。旋转将围绕精灵的中心旋转,因为其锚点默认设置为0.5、0.5。如果你能给我一些代码的例子,这将是更容易理解。是的,等等,尝试找到sth1@Pablo等等,你基本上想要一颗自转的恒星。然后比林绕着屏幕中心旋转?是的,这就是我想要的。现在它沿着屏幕的中心旋转,始终朝向中心。我希望恒星也绕着它的轴线旋转。等等,你是说如果地球是你屏幕的中心。从地球上看,这颗星只是从同一侧看到的?就像真实的月亮一样?没错。让我弄清楚这一点,看看是否可以将每颗星的位置和旋转设置为我的Box2D天体。我在那里使用的是
SetTransform(pos,angle)
#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