【问题标题】:calculating the trajectory of an object in SpriteKit when an impulse is applied to it在 SpriteKit 中计算施加脉冲时对象的轨迹
【发布时间】:2018-01-27 23:51:46
【问题描述】:

我想制作一条由小球组成的轨迹线,说明物体在冲动后会去哪里,有点像《愤怒的小鸟》中的内容。我做了一些研究,似乎 spriteKit 中的物理计算就像在现实生活中使用 si 单位和其他东西一样。使用位移公式 Δx = ViΔt + 1/2aΔt^2 我使用这个公式来设置物体在冲击后的九个球的位置。

这是我迄今为止尝试过的:

func calculateTrajectory(mass: CGFloat, force1: CGFloat, force2: CGFloat){

    for i in 0...8{

        var x = CGFloat()
        var y = CGFloat()

        let a1 = CGFloat(force1/mass)//I am taking the force applied to the object and calculating the acceleration from that 
        let a2 = CGFloat((force1/mass) - 10)//I am subtracting 10 for y because of the -10m/s/s of gravity
        let t = CGFloat((i/16)^2)//I am taking the number that the ball is in the line and dividing it by 16 so the line can show where the object will be from 0-0.5 secnds

        x = 0.5 * a1 * t
        y = 0.5 * a2 * t

        trajectory.copiedNodes[i].position = CGPoint(x:ball.position.x + x, y: ball.position.y + y)

    }



}

我也用这个来做一个后拉弹弓之类的东西。

calculateTrajectory(mass: (ball.physicsBody?.mass)!, force1: startPositionDrag.x - movedLocation.x, force2: startPositionDrag.y - movedLocation.y)

计算的力量与我在触球结束时用来投篮的冲动相同:

ball.physicsBody?.applyImpulse(CGVector(dx:startPositionDrag.x - endPositionDrag.x, dy:startPositionDrag.y - endPositionDrag.y ))

当我计算这个时,球并不靠近物体去的地方,当我通过将 i 除以更多来使球的时间变化更短时,即使我正在计算它们的位置,球也会变得更远在更少的时间之后。有什么办法我做错了吗?我对 CGFloat 的转换是否错误?请帮帮我。

【问题讨论】:

  • 我丢失了该帐户的密码,所以我使用了另一个帐户。请给我评论并在 cmets 中标记我。
  • 我认为你的物理学坏了。你不是用冲动而不是用力吗?然后缺少考虑初始速度的术语vt
  • 冲动只是施加速度吗?在文档中它说它以每秒牛顿为单位,所以那不是一种力量吗?然后它应该从 0 开始,然后因为力而加速……对吧?
  • 那行得通,但现在这条线是正确的形状,根据重力预测弯曲,但球没有沿着这条线走。它部分下降,然后回落。球的运动似乎受到其质量的影响,但方程式表明它不应该受到影响。我需要将质量考虑到这个方程中吗?如果有,怎么做?
  • @Jean-BaptisteYunès 你有什么想法吗?

标签: swift sprite-kit game-physics


【解决方案1】:

要将速度从 m/s 转换为每秒像素,您需要将 sprite kit 速度乘以 150。与加速度相同。

【讨论】: