我想做一个浮球。
在spritekit中我做 physicsbody.affectedbygravity = NO ;
但是我在 scenekit 做了什么?
我试图将质量设置为零,但是动态体变为静态:它不是由重力移动,也不是由碰撞移动。我的代码是
// add ball
SCNNode *ball = [SCNNode node];
ball.geometry = [SCNSphere sphereWithRadius:5];
ball.geometry.firstMaterial.locksAmbientWithDiffuse = YES;
ball.geometry.firstMaterial.diffuse.contents = @"ball.jpg";
ball.geometry.firstMaterial.diffuse.contentsTransform = SCNMatrix4MakeScale(2, 1, 1);
ball.geometry.firstMaterial.diffuse.wrapS = SCNWrapModeMirror;
ball.physicsBody = [SCNPhysicsBody dynamicBody];
ball.physicsBody.restitution = 0.9;
//ball.physicsBody.mass=0; //here makes a ball statical and not moving
ball.physicsBody.allowsResting = YES;
[[scene rootNode] addChildNode:ball];
UPD 这也没有帮助 - 球缓慢下降。
- (void)renderer:(id<SCNSceneRenderer>)aRenderer didSimulatePhysicsAtTime:(NSTimeInterval)time{
[ball.physicsBody applyForce:SCNVector3Make(0, 9.8, 0) atPosition:SCNVector3Make(0,0,0) impulse:NO]; //this
ball.physicsBody.velocity=SCNVector3Make(0,0,0); //or this
}
答案 0 :(得分:1)
您可以通过将scene.physicsWorld.gravity设置为零来关闭重力。 但是,如果您希望某些对象受到影响而某些对象不需要添加[SCNPhysicsField linearGravityField]并配置categoryBitMask以使其影响您想要的节点。
答案 1 :(得分:0)
我找到了部分解决方案和一些有趣的bug。 这使球在碰撞前保持不动,并在此之后“浮动”
// .... the same code above in creation
ball.physicsBody.mass=1;
[[scene rootNode] addChildNode:ball];
ball.physicsBody.mass=0; //important right after creating!!! or it become bodyless after changing mass to 1
如果联系,请将其释放。如果施加的力等于重力,它将浮动,缓慢下降。原因可能会降低摔倒的速度。
// physics contact delegate
- (void)physicsWorld:(SCNPhysicsWorld *)world didBeginContact:(SCNPhysicsContact *)contact{
if (contact.nodeA.physicsBody.type == SCNPhysicsBodyTypeDynamic && contact.nodeA.physicsBody.mass==0)
contact.nodeA.physicsBody.mass=1;
else
if (contact.nodeB.physicsBody.type == SCNPhysicsBodyTypeDynamic && contact.nodeB.physicsBody.mass==0)
contact.nodeB.physicsBody.mass=1;
}