OrbitViewer question: you can set what it orbits, but can you set where it starts?

When you initialize an OrbitViewer you can tell it what position to orbit, and the allowable range of distance from that position, and the starting distance from that position.

But can you tell it where to position itself to start the scene?

For example say I have a plane like the ground in the Learn Craft project, and I want to use an OrbitViewer in the scene, but I want the camera to start the scene at a position high above the plane, looking down on it.

Can that be done?

No matter how I define its position beforehand, in setup or in parameters or what have you, OrbitViewer seems to always set itself back to 0,0,0 when a scene starts.

Try looking at this:

dave1707 physics demo

from @dave1707 demo

@Bri_G I may be missing something but it seems as if you thought I was asking how to use the OrbitViewer at all, which I wasn’t.

Unless I’m missing it, that project does not set a custom location for the OrbitViewer, it just uses it in its default position.

Maybe I stated my problem unclearly, I will try to edit it to phrase it more clearly.

Try setting ‘viewer.rx’ and ‘viewer.ry’. These control the viewer rotation which is used to place it as well.

Oh btw these are angles in degrees


My bad, did kinda miss the point. Only other place might help is in one of the demos supplied -

viewer = scene.camera:add(OrbitViewer, myEntity.model.bounds.center, 30, 20, 100)

Which looks like the centre point of any model, but that’s not to say it’s working properly. Have you tried using this approach?

@Bri_G I agree that’s the natural place to start experimenting. I wasn’t able to get anywhere with it.

@John here’s what I’ve tried, which should be correct:

    viewer.rx = -12.237
    viewer.ry = -22.515
    viewer.rz = 2.033

It moves the viewer, but not to the right place.

Here’s what I want:

    myScene.camera.position = vec3(41.482,-7.358,-0.811)
    myScene.camera.eulerAngles = vec3(-12.237,-22.515, -0.0)

Can you suggest how I get the viewer at that location and direction?

@John how do I convert degrees to Euler angles?

As for positioning I can think of a kludge where I set the center of the orbit viewer to be an invisible entity that’s exactly where I want the viewer, and then set all of the proximity values to 0…

But then I have to change it once the scene starts, is that possible?

@Bri_G , @John:

reporting from the field:

  • The kludge works. Define an invisible entity and initialize the viewer at 0 distance from it, and presto, viewer is in custom position.
  • Note: I actually had to define it as 0.05 from it with minimum distance at 0.005, or else the viewer would not zoom.
  • viewer.rx and viewer.ry seem to work as expected in this situation–meaning they work exactly like eulerAngles. Whatever values they are set to will become the main camera entity’s eulerAngles.
  • You cannot apparently change a viewer’s center on the fly, so whatever starting position you set, you’re stuck with
  • If you replace the viewer with a different viewer, the camera’s position resets. So unless that’s the effect you want, you’re still stuck with whatever position you start with.

Years later I’ve figured this out.

function startCameraAtPosition(ry, rx, zoom, targetPosition)
    viewer = scene.camera:add(OrbitViewer, vec3(0,0,0), 30, 2, 300)
    viewer.ry = ry
    viewer.rx = rx
    viewer.zoom = zoom
    viewer.target = targetPosition

Where targetPosition is a vec3 location for the OrbitViewer to point at.

The startZoom, minZoom, and maxZoom numbers at the end of the camera:add command can of course be customized too.

Not to beat a dead horse here, but I spent so much time struggling with this that I hope I can help someone else get a handle on it as fast possible.

To which end I’m attaching a project that demonstrates how to do this. Position the camera anywhere you like, then tap the “save position” button and exit. When you restart, the camera will be in the same position as it was when you quit.

@UberGoober The saveprojecttab causes an error.

Main:41: Invalid buffer name
stack traceback:
	[C]: in function 'saveProjectTab'
	Main:41: in function 'saveSettings'
	Main:20: in function <Main:20>

@UberGoober Apparently you have to import the zip file into Codea for it to work. If you unzip the file in the Files app and run the program from there, you get the above error.

That makes sense.