3

I'm am trying to drop the SKSpriteNode with constant speed and without bouncing.

Here is the code I'm using:

SKSpriteNode *floor = [SKSpriteNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(self.size.width, 1)];
floor.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:floor.size];
floor.physicsBody.restitution = 0.0f;
floor.physicsBody.dynamic = NO;
floor.physicsBody.allowsRotation = NO;

SKSpriteNode* block = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:imageName]];
block.position = CGPointMake(160, 300);
block.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(block.size.width - 2, block.size.height)];
block.physicsBody.dynamic = dynamic;
block.physicsBody.restitution = 0.0f;
block.physicsBody.allowsRotation = NO;

When I vary the restitution value, I can see the difference in block bouncing, but when it's zero it still bounces a little bit. When multiple blocks are stacked, the below blocks also bounce a little bit.

How can I totally disable bouncing?

modusCell
  • 13,151
  • 9
  • 53
  • 80
aumanets
  • 3,703
  • 8
  • 39
  • 59
  • have you tried negative values? – CodeSmile Jun 19 '14 at 20:14
  • @LearnCocos2D I haven't. Where would you suggest to apply negative values? – aumanets Jun 19 '14 at 20:16
  • restitution of course. No idea what that does though. – CodeSmile Jun 19 '14 at 20:36
  • The restitution can't be negative. It determines the 'bounciness' of the physics body (0.0 - 1.0) – aumanets Jun 19 '14 at 20:56
  • 1
    In theory. In physics engines it can be helpful to go outside the value ranges. At worst it crashes right away, or doesn't do anything. – CodeSmile Jun 19 '14 at 21:17
  • With negative values it doesn't have normal behaviour, objects float through other objects. – aumanets Jun 20 '14 at 16:08
  • 1
    SpriteKit seems to be closely based on Box2D which also has this behavior. These libraries are made for speed, not accuracy. I would not hold out too much hope for getting a 100% inelastic collision even with a static object, let alone trying to prevent stacked dynamic objects from moving a little when hit from above. If these situations are unacceptable, you may be using the wrong tool for the job. I don't know if SpriteKit lets you decrease the time step, but for Box2D I would suggest that (eg. use time step length of 1/1000 sec instead of the typical 1/60 sec, etc). – iforce2d Jun 21 '14 at 16:20

1 Answers1

1

After some trial and error I've discovered that the Physics Engine is not meant to be used if you want accurate positioning of your elements. That's why in my board game, I have re-implemented the code in order to make posit

aumanets
  • 3,703
  • 8
  • 39
  • 59
  • What exactly was your solution at the end? I have the same problem. – damirstuhec Dec 07 '14 at 20:43
  • @Damir179 At first, Physics Engine looked like a good solution for my game. But as I mentioned it is not meant for accurate positioning. You can't control precise location of the object using the Physic Engine. So in my case, I have developed specific game engine with precise control of game objects and their exact positions. It was hard to move on, but was a good design choice. – aumanets Dec 08 '14 at 12:22