如何在Sprite Kit中实现鼠标接头? [英] How to implement mouse joint in Sprite Kit?

查看:175
本文介绍了如何在Sprite Kit中实现鼠标接头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有工作的拖放功能实现在Cocos2d + Box2d为iOS编写。我需要将其移植到Sprite Kit。逻辑是非常基本的:

I have working implementation of drag and drop feature written in Cocos2d + Box2d for iOS. I need to port it to Sprite Kit. The logic is pretty basic:


  • 当用户触摸屏幕时,在手指下找到精灵

  • 在发现的精灵和场景的物理身体之间建立鼠标关节,在触摸移动时设置联合的目标位置触摸

  • ,将联合目标更新为新位置

  • 触摸结束时,删除联合

  • when user touching the screen, find sprite under the finger
  • create mouse joint between found sprite and the scene's physics body, set joint's target to position of touch
  • when touches moved, update joint's target to new position
  • when touches ended, remove the joint

一切都很好。我喜欢触摸结束时物理模拟的方式 - 拖动的形状具有速度取决于拖动速度和方向,所以当我在移动精灵时从屏幕上离开我的手指,它将继续沿着相同的方向移动,但现在受到重力和阻尼。

Everything is working quite well. I like the way physics is simulated when touches ends - the dragged shape has velocity dependent on dragging speed and direction, so when I left my finger from the screen while moving the sprite, it will continue moving in the same direction, but now affected by gravity and damping.

不幸的是,我无法使用Sprite Kit重现相同的效果。没有像鼠标这样的关节,但我尝试使用其他联合类型。我几乎成功与SKPhysicsJointFixed - 我正在创建新的精灵和它之间的联合和拖动的精灵,当触摸移动我移动新创建的精灵。不幸的是,它不像Cocos2d + Box2d中的鼠标关节那样工作,而在拖动子图形时,其速度总是等于零。所以每次从屏幕上离开我的手指,拖曳的小精灵立即停止,开始下降,受到重力的影响。无论拖动时移动手指多快,释放拖动形状后,其行为方式完全相同。

Unfortunately, I'm not able to reproduce the same effect using Sprite Kit. There is no such joint like mouse joint, but I have tried using other joint types. I almost succeeded with SKPhysicsJointFixed - I'm creating new sprite and joint between it and dragged sprite, when touches moving I'm moving the newly created sprite. Unfortunately it doesn't work like mouse joint in Cocos2d + Box2d - while dragging the sprite, its velocity always equals zero. So, every time I left my finger from the screen, the dragged sprite stops immediately and start falling affected by gravity. No matter how fast I move the finger when dragging, after releasing dragged shape, it behaves exactly the same way.

我的问题是如何在Sprite Kit中实现鼠标关节,或如何实现如上所述的拖放功能?

My question is how to implement mouse joint in Sprite Kit, or how to implement drag and drop feature that works like described above?

更新:
这是一个box2d鼠标联合示例,可以让您更清楚地了解我正在使用Sprite Kit实现什么:
http://blog.allanbishop.com/wp-content/uploads/2010/09/Box2DJointTutorial1.swf

推荐答案

我对iOS开发非常新鲜,所以我猜这可能不是最好的方法,但这是我如何解决它,它似乎工作相当顺利。

I'm pretty new to iOS development so I guess this might not be the best way, but this is how I solved it and it seems to work pretty smooth actually.

我正在使用 UIPanGestureRecognizer 来处理触摸事件。我在这个代码中的 didMoveToView:中设置了一个:

I'm using a UIPanGestureRecognizer to handle the touch event. I set this one up in my didMoveToView: with this code:

UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];

一个 UIPanGestureRecognizer 被传递给 handlePan:,我检查与 recognitionizer.state == UIGestureRecognizerStateEnded 如果触摸停止。这是我要释放物体,让它飞走的地方。为了做到这一点,我需要计算一些东西,首先我用这个代码获得手指/触摸的速度:

An UIPanGestureRecognizer is passed to handlePan: and I check with recognizer.state == UIGestureRecognizerStateEnded if the touched stopped. This is where I want to "release" the object and "let it fly away". For doing so I needed to calculate a few things, first of all I get the velocity of the finger/touch with this code:

CGPoint velocity = [recognizer velocityInView:recognizer.view];

(我使用相同的方法( handlePan:)与其他语句在触摸开始时获取正确的对象,并且通过将对象的位置设置为移动时的触摸位置,让对象在手指下方)

(I use the same method (handlePan:) with other statements for getting the right object when touch starts, and letting the object be under the finger all the time by setting the position of the object to the location of the touch when moving)

现在我知道速度,但是我仍然不知道需要多少力量来应用于对象。您应该能够通过以下公式计算力:Force = mass * acceleration。我们知道质量(object.physicsBody.mass),我们知道速度。为了获得加速度,我们需要时间,因为加速度=速度/时间。

Now I know the velocity, but I still don't know how much force I need to apply to the object. You should be able to calculate the force by this formula: Force = mass * acceleration. We know the mass (object.physicsBody.mass) and we know the velocity. To get the acceleration we need the time as well because acceleration = velocity / time.

在我的更新中:方法这就是每次渲染一个新框架时,都会计算出上一次框架将要渲染的时间之间的差异:

In my update: method that is called every time a new frame is to be rendered I calculate the difference between the last time a frame was about to be rendered by doing something like this:

self.delta = currentTime - self.lastTime;
self.lastTime = currentTime;

我现在可以计算出所需要的力量,将其拖动到。为此,我执行以下操作:

I now can calculate which force that is needed to get the object moving in the velocity I'm "dragging it in". To do this I do the following:

[self.currentNode.physicsBody applyForce:CGVectorMake(velocity.x/self.delta * self.currentNode.physicsBody.mass, -velocity.y/self.delta * self.currentNode.physicsBody.mass)];

我将速度与上一帧后的时间差分开( self) delta )以获得加速度,然后将其与对象的质量相乘以获得保持(或实际获得)对象在相同方向移动所需的力并以与之相同的速度移动被我的手指移动了。

I take velocity divided with the difference in time since last frame (self.delta) to get the acceleration, and then multiply it with the mass of the object to get the force needed to keep (or actually getting) the object moving in the same direction and in the same velocity as it was moved by my finger.

这是在为我工作,我希望它也有助于其他人,请告诉我,如果我有问题或如果有是一个更好的解决方案。到目前为止,我还没有找到任何本地解决方案。

This is working for me at the moment and I hope it helps other people too, and please tell me if I got something wrong or if there is a better solution. So far I have not found any "native solution".

这篇关于如何在Sprite Kit中实现鼠标接头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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