logo
down
shadow

Making collisions work in Apple SpriteKit


Making collisions work in Apple SpriteKit

By : Md.Rabiul Islam Robi
Date : November 18 2020, 11:13 AM
I wish this helpful for you your nodes dont have a physicsbody assigned to them.
you're assigning them physicsbody categories, but they have no physicsbody to begin with. It's nil!
code :
yournode.physicsBody = [SKPhysicsBody <some initializer>]


Share : facebook icon twitter icon
bug with regular collisions in spriteKit

bug with regular collisions in spriteKit


By : Elemmiriel
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I finally found a way to fix it. I've changed the size of physicbody.size.y to the same as his spriteNode.y and the bug has gone.
Multiple collisions spritekit

Multiple collisions spritekit


By : Achini Fernando
Date : March 29 2020, 07:55 AM
it fixes the issue I am trying to make a SpriteKit game but I cannot figure out or find a good example of multiple collision detection. , //for multiple collision you can add code like something that
code :
CGPoint contactPoint=CGPointMake(0, 0);
//multiple collision
if (contact.bodyA.categoryBitMask || contact.bodyB.categoryBitMask)
{
    //first collision
    if(([contact.bodyA.node.name isEqualToString:@"hero"] && [contact.bodyB.node.name isEqualToString:@"coin"] ) || ([contact.bodyA.node.name isEqualToString:@"coin"] && [contact.bodyB.node.name isEqualToString:@"hero"]) )
    {
        if([contact.bodyA.node.name isEqualToString:@"coin"] )
        {

            [contact.bodyA.node removeFromParent];
        }
        else
        {
            [contact.bodyB.node removeFromParent];
        }

    }
    //second collision
    else if(([contact.bodyA.node.name isEqualToString:@"hero"] && [contact.bodyB.node.name isEqualToString:@"enemy"] ) || ([contact.bodyA.node.name isEqualToString:@"enemy"] && [contact.bodyB.node.name isEqualToString:@"hero"]) )
    {
        if([contact.bodyA.node.name isEqualToString:@"hero"] )
        {

            [contact.bodyA.node removeFromParent];
        }
        else
        {
            [contact.bodyB.node removeFromParent];
        }

    }
}
Collisions in SpriteKit

Collisions in SpriteKit


By : kishan
Date : March 29 2020, 07:55 AM
Any of those help Try browsing the web and stackoverflow before asking a question because there is a lot of tutorials on the web and even stack overflow about this. This question might even be closed because collision detection is one of the basics in SpriteKit and has been asked a lot about on SO.
Anyway there is some errors/omissions in your code, so lets have a look.
code :
 physicsWorld.contactDelegate = self
 object.physicsBody = SKPhysicsBody(...)
...physicsBody?.contactTestBitMask...
...physicsBody!.contactTestBitMask...
 object.physicsBody?.applyImpulse(push)
 struct ColliderType {
    static let object: UInt32 = 0x1 << 0
    static let boundary: UInt32 = 0x1 << 1
    ....
 }
...contactTestBitMask = ColliderType.object
...
  struct ColliderType {

static let object: UInt32 = 0x1 << 0
static let boundary: UInt32 = 0x1 << 1


 }

  class GameScene: SKScene, SKPhysicsContactDelegate {

var gameOver = false

var object: SKSpriteNode!


override func didMoveToView(view: SKView) {
    scene?.scaleMode = SKSceneScaleMode.AspectFill


    self.physicsWorld.gravity = CGVectorMake(0.0, 2.0)
    physicsWorld.contactDelegate = self

    object = SKSpriteNode( imageNamed: "Spaceship")
    object.zPosition = 2
    object.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
    object.physicsBody = SKPhysicsBody(circleOfRadius: object.size.width / 0.32)

    object.physicsBody?.dynamic = true
    object.physicsBody?.allowsRotation = true
    object.physicsBody?.categoryBitMask = ColliderType.object
    object.physicsBody?.contactTestBitMask = ColliderType.boundary
    object.physicsBody?.collisionBitMask = ColliderType.boundary
    self.addChild(object)


    let boundary = SKPhysicsBody(edgeLoopFromRect: CGRect(x: 253, y: 138, width:515, height:354))
    self.physicsBody = boundary
    self.physicsBody?.friction = 0 
    boundary.contactTestBitMask = ColliderType.object
    boundary.categoryBitMask = ColliderType.boundary
    boundary.collisionBitMask = ColliderType.object

    let push = CGVectorMake(10, 10)
    object.physicsBody?.applyImpulse(push)
}


func didBeginContact(contact:SKPhysicsContact) {
    print("We have Contact")

    gameOver = true
    self.speed = 0
    }
 }
