\n
Code:
\n// Get the lookat scene origin matrix for the rectangle\nvar lookAtSceneOriginGLKMatrix = GLKMatrix4Identity\nvar lookAtSceneOriginInverse = GLKMatrix4Identity\nvar rectangleModelMatrix = float4x4()\nvar lookAtSceneOriginMat = float4x4()\n\nlookAtSceneOriginGLKMatrix = GLKMatrix4Multiply(lookAtSceneOriginGLKMatrix, GLKMatrix4MakeLookAt(self.currentSatX * self.appDelegate.getCurrentRectangle().getRenderHeight(), self.currentSatY * self.appDelegate.getCurrentRectangle().getRenderHeight(), self.currentSatZ * self.appDelegate.getCurrentRectangle().getRenderHeight(), 0, 0, 0, 0, 1, 0))\nvar isInvertible = true\nlookAtSceneOriginInverse = GLKMatrix4Invert(lookAtSceneOriginGLKMatrix, &isInvertible)\nlookAtSceneOriginMat = float4x4(convertToFloat4x4FromGLKMatrix: lookAtSceneOriginInverse)\nrectangleModelMatrix = accumulatedRotationMatrix * lookAtSceneOriginMat * float4x4(scaleBy: 0.35)\n\n// Draw the rectangular plane\nscene.rectanglePlane?.draw(\n encoder: commandEncoder, modelMatrix: rectangleModelMatrix,\n projectionMatrix: projectionMatrix * viewMatrix\n)\n
\n","author":{"@type":"Person","name":"PhilBot"},"upvoteCount":3,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"As mentioned in a Comment your up vector always pointing to the direction of y axis. It should be perpendicular to the direction vector.
\nlet x = self.currentSatX * self.appDelegate.getCurrentRectangle().getRenderHeight()\nlet y = self.currentSaty * self.appDelegate.getCurrentRectangle().getRenderHeight()\nlet z = self.currentSatZ * self.appDelegate.getCurrentRectangle().getRenderHeight()\n\nlet direction = GLKVector3Make(x, y, z) //vector from planes postion to origin\n\nlet yAx = GLKVector3Make(0, 1, 0) // y axis\n\nlet side = GLKVector3CrossProduct(direction, yAx) //perpendicular vector to direction\n\nlet up = GLKVector3CrossProduct(side, direction) //perpendicular vector to side\nlet normalizedUp = GLKVector3Normalize(up)\n\nlookAtSceneOriginGLKMatrix = GLKMatrix4Multiply(lookAtSceneOriginGLKMatrix, GLKMatrix4MakeLookAt(x, y, z, 0, 0, 0, normalizedUp.x, normalizedUp.y, normalizedUp.z))\n
\n","author":{"@type":"Person","name":"udi"},"upvoteCount":1}}}Reputation: 60
I am developing a 3D scene on iOS 16.1 with Xcode 14.3. I have a rectangular plane orbiting the origin at an incline and I want to use GLKMatrix4MakeLookAt to cause the plane to always face the origin.
I am using the code below. The plane does 'face' the origin when it rotates, however I am also seeing this undesired 'rocking' ( yaw ) back and forth. Please see the attached video to understand how it looks.
Why does it do this rocking? How can I get rid of the rocking but still have the plane orbit the origin and always face it? Thank you.
Code:
// Get the lookat scene origin matrix for the rectangle
var lookAtSceneOriginGLKMatrix = GLKMatrix4Identity
var lookAtSceneOriginInverse = GLKMatrix4Identity
var rectangleModelMatrix = float4x4()
var lookAtSceneOriginMat = float4x4()
lookAtSceneOriginGLKMatrix = GLKMatrix4Multiply(lookAtSceneOriginGLKMatrix, GLKMatrix4MakeLookAt(self.currentSatX * self.appDelegate.getCurrentRectangle().getRenderHeight(), self.currentSatY * self.appDelegate.getCurrentRectangle().getRenderHeight(), self.currentSatZ * self.appDelegate.getCurrentRectangle().getRenderHeight(), 0, 0, 0, 0, 1, 0))
var isInvertible = true
lookAtSceneOriginInverse = GLKMatrix4Invert(lookAtSceneOriginGLKMatrix, &isInvertible)
lookAtSceneOriginMat = float4x4(convertToFloat4x4FromGLKMatrix: lookAtSceneOriginInverse)
rectangleModelMatrix = accumulatedRotationMatrix * lookAtSceneOriginMat * float4x4(scaleBy: 0.35)
// Draw the rectangular plane
scene.rectanglePlane?.draw(
encoder: commandEncoder, modelMatrix: rectangleModelMatrix,
projectionMatrix: projectionMatrix * viewMatrix
)
Upvotes: 3
Views: 93
Reputation: 3853
As mentioned in a Comment your up vector always pointing to the direction of y axis. It should be perpendicular to the direction vector.
let x = self.currentSatX * self.appDelegate.getCurrentRectangle().getRenderHeight()
let y = self.currentSaty * self.appDelegate.getCurrentRectangle().getRenderHeight()
let z = self.currentSatZ * self.appDelegate.getCurrentRectangle().getRenderHeight()
let direction = GLKVector3Make(x, y, z) //vector from planes postion to origin
let yAx = GLKVector3Make(0, 1, 0) // y axis
let side = GLKVector3CrossProduct(direction, yAx) //perpendicular vector to direction
let up = GLKVector3CrossProduct(side, direction) //perpendicular vector to side
let normalizedUp = GLKVector3Normalize(up)
lookAtSceneOriginGLKMatrix = GLKMatrix4Multiply(lookAtSceneOriginGLKMatrix, GLKMatrix4MakeLookAt(x, y, z, 0, 0, 0, normalizedUp.x, normalizedUp.y, normalizedUp.z))
Upvotes: 1