SceneKit's look(at:up:localFront:) not working right in certain scenarios - swift

This can be reproduced using the default Xcode 3D game template (SpaceShip).
Remove the runAction line that rotates the ship in the GameViewController.
Add this line. The ship correctly faces away from the camera.
Note the very small x component in the at: parameter.
ship.look(at: SCNVector3(0.001, 0, -100.0), up: SCNVector3(0, 1, 0), localFront: SCNVector3(0, 0, 1)) // Works! Ship faces away from camera.
However, it the x component of the at: parameter is exactly zero, the ship's orientation does not change. The ship continues to face the camera instead of facing away from it.
ship.look(at: SCNVector3(0, 0, -100.0), up: SCNVector3(0, 1, 0), localFront: SCNVector3(0, 0, 1)) // DOES NOT work
Seeing the same problem with simdLook()

For starters, if you show the world origin sceneView.debugOptions = ARSCNDebugOptions.showWorldOrigin], you will see that the x and y axis are actually opposite to what they would normally be, i.e. x is now y and y is now x.
Second, you are changing the x axis and not z axis. If you want the spaceship to look away or at you, that runs along the z axis. Negative(-) z will look at you where Positive(+)z will look away from you. Trying just changing the -100 to +100.

Related

CGAffineTransform tanslationX or translatedBy

