77613

How do I rotate an arkit 4x4 matrix around Y using Apple's SIMD library?

I am trying to implement some code based on an ARKit demo where someone used this helper function to place a waypoint

let rotationMatrix = MatrixHelper.rotateAboutY( degrees: bearing * -1 )

How can I implement the .rotateAboutY function using the SIMD library and not using GLKit? To make it easier, I could start from the origin point.

I'm not too handy with the matrix math so a more basic explanation would be helpful.

Answer1:

The rotation around Y matrix is:

| cos(angle) 0 sin(angle)| | 0 1 0 | |-sin(angle) 0 cos(angle)|

Rotation counter-clockwise around Y:

|cos(angle) 0 -sin(angle)| | 0 1 0 | |sin(angle) 0 cos(angle)|

So, we can easily construct the matrix using simd (Accelerate Framework):

func makeRotationYMatrix(angle: Float) -> simd_float3x3 { let rows = [ simd_float3(cos(angle), 0, -sin(angle)), simd_float3(0, 1, 0), simd_float3(-sin(angle), 0, cos(angle)) ] return float3x3(rows: rows) }

The angle here is in radians. More info here.

Answer2:

Probably the best way to leverage the built-in SIMD library for such operations (at least, with "best" meaning "do the least math yourself") is to express rotations with quaternions and convert to matrices when needed.

<ol> <li>Construct a simd_quatf representing the axis-angle rotation you want to apply.</li> <li>Convert that quaternion to a 4x4 matrix.</li> </ol>

You can do that with a one-line call :

let rotationMatrix = float4x4(simd_quatf(angle: radians, axis: axis))

Expanding to your "rotateAboutY" case:

let radians = degreesToRadians(-bearing) // degrees * .pi / 180 let yAxis = float3(0, 1, 0) let rotationMatrix = float4x4(simd_quatf(angle: radians, axis: yAxis))

Of course, once you have a rotation matrix, you can apply it using the * or *= operator:

someNode.simdTransform *= rotationMatrix

If you find yourself doing something like this often, you might want to write an extension on float4x4.

<sub>Tip: By the way, in Swift (or C++ or Metal shader language) you don't need the simd_ prefix on most (but not all) SIMD types and global functions.</sub>

Recommend

  • Python - Shoot a bullet in the direction (angle in degrees) my spaceship is facing
  • UIBezierPath not updated as expected
  • how to do power of complex number in CUBLAS?
  • model.solve() method is not working (“CPLEX DLL not found”) for DOcplex for Python
  • Three js, get the vector3 that pointerlockcontrolsi s facing
  • How do I make a continuous loop with the animate method?
  • How to get phone heading for augmented reality?
  • Rails: Converting from MySQL to PostGres breaks Geokit Distance Calculations?
  • Geometry shader doesn't do anything when fed GL_POINTS
  • Getting Coordinates of a Point in a circle
  • How to setCenter mapview with location in google maps sdk for iOS
  • Python sin and cosine giving incorrect values
  • SEO friendly 301 redirect .htm to .aspx
  • Recreate Apple Watch fitness tracker ‘progress’ bar - gradient on CAShapeLayer stroke
  • Animating an SVG Group
  • How can I use a custom function within an expression using the eval dataframe method?
  • How do I generate a Sine Sweep in Java (Android)
  • VB.Net Double comparison after some additions
  • get all processes in parallel
  • Zeromq with python hangs if connecting to invalid socket
  • Jquery UI Sortable, move item automatically
  • Tools for understanding HTML layout
  • Why isn't my “Fizz Buzz” test in R working?
  • Python PIL to extract number from image
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • Where these are stored?
  • apply a javascript function to draggable copy
  • CodeIgniter URI Parameter is partially bypassing an “if” statement
  • Aptana 3 remove bundle (jquery)
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • netsh acl setting (need alternative method - registry settings?)
  • opencv display image without x server
  • CSS bleed-through with cfinput type=“datefield”
  • how do i write assembly code from c#?
  • How VBA declared Volatility works
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • How to handle AllServersUnavailable Exception