gpt4 book ai didi

ios - SpriteKit 球在撞墙时失去所有能量,恢复=1

转载 作者:技术小花猫 更新时间:2023-10-29 10:52:54 31 4
gpt4 key购买 nike

如果我在 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com