0

This little method that I wrote, changes spotlight1's position to the unexpected value.

If I understand well, setPosition method should set spotlight's translation relative to the tv's position

TV's translation: [0.0, 0.0, -5.0] setPosition to [0.0, 5.0, 0.5] relative to Tv's translation.

So:

[0.0 + 0, 0.0 + 5, -5.0 + 0.5] = [0.0, 5.0, -4.5] 

But what I get is:

[0.0, 0.9999994, -4.9]

Am I missing some important information here?

func loadLights() {
    arView.scene.addAnchor(lightAnchor)
    lightAnchor.addChild(spotlight1)
    print(tv?.position)             // 0.0, 0.0, -5.0
    spotlight1.setPosition([0, 5, 0.5], relativeTo: tv)

    if let tv = tv {
        spotlight1.look(at: tv.position, 
                      from: spotlight1.position, 
                relativeTo: nil)
    }        
    print(spotlight1.position)     // 0.0, 0.99999994, -4.99
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Jakub Gawecki
  • 801
  • 2
  • 6
  • 14

1 Answers1

1

Reference Coordinate Frame

As paradoxical as it may sound, RealityKit did everything right. You need to take into account the frame of reference (frame of your tv model). As far as I understand, you reduced the scale of the tv model by five times. Am I right? However, reference's transform matters.

In other words, you've scaled down the local coordinate frame (a.k.a. local coordinate system) of the TV model, you're trying to position spotLight1 relative to.


About relativeTo parameter

Read this post and this post to explore possible issues.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • 1
    Hey Andy. Not sure if I follow. I did not want to reduce tv's scale 5 times. My intention was to: – Jakub Gawecki May 31 '22 at 10:29
  • That's why it's a good practice to put ModelEntity inside Entity to separate two transforms. :) – Andy Jazz May 31 '22 at 10:34
  • Sorry, it seems that it cut a part of my comment.. – Jakub Gawecki May 31 '22 at 13:01
  • Are we talking about creating a subclass of entity "TV", and assigning a model to it? Or create a nested 'tv' variable within that new subclass, and then assigning that entity there? And after that, which translation is the best practise to modify? Thanks! – Jakub Gawecki May 31 '22 at 13:09
  • You need to put a nested model inside an empty entity (`entity.addChild(model)`). Entity is like a container for your model. – Andy Jazz May 31 '22 at 18:57