Chipmunk 2D 设置轨道速度功能
因此,我使用 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);
有什么想法吗?谢谢!
So I've used the Planet.c demo to set up a velocity function for my 2D iPhone game that's in the works that involved orbiting planets. However the following code sets up nice orbits but only does so around the origin of the screen i.e. the lower left corner of the screen:
// Point mass position
cpVect p = body->p;
cpFloat sqdist = cpvlengthsq(p);
cpVect g = cpvmult(p, -gravityStrength / (sqdist * cpfsqrt(sqdist)));
cpBodyUpdateVelocity(body, g, damping, dt);
I've tried simply adding a displacement vector of the central star's position (in this case the center of the screen cpv(160, 240)) but that just sends it off in a strange orbit. I've always tried a variety of trying to get my body's position vector and radial vector in relation to the center as opposed to the origin but I can't get it to work quite right. I'm thinking something along the lines of:
// 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);
Any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
知道了!正如我怀疑的那样,这只是获得正确的向量减法的问题,即中心减去位置向量。干得好!
Got it! As I suspected was just a matter of getting the right vector subtractions namely the center minus the position vector. Well done!