0
votes

In SceneKit using Swift I'm trying to generate the face of a square that is aligned with the orientation of the sceneView's pointOfView node, but I'm having trouble understanding the math required to do this.

All I need to do is take the current location of the node (SCNVector3) and generate these points relative to the initial center point, and perpendicular to the forward direction of the camera: enter image description here

I have the forward direction and current position:

let mat = self.sceneView.pointOfView.transform
let forwardDirection = SCNVector3(-1 * mat.m31, -1 * mat.m32, -1 * mat.m33)
let position = self.sceneView.pointOfView!.position

I just haven't been able to figure out the rest of the math around this.

1

1 Answers

0
votes

The pointOfView node is basically the active camera. So you could create a SCNNode to hold the face (by creating its SCNGeometry), position the node as desired, and then add that node as a childnode of the camera. Then if you move around the camera the face node will follow.

Another approach that may suit better based on your limited description would be to use the spritekit overlay scene (since it’s just a single face).

EDIT: based on your comment below, please find included the following code (it's in OBJ C but it does the math you seem to be looking for). I use it for something similar, to align to a normal (the axis variable below is the input normal). The resulting matrix rotMat applied to the coords in your image should do the trick.

    // Find a vector in the plane
    GLKVector3 tangent0 = [self crossVector:axis : GLKVector3Make(1, 0, 0)];
    if ([self dotVector:tangent0 : tangent0] < 0.001) {
        tangent0 = [self crossVector: axis: GLKVector3Make(0, 1, 0)];
    }
    tangent0 = [self normalizeVector: tangent0];

    // Find another vector in the plane
    GLKVector3 tangent1 = [self crossVector: axis: tangent0];
    tangent1 = [self normalizeVector: tangent1];

    //construct a 4x4 matrix using tangents and initial axis 
    GLKVector4 tangent0GLK4 = GLKVector4Make(tangent0.x, tangent0.y, tangent0.z, 0);
    GLKVector4 tangent1GLK4 = GLKVector4Make(tangent1.x, tangent1.y, tangent1.z, 0);
    GLKVector4 axisGLK4 = GLKVector4Make(axis.x, axis.y, axis.z, 0);

    GLKMatrix4 rotMat = GLKMatrix4MakeWithColumns(tangent0GLK4, tangent1GLK4, axisGLK4, GLKVector4Make(0, 0, 0, 1));