缩放时关节断裂 [英] Joints break when zooming

查看:80
本文介绍了缩放时关节断裂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在排练Sprite Kit,遇到一个非常奇怪的问题.缩放(更改比例)父节点时,由SKPhysicsJointPin连接在一起的实体逐渐彼此分离,然后关节断开.让我向您展示这些图像.

I've been rehearsing Sprite Kit lately and I've come across a very strange problem. When zooming (changing the scale of) the parent node, bodies that are joined together by SKPhysicsJointPin separate from each other gradually and then joints break. Let me show you the images.

这是正常状态:

在这里放大时:

这是缩小时的状态:

And here's when zoomed out:

如果您问我如何连接实体:我将棕色的木棍连接到蓝色节点中心的蓝色节点上.知道我的问题是什么吗?

If you ask how I join bodies: I join the brown sticks to the blue nodes on the center of the blue nodes. Any ideas what my problem is?

我最近发现关节没有断裂,并且当连接体不是动态的时,一切都按预期工作.因此,例如,如果我使用[SKPhysicsBody bodyWithEdgleLoopF​​romRect]而不是 [SKPhysicsBody bodyWithRectangleOfSize]为一个精灵创建物理物体,没有问题.但是我需要身体是动态的.

I've recently found out that joints don't break and everything works as expected when the joining bodies are not dynamic. So for example, if I use [SKPhysicsBody bodyWithEdgleLoopFromRect] instead of [SKPhysicsBody bodyWithRectangleOfSize] to create physics body for a sprite, there's no problem. But I need the bodies to be dynamic.

这是我用来将物理附加到节点的代码.当然,这些都是动态完成的.为了简洁起见,我只是硬编码.

Here's the code that I use to attach physics to nodes. Of course it's all done dynamically. I just hard coded for brevity.

 -(void)attachPhysics{
    //fixedComponentLeft & fixedComponentRight are two SKSprites
    fixedComponentLeft.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:fixedComponentLeft.frame.size.width];
    fixedComponentRight.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:fixedComponentLeft.size.width];
    beam1.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:beam1.size];
    joiningBody.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:joiningBody1.size.width];
    [self.scene.physicsWorld addJoint:[SKPhysicsJointPin jointWithBodyA:fixedComponentLeft.physicsBody bodyB:beam1.physicsBody anchor:fixedComponentLeft.position]];
    [self.scene.physicsWorld addJoint:[SKPhysicsJointPin  jointWithBodyA:joiningBody.physicsBody bodyB:beam1.physicsBody anchor:beam1.endPoint]];
    beam2.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:beam2.size];
    [self.scene.physicsWorld addJoint:[SKPhysicsJointPin  jointWithBodyA:joiningBody.physicsBody bodyB:beam2.physicsBody anchor:beam2.position]];
    [self.scene.physicsWorld addJoint:[SKPhysicsJointPin jointWithBodyA:fixedComponentRight.physicsBody bodyB:beam2.physicsBody anchor:beam2.endPoint]];
 }

在上面的代码中,beam1和beam2是SKSpriteNode子类的实例.默认情况下,锚点为(0,0.5),我添加了一个名为endPoint的属性,该属性用作精灵上最右边的边缘点.

On the above code, beam1 and beam2 are instances of subclass of SKSpriteNode. By default the anchor point is (0,0.5) and I've added a property called endPoint which serves as the rightmost edge point on the sprite.

推荐答案

事实上,这不是答案,但我认为我不打算在此使用注释区域,因为问题足够大,无法容纳.我应该已经猜到问题是由以下事实引起的:绘制元素的节点和添加其实体的物理世界具有不同的坐标系.这是因为物理世界属于场景,这意味着无论场景中添加了多少个子节点,通常只有一个物理世界,连接到不同子节点上不同节点的所有实体共享同一世界.我不知道这是否好.所以这是我们可以尝试的事情:

In fact this is not an answer but I thought I'd not use the comment area for this, as the issue is big enough to not accommodate there. I should have guessed that the problem was caused by the fact that the node where the elements are drawn and the physics world their bodies are added to have different coordinate systems. It's because physics world belong to the scene and this means there's typically only one physics world no matter how many child nodes are added to the scene, all the bodies attached to different nodes on different child nodes share the same world. I don't know whether it's good or not. So here are the things we can try:

  1. 缩放场景本身而不是子节点:尝试时,毫不奇怪我遇到了很大的问题.由于场景通常是游戏中所有节点的根节点,因此这意味着将相应地按比例放大或缩小所有内容.这也包括控制盒.所以,这不是一个好主意

  1. Scale the scene itself instead of the child nodes: When I tried it, not surprisingly I had a very big problem. As scene is typically the root node for all the nodes in a game, this means everything will be scaled up or down accordingly. This includes the control hud too. So, not a good idea

您可以尝试对SKNode进行子类化,以便它实现自己的SKPhysicsWrold实例并将所有内容添加到该世界中:不幸的是,由于某些原因,Apple使其无法拥有自己的物理世界.是的,您可以将关节添加到所需的任何世界.但是关节需要身体共享同一世界.我记得从纯净的,未包装的Box2D中,身体也需要添加到物理世界中.因此,我想苹果公司已经做到了这一点,当您设置物理物体时,它会被添加到scene.physicsWorld中.而且,如果尝试将关节添加到自定义创建的物理世界中,您将收到Box2D错误,指出关节的能力小于关节的数量. :))

You can try to subclass SKNode so that it implements its own SKPhysicsWrold instance and add everything to that world: Unfortunately, for some reasons Apple made it impossible to have your own physics world. Yes, you can add your joints to any world you want. But joints need bodies to share the same world. From pure, unwrapped Box2D I remember bodies also needed adding to a physics world. So I assume, apple has made this automatic, when you set the physics body, it gets added to the scene.physicsWorld. And if you try to add joints to a custom created physics world, you're going to get a Box2D error stating that joint capacity is less than the joint count. :))

唯一现实的方法似乎是重建物理物体和关节.但这是一个真正的痛苦,我也没有成功.除了潜在的性能影响外,这种方法还需要非常准确地重新安排身体和关节的运动顺序,设置其先前的速度等.

The only realistic way seems to be to rebuild the physics bodies and joints. But this is a real pain and I have not been successful with it either. Besides a potential performance impact, this approach needs a very accurate order of recreation of bodies and joints, setting their previous velocities and etc.

因此,在网上搜索了20天以上却没有找到真正可行的解决方案后,我可以说这是一个真正的问题,我认为Apple需要考虑重新考虑,或者至少为其提供良好的解决方案.

So, having searched the net for 20+ days and not found a real working solution I can say that this is a real problem that I think Apple needs to consider revisiting, or at least offer good solutions for.

这篇关于缩放时关节断裂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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