Scenekit - 绘制对象的轨迹

时间:2016-02-04 22:31:05

标签: animation scenekit quaternions

好吧,我不太确定早先的转换公式,正如有人指出的那样(我只是在某处阅读并尝试过),但现在我正在尝试一种方法,我在一个边缘创建了一个小盒子。随着旋转应用于船舶,我继续读取它的位置。然后使用这些位置绘制几何图形。下面是相关代码:

scene = [SCNScene sceneNamed:@"ship.scn"];
_viewpoint1.scene = scene;


box = [SCNNode node];
box.geometry = [SCNBox boxWithWidth:.1 height:.1 length:.1 chamferRadius:.1];
box.physicsBody = [SCNPhysicsBody staticBody];
// position the box to the end of the ship
box.position = SCNVector3Make(box.position.x, box.position.y, box.position.z +5.6);
[_viewpoint1.scene.rootNode addChildNode:box];

当应用程序收到四元数时,船会旋转,而尖端处的方框也会旋转,我会捕获该框的位置并将其存储在数组位置。

_viewpoint1.scene.rootNode.orientation = SCNVector4Make(f_q0, f_q1, f_q2, f_q3);
SCNVector3 pt = [box convertPosition:SCNVector3Make(box.position.x, box.position.y, box.position.z) toNode:nil];

[positions addObject:[NSValue valueWithSCNVector3:SCNVector3Make(pt.x, pt.y, pt.z)]];

稍后,使用此位置数组创建如下几何:

SCNVector3 positions2[pointCount];
for (int j = 0; j < pointCount; j++)
    {
        SCNVector3 value = [[positions objectAtIndex:j] SCNVector3Value];
        SCNVector3 value1 = [_viewpoint2.scene.rootNode convertPosition:value fromNode:nil];
        positions2[j] = value1;
    };

    SCNGeometrySource *vertexSource1 =
    [SCNGeometrySource geometrySourceWithVertices:positions2 count:pointCount];

    NSData *indexData1 = [NSData dataWithBytes:indices2
                                       length:sizeof(indices2)];

    SCNGeometryElement *element1 =
    [SCNGeometryElement geometryElementWithData:indexData1
                                  primitiveType:SCNGeometryPrimitiveTypeLine
                                 primitiveCount:pointCount
                                  bytesPerIndex:sizeof(int)];

    SCNGeometry *geometry1 = [SCNGeometry geometryWithSources:@[vertexSource1]
                                                    elements:@[element1]];
    SCNNode* lineNode1 = [SCNNode nodeWithGeometry:geometry1];
    [_viewpoint2.scene.rootNode addChildNode:lineNode1];

我观察到的是,绘制的弧线大于船舶的旋转角度,因此如果船舶旋转30度,则绘制的弧度约为60度左右......应该与船舶旋转相同...想知道我在做什么不正确吗?

0 个答案:

没有答案