SpriteKit球失去所有能量击中墙壁,恢复原状= 1 [英] SpriteKit ball loses all energy hitting wall, restitution=1

查看:192
本文介绍了SpriteKit球失去所有能量击中墙壁,恢复原状= 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在y方向施加1的冲量,球会来回反弹而不会失去任何能量。但是,如果初始脉冲为0.5或更低,球会在撞到墙壁时立即失去所有能量。为什么会这样?我对SKPhysicsBody类的属性有很好的理解。尝试使用此代码查看您是否在计算机上获得相同的行为。

If I apply an impulse of 1 in the y direction, the ball bounces back and forth without losing any energy. However, if the initial impulse is 0.5 or below, the ball loses all energy instantly when it hits the wall. Why is this happening? I have a pretty good understanding of the properties of the SKPhysicsBody class. Try this code to see if you get the same behavior on your computer.

-(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;
}


推荐答案

当碰撞速度很小时足够的(例如 CGVector (0,0.5)),Sprite Kit物理引擎底层的Box2d将计算碰撞是无弹性的(即好像恢复为0,消除任何弹性),节点不会反弹。

When the collision velocity is small enough (such as your CGVector of (0, 0.5)), Box2d underlying the Sprite Kit physics engine will compute the collision as inelastic (i.e. as if the restitution was 0, removing any bounciness), and the node will not bounce.

根据Box2d 文档,这是为了防止抖动。

This is, per the Box2d documentation, to prevent jitter.

在Box2d源代码中,你甚至有这一行:

In the Box2d source code, you even have this line:

/// A velocity threshold for elastic collisions. Any collision with a relative linear
/// velocity below this threshold will be treated as inelastic.
#define b2_velocityThreshold            

您的冲动应高于此阈值,以便恢复原状。

Your impulse should be above this threshold for the restitution to be respected.

这篇关于SpriteKit球失去所有能量击中墙壁,恢复原状= 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