swift - SKPhysicBodies appear to be slightly off-place -


edit: have been able solve problem using physicseditor make polygonal physicsbody instead of using skphysicsbody(... alphathreshold: ... )

--

for reason i'm having trouble i'm assuming skphysicbodies being off-place. while using showphysics stationary obstacle nodes appear have physicbodies in correct position, able trigger collisions without touching obstacle. if @ image below shows have found physicsbodies off centre, despite showphysics telling me otherwise. (note, player node travels in middle of these obstacle nodes).

enter image description here

i thought worth noting while player travelling, physicbody appears travel ahead figured normal.

i use skphysicsbody(... alphathreshold: ... ) create physicbodies .png images.

cheers.

edit: here's how create obstacle physicbodies. once they're added worldnode left alone until need removed. apart don't change them in way.

let obstaclenode = skspritenode(imagenamed: ... ) obstaclenode.position = cgpoint(x: ..., y: ...) obstaclenode.name = "obstacle"  obstaclenode.physicsbody = skphysicsbody(texture: obstaclenode.texture!, alphathreshold: 0.1, size: cgsize(width: obstaclenode.texture!.size().width, height: obstaclenode.texture!.size().height)) obstaclenode.physicsbody?.affectedbygravity = false obstaclenode.physicsbody?.isdynamic = false obstaclenode.physicsbody!.categorybitmask = cc.wall.rawvalue obstaclenode.physicsbody!.collisionbitmask = cc.player.rawvalue obstaclenode.physicsbody!.contacttestbitmask = cc.player.rawvalue  worldnode.addchild(obstaclenode) 

the player node treated same way, here how player moves.

playernode.physicsbody?.velocity = cgvector(dx: dx, dy: dy) 

i'm assuming aren't showing exact images used create skspritenode , skphysicsbody instances. since using texture define shape of skphysicsbody running against this:

skphysicsbody documentation

if not want create own shapes, can use spritekit create shape based on sprite’s texture.

this easy , convenient can give unexpected results depending on textures using sprite. perhaps try making explicit mask or using simple shape represent physics body. there examples , guidelines in documentation.

i follow pattern when set properties on objects:

// safely unwrap , handle failure if fails guard let texture = obstaclenode.texture else { return }   // create physics body let physicsbody = skphysicsbody(texture: texture,                                  alphathreshold: 0.1,                                  size: cgsize(width: texture.size().width,                                               height: texture.size().height))  // safely set properties without need unwrap optional physicsbody.affectedbygravity = false  // set rest of properties  // set physics body property on node obstaclenode.physicsbody = physicsbody 

by setting properties on concrete instance of skphysicsbody , unwrapping , testing optionals minimize chances run-time crash may difficult debug.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -