Create Wall 3D math oriented away from camera

127 Views Asked by At

I have 2 Points which has x,y,z let's say from and to I am drawing wall between them using ARkit ios

To draw wall I use static height let's say 5 Meters

    node.position = SCNVector3(from.x + (to.x - from.x) * 0.5,
                               from.y + height * 0.5,
                               from.z + (to.z - from.z) * 0.5)

now I use following code to set angles

       // orientation of the wall is fairly simple. we only need to orient it around the y axis,
        // and the angle is calculated with 2d math.. now this calculation does not factor in the position of the
        // camera, and so if you move the cursor right relative to the starting position the
        // wall will be oriented away from the camera (in this app the wall material is set as double sided so you will not notice)
        // - obviously if you want to render something on the walls, this issue will need to be resolved.

    node.eulerAngles = SCNVector3(0,
                                  -atan2(to.x - node.position.x, from.z - node.position.z) - Float.pi * 0.5,
                                  0)
    

And I am rendering video into the wall.

Issue is If I draw from point1 to point2 (drag to right) Video is perfectly fine. But I start from point2 to point1 video is flipped because of eulerAngles is different in both case

Angle point1 to point2 --> (0.000000 -1.000000 0.000000 3.735537)

Angle point2 to point1 -- > (0.000000 -1.000000 0.000000 0.478615)

Here is uploaded images of result

https://stackoverflow.com/questions/53282351/arkit-add-2d-video-flipped-by-x

Any help would be appreciated.

1

There are 1 best solutions below

0
On

I have fixed issue with temporary solution still looking for a better one

Issue is of wall. wall is flipped other side of camera when draw from right to left direction. I have figure it out by setting isDoubleSided = false and by applying a image as diffuse contents which has text and I can see that image is flipped itself.

I have tried many things but this one helps me

  1. Normalize vector
  2. Find the cross between to SCNVectors
  3. If y > 0 I just swapped from and to Value

Code

    let normalizedTO = to.normalized()
    let normalizedFrom = from.normalized()
    let angleBetweenTwoVectors = normalizedTO.cross(normalizedFrom)

    var from = from
    var to = to
    if angleBetweenTwoVectors.y > 0 {
        let temp = from
        from = to
        to = temp
    }

// Inside extension of SCNVector3
func normalized() -> SCNVector3 {
    if self.length() == 0 {
        return self
    }

    return self / self.length()
}

func cross(_ vec: SCNVector3) -> SCNVector3 {
        return SCNVector3(self.y * vec.z - self.z * vec.y, self.z * vec.x - self.x * vec.z, self.x * vec.y - self.y * vec.x)
    }

Hope it is helpful. If anyone know better solution please answer it.