如何使用Sprite Kit在2D游戏中模拟z轴上的重力 [英] How to simulate Gravity in z-axis in a 2D game with Sprite Kit

查看:136
本文介绍了如何使用Sprite Kit在2D游戏中模拟z轴上的重力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在iOS 7上使用Sprite Kit编写2D球类游戏,目前正在努力进行一次物理模拟.

I'm writing a 2D ball game with sprite kit on iOS 7 and currently struggling on one physic simulation.

解释预期的行为:如果将球放入茶杯中,它会绕圈旋转,速度降低,最终停在茶杯的中心.

To explain the expected behavior: if a ball is dropped into a tea cup, it will circle around, loosing speed and finally stand still in the center of the cup.

我尝试使用重力将其存档,但Sprite Kit中的重力仅适用于垂直的X和Y轴,不适用于Z轴.我还尝试通过在beginContact上使用小型物理物体切换重力值来使用水平重力,具体取决于茶杯中的当前球位置.但是一些联系被删除了,结果看起来很不现实.

I've tried to archive this with gravity, but gravity in sprite kit only applies to vertical X and Y axis, not Z-axis. I also tried to use level gravity by switching gravity values with small physic bodies on beginContact depending on the current ball position in the tea cup. But some contacts are dropped and the result is far away to look realistic.

我认为我需要在update:方法中解决此问题,但是我不知道该走哪条路. 任何建议都非常欢迎,我需要指出的是,我不是数学专家,请解释您的发展道路. :-)

I think I need to solve this in the update: method, but I have no idea which way to go. Any advice greatly welcome and I need to mention that I'm not an expert on math, please explain your path to go. :-)

推荐答案

由于SpriteKit中没有对此行为的内置支持,因此与其尝试破解现有功能以获取所需内容,不如说是更好的选择在您的x,y 2D世界中集成一些已发布的2D物理公式.我认为模拟磁或归位行为之类的方法可能是正确的.

Since there's no built-in support for this kind of behavior in SpriteKit, rather than trying to hack existing functions to get what you want, you're probably better off integrating some published 2D physics formulas in your x,y 2D world. I would think that something like simulating magnetic or a homing behavior might be right for this.

一个简单的例子是(在场景的-update:方法中):

A simple example would be something like (in the scene's -update: method):

 CGFloat strength = 0.5; //(some scaling value)
 CGPoint ballLocation = ball.position;
 CGPoint cupLocation = cup.position;
 [ball.physicsBody applyForce:CGVectorMake((cupLocation.x - ballLocation.x) * strength, 
                                           (cupLocation.y - ballLocation.y) * strength)];

这篇关于如何使用Sprite Kit在2D游戏中模拟z轴上的重力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