SpriteKit: Collisions with 300 nodes, very low FPS

SpriteKit: Collisions with 300 nodes, very low FPS


By : Shankey
Date : March 29 2020, 07:55 AM
this will help You might want to try to give your rocks a circular physicsbody. I don't think it will solve the issue but might increase framerate a bit! As far as I've understood, circular physicsbodies are better for performance than rectangular ones.
Collisions between sprites in SpriteKit

Collisions between sprites in SpriteKit


By : Ankit Bhatnagar
Date : March 29 2020, 07:55 AM
it fixes the issue The confusion is that the collisionBitMask is used to define which physicsBodies that interacts in the physicsModel. What you really want is contactTestBitmask.
Also your Physics doesn't return proper values to use for a Bit Mask. As pure Ints they should be 1,2,4,8 etc.
Related Posts Related Posts :
  • Update tableView row from AppDelegate Swift 4
  • Assigning return value of a class method to a variable error
  • how to display circular progress in delphi ios apps?
  • I can't figure how to fix this Expected identifier or '('
  • How to Test APIs in Swift 4?
  • Link error /Build/Products/Debug-iphonesimulator file not found
  • working with a very large JSON object in iOS
  • Turn off part of the iPhone Screen
  • Phonegap 3.0 app with facebook login using xCode
  • How to add interstitial admob ads to my ios project
  • adding tapku calendar to ios app
  • Add an alertcontroller in a global swift file?
  • Take square image with Cordova (Phonegap) Camera API?
  • Amazon S3 policy allowing only upload not overwrite
  • How to change the UILabel content by tapping?
  • Adding checkmark to selected table cell also checks another table cell
  • UICollectionView cellForItemAtIndexPath indexPath row is nil
  • Resize and set quality on JPEG image while retaining EXIF in iOS
  • How to push rootviewcontroller in Ios
  • How to add UIBarbuttonItem at centre on UInavigationbar in iOS
  • Would this IOS device test crash be expected
  • PickerView not showing any values
  • How to set root view controller in storyboard animated with navigation controller programmatically
  • Can anyone suggest me the effective way to deal with s3 upload fail because of timezone difference issue?
  • Getting list of files in documents folder
  • Error while using Realm Object as a singleton. Is it a good idea?
  • inserting into SQLite database
  • More nodes than I should
  • Facebook IOS SDK navigate to another controller after login not working
  • Swift Array - use "Contains" of type AnyObject
  • Sidebar menu with indicate navigationbar on iOS
  • Programmatically set size of UIView from Storyboard
  • Navigation bar blink
  • Web services issue on POST method
  • Parse.com error 'Method not allowed when Pinning is enabled.' when I use a PFQueryTableViewController subclass
  • Swift can't send URLRequest at all?
  • No Provisiong profiles with a valid signing identity were found and Failed to locate or generate matching signing assets
  • iOS: how to set httponly flag for NSHTTPCookie
  • How to get the mac address of a beacon in objective-c without knowing its UUID, major and minor values?
  • tableView is not showing data from JSON in swift
  • iTunes reject app for date of birth
  • SWIFT reporting "Extra argument in call" - works in Playground
  • save data depending on passed value in swift
  • webview not showing on full Screen of iphone 6
  • Location Notification's based on Latest iOS Feature
  • Adjusting Height of UITableViewCell
  • How to show five digit unicode in iphone
  • unwind doesn't work
  • Confusion about coordinates, frames & child nodes in SpriteKit on iOS?
  • If Statements not being recognized by Xcode
  • readonly property in RubyMotion
  • 'No available types for source 1' when the app runs on my device(IPhone 6) swift
  • IOS:Remove view when clicked in background
  • Removing "\" from URL in iOS Swift
  • iOS - Send an email automatically (NOT from user's account)
  • The usage of NSNotificationCenter in the relationship of parent and children
  • When do Apple auto-renewable subscriptions renew?
  • Unknown large spacing between x-axis label and base of plot in core-plot
  • How do I change the displayed time format after NSTimer is initiated?
  • Swift JSON breaks when [ ] are used
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org