PhysicsBody: Could not create physics body
OK, here's a test of different approaches for avoiding this bug as of iOS 13.3 (edit also now tried on 13.3.1) and Xcode version 11.3. Full source of the test at this link:
https://github.com/bg2b/bugtest
Relevant code:
func addShip(_ texture: SKTexture, how: String) {
let sprite = SKSpriteNode(texture: texture)
sprite.position = CGPoint(x: x, y: 0)
sprite.zRotation = .pi / 4
x += 100
sprite.physicsBody = SKPhysicsBody(texture: texture, size: sprite.size)
if sprite.physicsBody == nil {
print("\(how) failed")
} else {
print("\(how) worked")
}
addChild(sprite)
}
override func didMove(to view: SKView) {
// The atlas version of a texture
addShip(SKTexture(imageNamed: "ship_blue"), how: "simple atlas reference")
// From an atlas, but call size() to force loading
let texture = SKTexture(imageNamed: "ship_blue")
_ = texture.size()
addShip(texture, how: "atlas force load")
// Reconstruct via CGImage (size would be wrong because of 2x)
let cgTexture = SKTexture(cgImage: texture.cgImage())
addShip(cgTexture, how: "reconstruct via cgImage")
// Re-render using view
let renderedTexture = view.texture(from: SKSpriteNode(texture: texture))!
addShip(renderedTexture, how: "re-render using view")
// Non-atlas texture
addShip(SKTexture(imageNamed: "nonatlas_ship_blue"), how: "not in atlas")
}
Summary:
- Simply referencing the texture from an atlas and making the physics body may fail
- Force-loading the texture by calling
size()
before making the body fails - Trying to make a new texture by going through
cgImage()
fails (the image itself is broken, probably related to the same bug) - Rendering to a texture using a view and then making the physics body from that new texture works
- Making the physics body from a non-atlas copy of the texture works
Console output from the test program showing what works and what does not:
2020-02-01 06:23:51.872975-0500 bugtest[14399:9898087] PhysicsBody: Could not create physics body.
simple atlas reference failed
2020-02-01 06:23:51.886387-0500 bugtest[14399:9898087] PhysicsBody: Could not create physics body.
atlas force load failed
2020-02-01 06:23:51.913927-0500 bugtest[14399:9898087] PhysicsBody: Could not create physics body.
reconstruct via cgImage failed
re-render using view worked
not in atlas worked
Here's a screen shot showing the effect of the different approaches. You have to look a bit closely, but only the last two have valid physics bodies.
SpriteKit PhysicsBody: Could not create physics body
thanks 0x141E, i found out that my texture had some white shit around it, after I've deleted it, everything started to work. Now i am wondering how to hide stroke around my texture which represents my physical object.
SKLabelNode PhysicsBody Issue
A SKLabelNode
doesn't have a physics body, so you can not apply physics directly to it. What you could do is create them as a set of SKSpriteNodes
and assign an image of each letter to the body texture.
To "turn the physics body on and off", you should toggle the node's property isDynamic
. For other effects, you can play with affectedByGravity
property and the collision and the contact bitMasks, depending on what you want.
To make the collision feel more real, you can use SKPhysicsContactDelegate
, so you can automatically "draw" a physics body with the exact shape of the letters. Something like this:
letterNode.physicsBody = SKPhysicsBody(texture: letterTexture, alphaThreshold: 1.0, size: letterTexture.size())
To handle the collisions and contacts, don't forget to set the contactDelegate in your scene, like this:
self.physicsWorld.contactDelegate = self
Related Topics
How to Go from Cmutablepointer<Cgfloat> to Cgfloat[] in Swift
Fill Circle with Wave Animation in Swiftui
How to Change the Current Day's Hours and Minutes in Swift
How to Disable User Interaction on Swiftui View
Differencebetween Final Class and Class
Swift: Floating Plus Button Over Tableview Using the Storyboard
How to Program a Nsoutlineview
Building a Spritekit/Gamekit Leaderboard Within a Specific Scene
How to Animate Path in Swiftui
Timestamped Event Matching Error: Failed to Find Matching Element
Is There a Daylight Savings Check in Swift
Swift Sort Dictionary by Value
Extend Generic Array<T> to Adopt Protocol
How to Make Cells Have a Gradient Background
Avcapturevideodataoutput Captureoutput Not Being Called
Using Applescript with Apple Events in MACos - Script Not Working