如何制作一个在spritekit swift中以角度旋转的正弦?

问题描述:

我想在我的游戏中出现一种围绕着屏幕的声波。我能够做出正弦的直线,但是我怎样才能使正弦旋转20度?例如,从左下角到右上角?如何制作一个在spritekit swift中以角度旋转的正弦?

 let pi = CGFloat.pi 
     let wave = Waves[0] 
     let x = path.currentPoint.x 
     let c = (2 * pi)/(wave.Wavelength * 30) 
     let a = 30 * wave.Amplitude 

     let y = a * sin(c * x) 

     path.addLine(to: CGPoint(x: x + 2, y: y)) 
+0

您需要包含更多信息,您如何使用路径?作为SKShapeNode?只需旋转节点 – Knight0fDragon

您可以将旋转的仿射变换计算点位置

xnew = xbase + (x - xbase) * Cos(rot_angle) - (y - ybase) * Sin(rot_angle) 
ynew = ybase + (x - xbase) * Sin(rot_angle) + (y - ybase) * Cos(rot_angle) 

这里(xbase, ybase) - (你的情况可能是0,0)旋转中心,rot_angle的坐标 - 旋转的角度,你的情况20 * Pi/180

也许你的图形库已经包含仿射变换的功能,在这种情况下,只需要形成变换矩阵并应用它。

,您可以拨打UIBezierPath与2 Bézier curves相当准确地近似正弦波:

/// Convert degrees to radians 
func degreeToRadian(_ angle: CGFloat) -> CGFloat { 
    return angle * CGFloat.pi/180.0 
} 

let path = sinePath(size: CGSize(width: 200.0, height: 100.0)) 
path.apply(CGAffineTransform(rotationAngle: degreeToRadian(20))) 

我得到了baseX常数:

/// Make a path that approximates a peak-to-peak sine wave 
func sinePath(size: CGSize) -> UIBezierPath { 
    let path = UIBezierPath() 

    // path starts at 0,0 
    path.move(to: CGPoint.zero) 

    let halfWidth = size.width/2.0 

    // base x-values for control points 
    // baseX approximates the best fit Bézier curve for a sine wave 
    let baseX: = CGFloat(0.3642124232) 
    let firstX = halfWidth * baseX 
    let secondX = halfWidth * (1.0 - baseX) 

    // curve from start peak to trough 
    path.addCurve(to: CGPoint(x: halfWidth, y: size.height), 
       controlPoint1: CGPoint(x: firstX , y: 0), 
       controlPoint2: CGPoint(x: secondX, y: size.height)) 

    // curve from trough to end peak 
    path.addCurve(to: CGPoint(x: size.width, y:0), 
       controlPoint1: CGPoint(x: size.width - secondX, y: size.height), 
       controlPoint2: CGPoint(x: size.width - firstX , y: 0)) 
    return path 
} 

然后你可以用一个CGAffineTransform像这样旋转它从这个问题:

How to approximate a half-cosine curve with bezier paths in SVG?