39497

SKPhysicsBody not as expected

Question:

I have the following code to create a rectangular brick and a physics body associated with it. I expected the physics body to be a solid rectangle same size and position as that of the brick, but am getting a body which I think has a poition offset and perhaps also a size difference. Is there some issue with the coordinate systems I have missed? What is the right way to approach this?

- (void)addBrick { SKShapeNode *brick = [[SKShapeNode alloc] init]; CGRect brickBoundary = CGRectMake(0.0, 0.0, 100.0, 100.0); brick.position = CGPointMake(100.0, 100.0); brick.path = CGPathCreateWithRect(brickBoundary, nil); brick.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(100.0,100.0)]; brick.physicsBody.restitution = 1.0; brick.physicsBody.friction = 0.0; brick.physicsBody.dynamic = NO; [self addChild:brick]; }

Answer1:

This is easy way to create a rect and add a physics body to it.

SKSpriteNode *n1 = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:objectSize]; n1.position = CGPointMake(self.size.width/2, 200); n1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:n1.size]; n1.physicsBody.dynamic = NO; [self addChild:n1];

Here is a modified version from a <a href="http://www.raywenderlich.com" rel="nofollow noreferrer">RW</a> tutorial to provide your spite with a debug rect

// RW Debug modified version CGPathRef bodyPath = CGPathCreateWithRect( CGRectMake(-n1.size.width/2, -n1.size.height/2, n1.size.width, n1.size.height),nil); SKShapeNode *shape = [SKShapeNode node]; shape.path = bodyPath; shape.strokeColor = [SKColor colorWithRed:1.0 green:0 blue:0 alpha:0.5]; shape.lineWidth = 1.0; [n1 addChild:shape]; CGPathRelease(bodyPath);

You implementation produced this (red being the physics box).

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/4b81j.png" data-original="https://i.stack.imgur.com/4b81j.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Recommend

  • SKShapeNode Circle not appearing circular
  • SKShapeNode Using Wrong Width and Height
  • Mongo DB duplication issue while using sorting with limit and skip in aggregation
  • Opengl Iphone SDK: How to tell if you're touching an object on screen?
  • Dynamically change cell's height while typing text, and reload the containing tableview for res
  • Progress Bar in Python Console on Windows
  • Merge two imageViews into one and save iOS swift
  • Spark dataframes: Extract a column based on the value of another column
  • UITableView gives empty table, does not load data
  • String not fitting in label which is in a subview for swift
  • Updating app on device with .xib removed crashes app
  • How to load image asynchronously using UIImageView+AFNetworking in ios
  • Determine if UTF-8 encoded NSData contains a null-terminated string
  • iOS :Call a method in specific time
  • MKOverlayView performance
  • How can I figure out what UITableViewCell I'm clicking on during a search?
  • windowScriptObject method not found on Objective-C code
  • SpriteKit SKScene add AVCaptureVideoPreviewLayer CALayer as background not working
  • UILabel extra spaces before and after text ios
  • iOS Date formatting
  • x-axis labels in coreplot not displayed
  • How to make the tableview response pan gesture in ZUUIRevealController
  • iOS - MKOverlayView custom view rect fills works, but line draws do not
  • UIScrollView setContentOffset: animated: not working
  • How do I add a UIAlertController in app delegate (obj-c)
  • iOS 9 errors and correct conversion to swift 2
  • GKLeaderboardViewController initial leaderboard
  • Add Labeltext on Mkannotation image
  • iOS 6 dateFromString returns wrong date
  • “A GKScore must specify a leaderboard.”
  • How to resolve this packager error on react native Android
  • Installing PHP 7 on digitalocean
  • Question about instantiating object
  • Problems installing Yesod for Haskell
  • NSScanner Loop Question
  • Mysterious problem with floating point in LISP - time axis generation
  • Eraser for UIBezierPath
  • Compare two NSDates in iPhone
  • File upload with ng-file-upload throwing error
  • Load html files in TinyMce