Ios8 在SceneKit中受重力影响?

Ios8 在SceneKit中受重力影响?,ios8,scenekit,Ios8,Scenekit,我想做一个浮球 在spritekit中,我做物理身体。受重力影响=NO 但是我在scenekit中做了什么 我试图将质量设置为零,但动态物体变成了静态物体:它既不受重力的影响,也不受碰撞的影响。我的代码是 // add ball SCNNode *ball = [SCNNode node]; ball.geometry = [SCNSphere sphereWithRadius:5]; ball.geometry.firstMaterial.locksAmbientWithDiffuse = Y

我想做一个浮球

在spritekit中,我做物理身体。受重力影响=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
}
-(void)渲染器:(id)aRenderer didSimulatePhysicsAtTime:(NSTimeInterval)时间{
[ball.physicsBody applyForce:scinvector3make(0,9.8,0)atPosition:scinvector3make(0,0,0)pulse:NO];//此
ball.physicsBody.velocity=SCInvector3Make(0,0,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;
}

可以通过将scene.physicsWorld.gravity设置为零来关闭重力。
但是,如果你想让一些对象受到影响,而另一些不受影响,你需要添加一个[SCNPhysicsField linearGravityField],并配置categoryBitMask以使其影响你想要的节点。

我看到了,但苹果的手册没有提到bitmask——请看------那么,你的意思是我必须将同一个bitmask都设置为“重力”吗模型和字段的节点?因此,Scenekit字段管理器如下所示: