【问题标题】:Chipmunk 2D Setting Oribital Velocity FunctionChipmunk2d 设置轨道速度函数
【发布时间】:2011-06-03 17:15:26
【问题描述】:

因此,我使用 Planet.c 演示为我的 2D iPhone 游戏设置了速度函数,该游戏涉及环绕行星的作品。然而,下面的代码设置了很好的轨道,但只在屏幕的原点,即屏幕的左下角:

// Point mass position
cpVect p = body->p;

cpFloat sqdist = cpvlengthsq(p);
cpVect g = cpvmult(p, -gravityStrength / (sqdist * cpfsqrt(sqdist)));

cpBodyUpdateVelocity(body, g, damping, dt);

我尝试简单地添加中心恒星位置的位移矢量(在本例中为屏幕中心 cpv(160, 240)),但这只是将它发送到一个奇怪的轨道上。我一直在尝试各种尝试让我的身体的位置矢量和径向矢量相对于中心而不是原点,但我无法让它完全正常工作。我的想法是这样的:

// Sun position
cpVect disp = cpv(160, 240);

// Point mass position
cpVect p = body->p;

// Point mass relative to Sun
p = cpvsub(disp, p);

cpFloat sqdist = cpvlengthsq(p);
cpVect g = cpvmult(p, -gravityStrength / (sqdist * cpfsqrt(sqdist)));

cpBodyUpdateVelocity(body, g, damping, dt);

有什么想法吗?谢谢!

【问题讨论】:

    标签: iphone velocity chipmunk orbital-mechanics


    【解决方案1】:

    知道了!正如我所怀疑的,这只是获得正确的向量减法的问题,即中心减去位置向量。干得好!

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多