2014-10-08 40 views
0

我想製作一個浮球。在SceneKit中受重力影響?

在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

我發現部分解決方案和一些有趣的錯誤。 這讓球停留不動,如果接觸到碰撞和「浮動」之後

// .... 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; 
} 
1

您可以通過將scene.physicsWorld.gravity設置爲零來關閉重力。 但是,如果您希望某些對象受到影響,有些則不需要添加[SCNPhysicsField linearGravityField]並配置categoryBitMask以使其影響您想要的節點。

+0

我看到了,但Apple的手冊中沒有提到有關位掩碼的內容 - 請參閱https://developer.apple.com/library/prerelease/iOS/documentation/SceneKit/Reference/SCNPhysicsField_Class/index.html#//apple_ref/occ/ clm/SCNPhysicsField/linearGravityField -----所以,你的意思是我必須將相同的位掩碼設置爲「引力」模型和字段的節點? – djdance 2014-10-09 08:32:45

+0

所以,Scenekit字段在這裏:https://developer.apple.com/library/ios/documentation/SceneKit/Reference/SCNPhysicsField_Class/index.html#//apple_ref/occ/instp/SCNPhysicsField/categoryBitMask – djdance 2014-11-05 16:49:26