Joints break when zooming

Mikayil Abdullayev

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.

This is for the normal state: Here's the normal state

Here's when zoomed in: Zoomed in

And here's when zoomed out: 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?

EDIT: 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]];
 }

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.

Mikayil Abdullayev

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. 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

  2. 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. :))

  3. 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.

    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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Zooming out of my world: Joints and their positions are not adjusted

From Dev

Div not expanding in width when zooming in

From Dev

Iphone/Ipad crashes when zooming

From Dev

Scrollbar when zooming in html page

From Dev

Openlayers - Marker disappears when zooming

From Dev

Div not expanding in width when zooming in

From Dev

Keep zooming (or zooming out) when mouse key is pressed

From Dev

Absolute html layout is broken when zooming

From Dev

Calculate a dynamic iteration value when zooming into a Mandelbrot

From Dev

D3 prevent distortion when zooming

From Dev

Chrome will increase the font size when zooming out

From Dev

Gnuplot - Graphic appears and disappears when zooming

From Dev

Keep website picture size when zooming out

From Dev

HTML: Background elements disappear when zooming out

From Dev

Delay between basemap and vector layer when zooming

From Dev

Page changing size on iOS when zooming

From Dev

Tablix border inconsistent when zooming into ssrs report

From Dev

Bottom: 0 - weird behavior when zooming

From Dev

Elements moving when resizing or zooming browser window

From Dev

Keep selected div centered when zooming

From Dev

Gnuplot - Graphic appears and disappears when zooming

From Dev

Javascript animations not working properly when zooming in and out

From Dev

Zooming works only when hovering on the dots in scatterplot

From Dev

Bootstrap Grid Alignment Issue When Zooming

From Dev

When to break down VHDL?

From Dev

justifiedGallery break when reloaded

From Dev

three.js - how to limit the zooming so that object does not break due to camera

From Dev

Skobbler Annotation disappears from map when zooming out on Android

From Dev

d3js: zooming when there are two y axes

Related Related

  1. 1

    Zooming out of my world: Joints and their positions are not adjusted

  2. 2

    Div not expanding in width when zooming in

  3. 3

    Iphone/Ipad crashes when zooming

  4. 4

    Scrollbar when zooming in html page

  5. 5

    Openlayers - Marker disappears when zooming

  6. 6

    Div not expanding in width when zooming in

  7. 7

    Keep zooming (or zooming out) when mouse key is pressed

  8. 8

    Absolute html layout is broken when zooming

  9. 9

    Calculate a dynamic iteration value when zooming into a Mandelbrot

  10. 10

    D3 prevent distortion when zooming

  11. 11

    Chrome will increase the font size when zooming out

  12. 12

    Gnuplot - Graphic appears and disappears when zooming

  13. 13

    Keep website picture size when zooming out

  14. 14

    HTML: Background elements disappear when zooming out

  15. 15

    Delay between basemap and vector layer when zooming

  16. 16

    Page changing size on iOS when zooming

  17. 17

    Tablix border inconsistent when zooming into ssrs report

  18. 18

    Bottom: 0 - weird behavior when zooming

  19. 19

    Elements moving when resizing or zooming browser window

  20. 20

    Keep selected div centered when zooming

  21. 21

    Gnuplot - Graphic appears and disappears when zooming

  22. 22

    Javascript animations not working properly when zooming in and out

  23. 23

    Zooming works only when hovering on the dots in scatterplot

  24. 24

    Bootstrap Grid Alignment Issue When Zooming

  25. 25

    When to break down VHDL?

  26. 26

    justifiedGallery break when reloaded

  27. 27

    three.js - how to limit the zooming so that object does not break due to camera

  28. 28

    Skobbler Annotation disappears from map when zooming out on Android

  29. 29

    d3js: zooming when there are two y axes

HotTag

Archive