Scott
Scott

Reputation: 1222

SKPhysicsBody slowing down program

I have a random maze generator that starts building small mazes then progress into massive levels. The "C"s are collectables and the "T"s are tiles. the "P" is the player starting position. I included a sample tile map below.

The performance issue is not when I have a small 6x12 pattern like here; it shows up when I've got a 20x20 pattern for example.

Each character is a tile, and each tile has it's own SKPhysicsBody. The tiles are not square, they are complex polygons and the tiles don't quite touch each other.

The "C"s need to be able to be removed one at a time, and the "T"s are permanent for the level and don't move. Also the maze only shows 6x4 section of tiles at a time and moves the background to the view centered on the player.

I've tried making the T's and C's rectangles which drastically improves performance (but still slower than desired) although the user won't care for this, the shape of the tile is just too different.

Are there any performance tricks you pros can muster up to fix this?

TTTTTT
TCTTCT
TCCCCT
TTCTCT
TCCTCT
TTCCTT
TTTCTT
TTCCCT
TCCTCT
TCTTCT
TTCCCT
TTPTTT

Upvotes: 1

Views: 217

Answers (2)

Stefan
Stefan

Reputation: 5451

What kind of collision method are you using?

enter image description here

SpriteKit provides several possibilities to define the shape of the SKPhysicsBody. The best performance provides a rectangle or a circle:

myPhysicsBody = SKPhysicsBody(rectangleOfSize: mySprite.size)

You can also define more complex shapes like a triangle, which have a worse performance.

Using the texture (SpriteKit will use all non transparent pixels to detect the shape by itself) has the worst performance:

myPhysicsBody = SKPhysicsBody(texture: mySprite.texture, size: mySprite.size)

Activating 'usesPreciseCollisionDetection' will also have a negative impact on your performance.

Upvotes: 1

Paweł Białecki
Paweł Białecki

Reputation: 48

The tiles are not square, they are complex polygons

I think this is your problem. Also, if your bodies are dynamic, setting them static will drastically improve performance. You can also try pooling. And be aware, that performance on the simulator is drastically lower than on the real device.

Upvotes: 1

Related Questions