作者热门文章
- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
如果我在 y 方向施加 1 的冲量,球会来回弹跳而不会损失任何能量。但是,如果初始冲量为 0.5 或以下,则球在撞到墙壁时会立即失去所有能量。为什么会这样?我对 SKPhysicsBody 类的属性有很好的理解。试试这段代码,看看您的计算机上是否会出现相同的行为。
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
SKPhysicsBody* borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody = borderBody;
self.physicsBody.friction = 0.0f;
self.backgroundColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1.0];
SKShapeNode *ball = [SKShapeNode node];
CGMutablePathRef pathToDraw = CGPathCreateMutable();
[ball setStrokeColor:[UIColor blackColor]];
CGPathMoveToPoint(pathToDraw, NULL, 0, 0);
CGPathAddEllipseInRect(pathToDraw, NULL, CGRectMake(-16, -16, 32, 32));
ball.path = pathToDraw;
ball.position = CGPointMake(size.width / 2, size.height / 2);
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/2];
ball.physicsBody.friction = 0.0f;
ball.physicsBody.restitution = 1.0f;
ball.physicsBody.linearDamping = 0.0f;
ball.physicsBody.allowsRotation = NO;
[self addChild:ball];
[ball.physicsBody applyImpulse:CGVectorMake(0, 0.5)];
}
return self;
}
最佳答案
当碰撞速度足够小时(例如 (0, 0.5)
的 CGVector
),Sprite Kit 物理引擎底层的 Box2d 会将碰撞计算为非弹性(即,就好像 restitution
为 0,移除任何弹跳),并且节点不会弹跳。
这是根据 Box2d documentation , 以防止抖动。
在 Box2d 源代码中,您甚至有这一行:
/// A velocity threshold for elastic collisions. Any collision with a relative linear
/// velocity below this threshold will be treated as inelastic.
#define b2_velocityThreshold
您的冲动应该高于此阈值才能尊重赔偿。
关于ios - SpriteKit 球在撞墙时失去所有能量,恢复=1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22058292/
我是一名优秀的程序员,十分优秀!