1
votes

My current project contains a gravity simulator where sprites move in accordance with the forces they experience in the game scene.

One of my features involve allowing moving sprites to draw a line behind them so you can see what paths they take.

Shown here:

enter image description here

However, as the Sprite continues it's movements around the screen, the FPS begins to dive. This can be seen in this second image where some time has passed since the sprite first started its movement.

enter image description here

When researching, I found other people had posted with similar problems:

Multiple skshapenode in one draw?

However, in the question above, the answer's poster detailed that it (The answer) was meant for a static image, which isn't something I want, because this line will change in real time depending on what influences the sprites path, this was reflected when I tried implementing a function to add a new Line to the old one which didn't work. That Code here

I'm asking if anyone can assist me in finding a way to properly stop this constant FPS drop that comes from all the draw operations. My current draw code consists of two Functions.

-(void)updateDrawPath:(CGPoint)a B:(CGPoint)b
{
    CGPathAddLineToPoint(_lineToDraw, NULL, b.x, b.y);
    _lineNode.path = _lineToDraw;
}

-(void)traceObject:(SKPlanetNode *)p
{
    _lineToDraw = CGPathCreateMutable();
    CGPathMoveToPoint((_lineToDraw), NULL, p.position.x, p.position.y);
    _lineNode = [SKShapeNode node];
    _lineNode.path = _lineToDraw;
    _lineNode.strokeColor = [SKColor whiteColor];
    _lineNode.antialiased = YES;
    _lineNode.lineWidth = 3;
    [self addChild:_lineNode];
}
  • updateDrawPath: Draws line to latest position of Sprite.

  • traceObject: Takes SKPlanetNode (Subclass of SKSpriteNode), and sets it up to have a line drawn after it.

If anyone can suggest a way to do this and also reduce the terrible overhead I keep accumulating, it would be fantastic!

1
drawing sprites gets to be n^2 if they are related to each other... try drawing your line as a bezier curve or bitmap and composite it... - Grady Player
I found a solution in answering this question : stackoverflow.com/questions/24553245/… -- It was in relation to drawing with touch, but the solution is directly applicable to your current issue. - prototypical
@prototypical It's your solution! Also ". If you set showsDrawCount on your SKView instance, you will see what I mean." I don't get an extremely high draw count but this problem is still visible - Micrified
that's what I said, I found a solution in answering that question. - prototypical
It's the "caching" element of my answer you need to take note of. A SKShapeNode is NOT efficient when you have all those segments and gets even worse as you continue to draw. - prototypical

1 Answers

-1
votes

A couple suggestions:

  • Consider that SKShapeNode is more or less just a tool for debug drawing mostly, due to the fact that it doesn't draw in batches it's really not suitable to make a game around that or to use it extensively (both many shapes as well as few but complex shapes).
  • You could draw lines using a custom shader which will likely be faster and more elegant solution, though of course you may have to learn how to write shader programs first.
  • Be sure to measure performance only on a device, never the simulator.