English 中文(简体)
of2d
原标题:Find an angle of vector using Cocos2d

I do not find what s the problem. I am using ccpToAngle() to find an angle of vector but can t get proper angle. My ball is launched/initial position at cpp(170,40) and using swipe i want to throw it.
I wrote to find an angle,

 -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {

    CGPoint location = [touch locationInView: [touch view]];
    CGPoint vector = ccpSub(location, ccp(170 ,40));
    normalVector = ccpNormalize(vector);
    float angleRads = ccpToAngle(normalVector);
    float angle = CC_RADIANS_TO_DEGREES(angleRads);
   }

但是,如果一刀阔.,就会增加角。 我认为,从同一线/方向来看,gle角应当每次都一样。 但是,在这里,我只看到70到90度的角,如果 more子更长,角就接近60,66度。

我想从0到180度。 是否有可能将ccp(170,40)视为轴源和化合物;在Cocos2d找到病媒的视角?

增 编 提前。

最佳回答

如果你想找到一种 s子,你就必须在这里使用三角。

有些人可以帮助你。

- ( void ) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
   locationTouchBegan = [touch locationInView: [touch view]];
    //location is The Point Where The User Touched
    locationTouchBegan = [[CCDirector sharedDirector] convertToGL:locationTouchBegan];
    //Detect the Touch On Ball
    if(CGRectContainsPoint([ball boundingBox], locationTouchBegan))
    {
        isBallTouched=YES;
    } 

}

因此,你如何发现球受到触动。

现在,我们可以计算方向。

- ( void ) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
   locationTouchEnded = [touch locationInView: [touch view]];
    locationTouchEnded = [[CCDirector sharedDirector] convertToGL:locationTouchEnded];
    [self calculateDirections];
}

这就是我们如何计算方向。

-(void)calculateDirections
{
    if (isBallTouched==YES )
    {
        perpendicularBig = screenSize.height - ball.position.x;
        baseSmall = screenSize.width/2 - locationTouchEnded.x; // if the ball is at center.
        perpendicularSmall=locationTouchEnded.y - ball.position.x;
        baseBig=(perpendicularBig*baseSmall)/perpendicularSmall;
        endPoint=ccp(screenSize.width/2 - baseBig,320); //320 can replace by the value of end point y where you want to end the ball run. 
        [self moveBall:3.0f];
    }
}
-(void)moveBall:(float)duration
{

    [ball runAction:[CCJumpTo actionWithDuration:duration position:ccp(endPoint.x,endPoint.y) height:20 jumps:1]];
    isBallTouched = NO;
}
问题回答

暂无回答




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签