1

I am trying to figure out what is the best way to draw a line between two SKNodes in SpriteKit, nodes that move each frame. [Example sketch below... the red line is what I want to draw]

enter image description here

I can draw a line. At the moment I override the update call and every frame I detect the position of the two nodes (P1 and P2), and then simply draw a line [using an SKShapeNode and creating the path that goes from P1 to P2 then setting the shape node's path to it...].

It works and it performs well. However, that is handling two nodes, one line that visually connects them. What I am going to need is two draw multiple lines between multiple nodes. I feel like it'll be cumbersome to do so for N nodes to loop through each case every frame and remove and redraw lines.

enter image description here

I am wondering if there is a way to leverage physics body and joint to "add" a line between two nodes once and then have sprit kit handle the updates as the nodes move.

So, what is the best way to show lines between nodes that moves around every frame instead of having to loop through them each frame to redraw the line nodes as I get more nodes to handle?

zumzum
  • 17,984
  • 26
  • 111
  • 172
  • You can certainly use an `SKPhysicsJoint` Pin - that's probably the first thing to look at. https://developer.apple.com/documentation/spritekit/skphysicsjoint – Steve Ives Apr 24 '19 at 07:30
  • using joints will force the line to be a certain length though as the nodes move, right? So, the lines would not change length as needed? – zumzum Apr 24 '19 at 22:05
  • You could be right - what’s the maximum number of nodes you think you’ll have on screen at once? – Steve Ives Apr 25 '19 at 12:53
  • If I were to guess right now could be about 100 or more – zumzum Apr 29 '19 at 22:21
  • Are you concerned about performance (frame rate) or code complexity? That seems like the sort of number where performance may not be an issue. Code complexity may be reduced by making these nodes a subclass of SKSpriteNode, with the new class containing the main node and a number of SKShapeNodes to represent the lines. – Steve Ives Apr 30 '19 at 08:28
  • yah, for now I am updating each line every frame. I was trying to find out if there was a clever way to go about this. – zumzum May 01 '19 at 21:42
  • @zumzum could you please let me know if you are able to find a way to fix it – VSP Mar 19 '21 at 06:28
  • I am updating each line at each frame. It works well in my user case (less than a 100 concurrent). If there is a better way, I'd love to get some feedback on it. – zumzum Mar 19 '21 at 14:24
  • If possible could you please share the piece of code to understand the way how you are handling this what I required is joint multiple nodes with line and I can perform pan gesture of every nodes and when the node moves I need line to behave the same @zumzum, I have tried with joint but I don't think it draw the line also it have certain limit to perform the pan gesture – VSP Mar 21 '21 at 05:33

0 Answers0