How to detect contact in different areas of the physical body

I would like to get the classic headshot effect in a sprite game.

My ideas and attempts:

  • Create separate bodies "body" and "head" of physics on the same spray and attach them with a joint. Impossible: (

  • Create the physical body of the โ€œbodyโ€ over the โ€œenemyโ€ sprite and create another transparent sprite with the โ€œheadโ€ physics that follows the body (sits on top). This approach requires me to keep track of which head is connected to which body. It will also reduce the effectiveness of games. I am curious if I can subclass SKSpriteNode and create an โ€œenemyโ€ that has these two spraynods and a physics connected.

  • Check the location of the projectile against the enemy to see if the projectile has reached a certain Y range at the top of the sprite. I believe that this will be ineffective, because the shells fly quite quickly.

I would be grateful if someone would create this effect and be able to share their knowledge. Thanks!

+2
sprite-kit skphysicsbody
source share
2 answers

I use this setting in many places. It scales very well if you want to add more parts or weapons, and this makes it easy to visually hide any of these parts or weapons. Using userData can come in handy if you have a lot and different enemies have different things, or you do crazy things, such as parent sprites for children, on different entities based on gameplay, etc.

-(SKSpriteNode*)makeAnEnemy { SKSpriteNode *mainEnemy; SKSpriteNode *enemyHead; //setup mainEnemy physics body //setup enemyHead physics body [mainEnemy addChild:enemyHead]; // depending on your plans, on hit you could use the following code later: SKSpriteNode *theEnemyThatWasHit = (SKSpriteNode*)enemyHead.parent; //or you can use userdata and set it all up here [mainEnemy.userData setObject:enemyHead forKey:@"myHead"]; [enemyHead.userData setObject:mainEnemy forKey:@"myBody"]; return mainEnemy; } 
+1
source share

Using the pinned attribute for physicalBody makes it easy to create complex sprites with multiple contact areas. Binding a physical body to a Node Parent:

The default value is NO. If YES, then the position of the node is fixed relative to its parent. The position of a node cannot be changed by actions or physical forces. Node can rotate freely around its position in response to collisions or other forces. If the parent node has a physical body, then the two physical bodies are treated as if they are connected to a pin connection.

Here is the main class of dogs, where the dog has a main body and head. Please note that I set allowsRotation = false so that the head could not move around the body.

 import SpriteKit // for readability let mainSize = CGSize(width: 300, height: 300) let headSize = CGSize(width: 100, height: 100) class Dog : SKSpriteNode { let head = SKSpriteNode(color: SKColor.greenColor(), size: headSize) init() { super.init(texture: nil, color: SKColor.redColor(), size: mainSize) head.position = CGPoint(x: mainSize.width - headSize.width, y: 0) addChild(head) } // called after Dog has been added to the scene func configurePhysicsBody() { physicsBody = SKPhysicsBody(circleOfRadius: mainSize.width / 2) physicsBody.dynamic = true physicsBody.allowsRotation = false // set contactBitmask for main body head.physicsBody = SKPhysicsBody(circleOfRadius: headSize.width / 2) head.physicsBody.dynamic = true head.physicsBody.allowsRotation = false // The head is pinned to the parent node, so position is fixed relative to parent head.physicsBody.pinned = true // set contactBitmask for head } } 

enter image description here

+1
source share

All Articles