Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SKTileLayer physicsBody correction.. #10

Open
aornano opened this issue Apr 17, 2017 · 1 comment
Open

SKTileLayer physicsBody correction.. #10

aornano opened this issue Apr 17, 2017 · 1 comment

Comments

@aornano
Copy link

aornano commented Apr 17, 2017

Maybe this is not properly a bug or issue, but an improve.
Suppose we have a layer in our map that represent a wall, composed by little rectangles with a texture, and we have this code to get the layer:

self.wallsTilesLayer = self.tilemap.getLayer(named: "walls") as! SKTileLayer

Now to get the physicsBody we can do:

self.wallsTilesLayer.setPhysicsBody()

that call this method of your framework:

open func setupPhysics(isDynamic: Bool=false){
        physicsBody = SKPhysicsBody(edgeLoopFrom: self.boundingRect)
        physicsBody?.isDynamic = isDynamic
    }

So, if you have a layer composed by many rectangle with textures around your map, this physicsBody cover only the bounding rectangle of your map.

That's not enough.

Now we suppose that a normal user set all tiles of his wall with the property string "wall", we can obtain the array of tiles that composed all the wall layer with :

self.wallsTiles = wallsTilesLayer.getTilesWithProperty("wall", "" as AnyObject)

One of the immediate error we can do is to create a physicsBody for each tile of the array, something like:

for wallTile in wallsTiles {
    wallTile.setupPhysics(isDynamic:false)
}

This work , but it generate a giant amount of physics bodies to check and this drop your FPS.
A good thing to avoid this problem could be make an array of SKPhysicsBodies:

var wallBodies = [SKPhysicsBody]()
for wallTile in wallsTiles {
            let wallBody = SKPhysicsBody.init(edgeLoopFrom: wallTile.frame)
            wallBody.isDynamic = false
            wallBodies.append(wallBody)
}
let wall = SKPhysicsBody.init(bodies: wallBodies)
self.wallsTilesLayer.physicsBody = wall

I don't know if your function was an error but I just want to report my idea to improve the physics.

@mfessenden
Copy link
Owner

Apologies for the late reply.

That method is a leftover from a really old feature request. The idea was to allow users bypass Apple's clunky sks editor and quickly prototype shapes & dynamics in Tiled for SpriteKit...even if no tiles are being drawn. I thought it might be a fun exercise to expand the API to accommodate this, so I created a few proof of concepts (one of which is shown here). I believe that function has been renamed setupLayerPhysicsBoundary in my development branch, so I'll be sure to update that for the next release.

Right now the methods for adding physics to tiles & objects are fairly rudimentary; I'm not opposed to adding more dynamics features, but I also don't want to go to far down a path of adding custom features that users will be implementing themselves.

The next update has support for tile objects and (hopefully) objects created via the tile collision editor. I'm happy to entertain any ideas you have beyond that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants