2014-11-24 66 views
0

SpriteKit的旋轉座標系統非常難以使用,無需多言。在SpriteKit旋轉座標系統中的視場中

在數學上,這是一場完全的災難。

我具有視場,X,使得最左邊的視圖是θ+ X,最右邊的是θ-Xθ是子畫面的zRotation。

如何檢查角度是否在另一個視野?

let λ be atan2(test.y - char.y, test.x - char.x) 
let θ be char.zRotation 
if λ in θ - x, θ + x 
    return true 

This makes no sense

回答

0

如果夾住角度PI和-PI之間那麼數字是有點簡單:

let PI=CGFloat(M_PI) 

var spriteAngle:CGFloat=0 
let fieldOfView:CGFloat=0.5 
var targetAngle:CGFloat=0 

while spriteAngle>PI { 
    spriteAngle=spriteAngle-2*PI 
} 
while spriteAngle < -PI { 
    spriteAngle=spriteAngle+2*PI 
} 
while targetAngle>PI { 
    targetAngle=targetAngle-2*PI 
} 
while targetAngle < -PI { 
    targetAngle=targetAngle+2*PI 
} 

let lowerBound=spriteAngle-fieldOfView 
let upperBound=spriteAngle+fieldOfView 

if lowerBound <= targetAngle && targetAngle <= upperBound { 
    //angle in range 
} 
0

下面是如何確定一個例子,如果一個節點內另一個節點的視野。如果將角度轉換爲[0,2 * pi),則相當簡單。

// 1/2 of the field of view 
    #define kFieldOfViewDiv2 (M_PI/8) 

    // Calculate differences in x and y between the sprites 
    CGFloat dx = sprite2.position.x - sprite.position.x; 
    CGFloat dy = sprite2.position.y - sprite.position.y; 
    // Determine angle of sprite2 relative to sprite 
    CGFloat theta = atan2(dy, dx); 
    // Theta in [0, 2*pi) 
    if (theta < 0) { 
     theta = theta + M_PI*2; 
    } 
    // Alpha in (-2*pi, 2*pi) 
    CGFloat alpha = fmod(sprite.zRotation,2*M_PI); 
    // Alpha in [0, 2*pi) 
    if (alpha < 0) { 
     alpha = alpha + M_PI * 2; 
    } 
    // Calculate |theta-alpha| 
    CGFloat diff = fabs(theta-alpha); 
    // Handle wrap near 0 degrees 
    if (diff > M_PI) { 
     diff = 2*M_PI - diff; 
    } 
    // Field of view test 
    if (diff <= kFieldOfViewDiv2) { 
     NSLog(@"In field of view"); 
    }