Rotating an Entity towards the camera transform

Hey there,


I'm new to working within the 3D space and 3D calculations so forgive me if this question is very basic. I'm facing the following case;


I have a single entity in the scene that can be positioned anywhere, limited only by a certain radius- it can be positioned in front of the camera, behind it or to the left or right of it. Once it is positioned it has a transform's rotation property set to simd_quatf(real: 0.0, imag: SIMD3<Float>(0.0, 0.0, 0.0)).

I'd like to modify this rotation property so that it would face the camera when I'd look at it. So even if I'm facing the opposite direction of where my entity is placed, when I turn around, I'd like it to be facing the camera directly. What calculations do I need to apply in order to achieve this?

hi, Using the method HasTransform.look() has worked great for me, you just have to make sure the direction RealityKit thinks is forward on your model is the same direction you do. https://developer.apple.com/documentation/realitykit/hastransform/3244204-look You just set ‘at’ to be the entity’s position, and ‘to’ should be the camera transform’s position.

Thank you for your response
Just to make sure, since the look() method accepts values of SIMD3<Float> for target & position parameters, I should just pass the Transform.translation value of my camera & the entity?

Yes exactly 🚀

While this does seem to work, I've encountered the following issues:


1) The 'HasTransform.look()' method, according to the documentation, also 'moves the entity to the specified `position` which is exactly what's happening in my case. The node gets rotated correctly but is placed to the position of the camera. I tried changing the order of parameters to see what would happen but it ended up as a big mess.

2) I should have mentioned this sooner, my bad, but this method also rotates around other axes while I would only like it to rotate around the Y axis.


Any idea how I could go about this?

It should be straightforward to fix those issues, try something like this:


entity.look(at: camera.transform, from: entity.position, upVector: [0, 1, 0])

It does depend on your entity to not be a child of something else in the scene with a non identity transform. Otherwise you have to find the camera transform relative to the entity's parent, there are several conversion methods in the HasTransform docs here.


As a side note, I noticed that this look() function also scales your object, so watch out for that if you have an object with a non [1,1,1] scale.

There is an example app in the ARKit docs that demonstrates something like this by orienting a robot head to always face the camera. See: https://developer.apple.com/documentation/arkit/combining_user_face-tracking_and_world_tracking


At any given time, the sample calculates the transform facing the camera by using a camera anchor-entity, and at any given time taking the inverse of its transformMatrix. See the sections on positioning and orienting.

Rotating an Entity towards the camera transform
 
 
Q