I'm getting confused.
Using vision, I transform bottom-left coordinates to top-left by
CGAffineTransform(scaleX: 1, y: -1).translatedBy(x:0, y: -1)
however to rotate the camera view according to the orientation I
CGAffineTransform(translationX: 1, y: 0), rotated(by: -CGFloat.pi / 2)
why in the second case do we use CGAffineTransform(translationX... rather than CGAffineTransform(scaleX..
What is the difference between the two?
So why to transform bottom-left coordinates to top left do we use scale
So your question really is: Why is
CGAffineTransform(scaleX: 1, y: -1).translatedBy(x:0, y: -1)
the way to flip?
Let's start with the scale part. Scaling the y coordinate system to -1 is a multiplication: it reverses the scale so that up is down. That's the flip. (Scaling the x to 1 just means "leave it alone".)
The translate part is because transforms take place around the origin (the bottom left corner, originally). So when we flip by scaling, we flip ourselves right off the screen. In order to compensate for that, we slide back onto the screen.
They have a completely different meaning from each other, as per Apple doc:
.translatedBy
Returns an affine transformation matrix constructed by translating an
existing affine transform.
.scaledBy
Returns an affine transformation matrix constructed by scaling an
existing affine transform.

RealityKit – Create line between two points in 3d space

How to create a line between two points in 3d space with RealityKit?
There are examples of creating lines between two points in Scenekit, however, there are basically none using RealityKit.
To create the line, I've created a rectangle model entity and placed it between my first touched point and the current touched point. From here, all I would need to do is rotate the rectangle to face the current touched point. However, using the simd_quatf(from: to:) doesn't work as intended.
rectangleModelEntity.transform.rotation = simd_quatf(from: firstTouchedPoint,
to: currTouchedPoint)
If I were to touch a point and then drag directly downwards, the rectangle model should be to be a straight line between first touched and current touched point, but it stays horizontal with a slight tilt.
To solve this, I tried getting the angle between my initially horzontal line as a vector and a vector from the first touched to current touched point
let startVec = currTouchedPoint - firstTouchedPoint
let endVec = endOfModelEntityPoint - modelEntityCenterPoint
let lengthVec = simd_length(cross(startVec, endVec))
let theta = atan2(lengthVec, dot(startVec, endVec))
This gives me the angle between two vectors in 3d space, which seems correct, when I checked it gave me 90 degrees when touching and dragging directly between it.
The problem is I don't know what the axis to rotate it on should be. Since this is 3d space, the line doesn't need to be on a 2d plane, the current touched position can be downwards and in front of the starting touch position.
rectangleModelEntity = simd_quatf(angle: theta, axis: ???)
Personally, I'm not even too sure if the above is the correct solution to creating a line between two points. In theory it's rather basic, create a rectangle with low height/depth to mimic a line, position it in the center of the starting and current touch point then rotate it so it's oriented correctly.
What should be the axis for the above degrees between two vectors?
Is there a better method of creating two lines between points in 3d space with RealityKit/ARKit?
I have implemented using a box. Let me know if you have a better way.
let midPosition = SIMD3(x:(position1.x + position2.x) / 2,
y:(position1.y + position2.y) / 2,
z:(position1.z + position2.z) / 2)
let anchor = AnchorEntity()
anchor.position = midPosition
anchor.look(at: position1, from: midPosition, relativeTo: nil)
let meters = simd_distance(position1, position2)
let lineMaterial = SimpleMaterial.init(color: .red,
roughness: 1,
isMetallic: false)
let bottomLineMesh = MeshResource.generateBox(width:0.025,
height: 0.025/2.5,
depth: meters)
let bottomLineEntity = ModelEntity(mesh: bottomLineMesh,
materials: [lineMaterial])
bottomLineEntity.position = .init(0, 0.025, 0)
anchor.addChild(bottomLineEntity)
The axis is the cross product of the direction your object is facing at the beginning and the direction it should be facing now.
Like if it's at position p1=[x1,y1,z1], initially facing d1=[0, 0, -1], and you want it to face a point p2=[x, y, z] the axis would be the cross product: |d1|✕|p2 - p1|.
May have to swap the two around, or just negate the angle though, depending on how it works out.

Reset "natural" orientation of part to 0,0,0 without moving it

I downloaded several sword models from the marketplace. Then, in workspace, oriented them all the same (eg handles all on same side, blade flat to ground). I then moved them into ReplicatedStorage. Later, I clone them and place them back in the workspace.
When they return to workspace, they are back to their original (ie marketplace) orientation. I have to rotate them again in code.
Is there a way to reset the "natural" orientation of a part/mesh?
By this I mean;
Create or download model from market place
rotate/transform to desired position
do "something" which resets the orientation to 0, 0, 0 WITHOUT visually rotating the model.
I hope this makes sense!
Let's assume you have set the sword model's PrimaryPart to the handle. To change the PrimaryPart's CFrame as well as all the other parts in the model relative to it, we use the :SetPrimaryPartCFrame() function
sword:SetPrimaryPartCFrame(CFrame.new(0, 0, 0)) -- 0, 0, 0 as in X = 0, Y = 0, Z = 0
Now, let's say you aren't satisfied with the orientation of the model. We can fix this by "multiplying" the location CFrame with an orientation CFrame (CFrame.fromOrientation())
Orientation CFrames are expressed in radians, so in order to convert radians to degrees, we use the math.rad() function
In this code example we set the location to 0, 0, 0 and orientation to 0, 0, 90
sword:SetPrimaryPartCFrame(CFrame.new(0, 0, 0) * CFrame.fromOrientation(0, 0, math.rad(90)))
The only thing left to do is to experiment with various orientation X, Y and Z values until you get the correct result

SCNNode Rotation Multiple Axes

This Question was posted, but never answered.
Similar to This Question, I am trying to understand SCNNode.rotation as a 4D vector. The prior question utilizes an example that only manipulates 1 axis, i.e.,
SCNNode.rotation = (0, 0, 1, degToRad(45)) //Rotate about z-axis by 45 degrees
which makes sense; however, what if I wanted to rotate the X axis by 20 degrees, Y axis by 45 degrees and then Z axis by 78 degrees?
SCNNode.rotation = ??
I would provide code I've tried, but I don't understand conceptually the notion of a 4D rotation vector.
Every node just has a transform with 4x4 matrix. So all the rotation operations are reflecting in the changing the transform.
In this case, if you change either of rotation, eulerAngles and orientation, you are supposed to get same value.
If rotating about three axises, I suggested using eulerAngles.
node.eulerAnges = SCNVector3(x:degToRad(20),y:degToRad(45), z:degToRad(78))
After you set this, go back and check to value of rotation:
SCNVector4(x: -0.16975601, y: 0.5943193, z: 0.786109, w: 1.448788)
This means there is an axis going through point(-0.16975601, 0.5943193, 0.786109) and origin (0,0,0), and node is rotating around it for 1.448788 (82 degree).

Placing "SCNNode" at a given position and facing "SCNCamera" with offset around Y-axis

So,
I have the exact position I want to place the node at. If I test things with a sphere geometry I can place spheres in the world by telling the node:
node.simdPosition = position
(I provide the "position" as an input to the function).
That successfully places the object in the world exactly where I want it to go.
What I really want to do is placing a plane:
let plane = SCNPlane(width: 0.2, height: 0.3)
plane.cornerRadius = plane.width / 10
plane.firstMaterial?.diffuse.contents = UIColor.red
plane.firstMaterial?.specular.contents = UIColor.white
let node = SCNNode(geometry: plane)
Then telling it to be placed at the "position":
node.simdPosition = position
All this works with the plane as well. What I have problems with is the angle:
I want to tell the plane's node to be placed with a given "angle" (around Y) offset to the camera. I tried this but it's not working:
node.rotation = SCNVector4Make(0, 1, 0, currentFrame.camera.eulerAngles.z - angle)
So then, the question is, how can a node be placed at a certain position and at the moment it gets placed in the world, also have a certain Y angle offset from the perpendicular to the camera?
I was using the wrong Euler angle... (z)
This made it work:
node.eulerAngles = SCNVector3Make(0, cameraEulerAngles.y - Float(0.7), 0)