0

I am trying to make a simple app where if you tap the screen a box is switched from floating to affected by gravity. I can't seem to find a way to make the box float in the air though.

This code here takes care of half the problem:

    boxNode.physicsBody = [SCNPhysicsBody dynamicBody];

This causes the box to drop out of the air and hit a floor I created. Is there anything in SCNPhysicsBody that would do the opposite of this? Say, perhaps, cause objects to float or just sail off toward a ceiling?

Also, I've written this code:

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {

    if (myBool == false) {
        myBool = true;
        NSLog(@"true");
    } else {
        myBool = false;
        NSLog(@"false");
    }


}
- (void)viewDidLoad
{
    [super viewDidLoad];

// touch recognizer
    UITapGestureRecognizer *screenTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [self.view addGestureRecognizer:screenTap];

    // create box
    SCNBox *myBox = [SCNBox boxWithWidth:1.0 height:1.0 length:1.0 chamferRadius:0.1];
    SCNNode *boxNode = [SCNNode nodeWithGeometry:myBox];
    boxNode.position = SCNVector3Make(0.0, 0.0, 4.0);
    [myScene.rootNode addChildNode:boxNode];



    while (myBool == true) {

    boxNode.physicsBody = [SCNPhysicsBody dynamicBody];

    }


}

I'm not sure why the while loop doesn't work though. I was thinking it would detect that myBool has been changed and alter the physics of the boxNode, but it doesn't.

4

1 回答 1

1

viewDidLoad方法仅在加载视图时调用一次。如果您的应用程序是用 初始化的myBool = false,那么 while 循环将永远不会运行。但是在这种情况下,如果myBool为真,while 循环将永远不会停止执行,从而阻止视图加载,从而阻止用户点击视图来触发您的手势识别器。

我没有测试以下内容,但它至少应该给你一个起点。场景是viewDidLoad根据您的代码创建的,重要的是场景的physicsWorld重力设置为零(默认为 -9.8)。稍后当用户点击视图时,我们将重力重置为默认值,这应该会导致盒子下落。

头文件GameViewController.h

#import <UIKit/UIKit.h>
#import <SceneKit/SceneKit.h>

@interface GameViewController : UIViewController {
    SCNScene *myScene;
}

@end

GameViewController.m

#import "GameViewController.h"

@implementation GameViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // create a new scene
    myScene = [SCNScene scene];

    // create box
    SCNBox *myBox = [SCNBox boxWithWidth:1.0 height:1.0 length:1.0 chamferRadius:0.1];
    SCNNode *boxNode = [SCNNode nodeWithGeometry:myBox];
    boxNode.position = SCNVector3Make(0.0, 0.0, 4.0);
    [myScene.rootNode addChildNode:boxNode];

    boxNode.physicsBody = [SCNPhysicsBody dynamicBody];

    //'disable' scene gravity
    myScene.physicsWorld.gravity = SCNVector3Make(0, 0, 0);

    SCNView *scnView = (SCNView *)self.view;
    scnView.scene = myScene;
    scnView.allowsCameraControl = YES;
    scnView.autoenablesDefaultLighting = YES;
    scnView.backgroundColor = [UIColor blackColor];

    // add a tap gesture recognizer
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    NSMutableArray *gestureRecognizers = [NSMutableArray array];
    [gestureRecognizers addObject:tapGesture];
    [gestureRecognizers addObjectsFromArray:scnView.gestureRecognizers];
    scnView.gestureRecognizers = gestureRecognizers;
}

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
    myScene.physicsWorld.gravity = SCNVector3Make(0, -9.8, 0);
}

@end
于 2016-03-29T01:29:01.263 回答