场景工具包中的墙碰撞

时间:2015-05-31 18:48:18

标签: objective-c scenekit

我正在尝试编写一个游戏,其中一个节点可以移动但是被一系列墙壁包含 - 基本上是一个迷宫游戏。为了得到基本的东西我将Apple的SceneKitVehicle项目(https://developer.apple.com/library/ios/samplecode/SceneKitVehicle)拆分为基本元素并放入一个立方体(下面代码中的框节点),而不是原始代码中的物理载体。 / p>

问题是,我以编程方式移动盒子节点,直到它到达墙壁,它只是直接穿过墙壁而不停止。与此同时,我可以将车辆代码重新放入,并在到达墙壁时停止。

- (void)setupEnvironment:(SCNScene *)scene
{
    //floor
    SCNNode*floor = [SCNNode node];
    floor.geometry = [SCNFloor floor];
    floor.geometry.firstMaterial.diffuse.contents = @"wood.png";

    SCNPhysicsBody *staticBody = [SCNPhysicsBody staticBody];
    floor.physicsBody = staticBody;
    [[scene rootNode] addChildNode:floor];
}

- (void)moveBox
{
    SCNAction *moveByAction = [SCNAction moveByX:0 y:0 z:-3 duration:0.1];
    SCNAction *repeatAction = [SCNAction repeatActionForever:moveByAction];
    [_boxNode runAction:repeatAction];
}

- (void)setupSceneElements:(SCNScene *)scene
{
    // add walls
    SCNNode *wall = [SCNNode nodeWithGeometry:[SCNBox boxWithWidth:400 height:100 length:4 chamferRadius:0]];
    wall.geometry.firstMaterial.diffuse.contents = @"wall.jpg";
    wall.geometry.firstMaterial.diffuse.contentsTransform = SCNMatrix4Mult(SCNMatrix4MakeScale(24, 2, 1), SCNMatrix4MakeTranslation(0, 1, 0));
    wall.geometry.firstMaterial.diffuse.wrapS = SCNWrapModeRepeat;
    wall.geometry.firstMaterial.diffuse.wrapT = SCNWrapModeMirror;
    wall.geometry.firstMaterial.doubleSided = NO;
    wall.castsShadow = NO;
    wall.geometry.firstMaterial.locksAmbientWithDiffuse = YES;

    wall.position = SCNVector3Make(0, 50, -92);
    wall.physicsBody = [SCNPhysicsBody staticBody];
    [scene.rootNode addChildNode:wall];
}

- (SCNNode *)setupBox:(SCNScene *)scene
{
    SCNBox *boxgeo = [[SCNBox alloc] init];
    boxgeo.height = 5;
    boxgeo.width = 5;
    boxgeo.length = 5;

    SCNNode *box = [SCNNode nodeWithGeometry:boxgeo];
    box.position = SCNVector3Make(0, 0, -30);
    boxPosition = box.position;
    box.rotation = SCNVector4Make(0, 1, 0, M_PI);

    box.physicsBody = [SCNPhysicsBody kinematicBody];

    [scene.rootNode addChildNode:box];

    return box;
}

- (SCNScene *)setupScene
{
    // create a new scene
    SCNScene *scene = [SCNScene scene];

    //global environment
    [self setupEnvironment:scene];

    //add elements
    [self setupSceneElements:scene];

    //setup box
    _boxNode = [self setupBox:scene];

    [self moveBox];

    //create a main camera
    _cameraNode = [[SCNNode alloc] init];
    _cameraNode.camera = [SCNCamera camera];
    _cameraNode.camera.zFar = 500;
    _cameraNode.position = SCNVector3Make(0, 60, 50);
    _cameraNode.rotation  = SCNVector4Make(1, 0, 0, -M_PI_4*0.75);
    [scene.rootNode addChildNode:_cameraNode];

    return scene;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[UIApplication sharedApplication] setStatusBarHidden:YES];

    SCNView *scnView = (SCNView *) self.view;
    scnView.backgroundColor = [SKColor blackColor];

    SCNScene *scene = [self setupScene];
    scnView.scene = scene;
    scnView.scene.physicsWorld.speed = 4.0;
    scnView.pointOfView = _cameraNode;
    scnView.delegate = self;

    [super viewDidLoad];
}

任何想法为什么盒子节点不会被隔离墙挡住?

问题可能是箱子节点需要像车辆那样的力推动,以便墙壁停止吗?

1 个答案:

答案 0 :(得分:2)

你想要完成的事情有点棘手。移动一个框并使其对物理交互作出反应是不可能用kinematicBody来设想它。

你可以保留它,但是你需要根据位置/边界框自己伪造碰撞,或者在物理世界中听取交叉点。

您还可以更改为与其他动态对象交互的动态主体。但是,在moveBox函数中,您使用SCNAction来移动Box。如果您使用的是dynamicBody,则无法以这种方式移动。您必须使用forces

最后一个选项只是使用SCNConstraint。不是最漂亮的,但是如果你的墙没有动,那就行了。