Orbiting camera

Started by JonnyDee, August 26, 2003, 05:20:15 AM

Previous topic - Next topic

JonnyDee

Hi,

I am currently trying to figure out how to rotate the camera around an object.  The effect i'm looking for is for the player object to always be maintained in the center of the view, whilst the camera orbits around the player and the player rotates so the camera is always looking at the same side of the player.  I have tried to use a dummy object which rotates around the player to give the coords to move the camera to, like this:

playerRotation = (float)(xDif * rotationMultiplier);

// Rotate the player and cameraHolder
cameraHolder.setRotationPivot(new SimpleVector(sphereX, sphereY, sphereZ));
sphere.rotateY((float)Math.toRadians(playerRotation));
cameraHolder.rotateY((float)Math.toRadians(playerRotation));

cameraX = cameraHolder.getCenter().x;
cameraY = cameraHolder.getCenter().y;
cameraZ = cameraHolder.getCenter().z;

// Move the camera back to the cameraHolder and look at the player
theWorld.getCamera().setPosition(
 new SimpleVector(
   cameraX,
   cameraY,
   cameraZ));
theWorld.getCamera().lookAt(new SimpleVector(sphereX, sphereY, sphereZ));

Whilst debugging this code, i've found that cameraX, Y and Z don't change from the initial values.  I can see the player rotating and if I use align to align the camera to the player, the camera rotates when I move the mouse, as it should, but I can no longer see the player!  Am I using getCenter() incorrectly?  I tried using calcCenter() before getting the cameraHolder center to reset the camera position, but this sets cameraX, Y and Z to NaN.

Any help would be appriciated!

Thanks,

Jon

EgonOlsen

I see...the problem is, that getCenter() returns you the center in object-space while you want to get the center transformed into world-space. It should be possible to emulate this behaviour somehow, but i think it's better to add a new method like getTransformedCenter() to Object3D and maybe a convenience method to do the camera placement using this value to Camera too.
I'll add this to the next release, which should be out in the next 48h (i'll try to get things sorted today) and YOU will have to play beta-tester for this method  :lol:
I'll let you know...
BTW.: I'll document this behaviour of getCenter() in the docs, because it seems to be misleading the way it is documented now.

JonnyDee

Thanks for the speedy reply!  As soon as the feature is available, i'm happy to be the beta tester  :D .  An easy way to place the camera using a helper method would be excellent and just what i'm looking for - looking forward to the new version!

Cheers,

Jon

EgonOlsen

The beta of the new version available for download here:

EDIT: Removed the link. This version is now official.

The docs and the manual are still in need of some work and that's why this is not an "official" release, but this way you can already test the features you requested. This version is a major update, because some internals have changed and some obsolete stuff from Config has been removed. So in case of problems, please let me know.
Here's a list of the changes until now:

0.92 - Extended matrix-locking to cover the camera too (if locking is used). Worked on Matrix and SimpleVector to increase performance of some methods slightly. Removed unused transparency constants from Object3D. Added support for tiled textures (i.e. texture coordinates below zero and larger than 1) for the SoftGL-renderer. This requires that even the textures used for software-rendering only have to have a height of a power of 2 (jPCT will convert non-power-of-2-textures). Added some methods to SimpleVector, Camera and World to support first-person-shooter-like navigation better. Fixed a bug in the OcTree that caused collisions detection to fail in multi-threaded applications in some rare cases. Fixed a bug in the ray/polygon-collision detection in combination with octrees. Added an algorithm to automatically speed-up collision detection. Modified z-Sorting to ensure a defined sorting order (per frame) even for polygons with equal depth. Removed some unused or now useless configuration variables from Config (public and non-public ones). Reworked both software-renderers to be more pixel-perfect. Removed artifacts from the OptiZ-algorithm. It should work flawless in every situation now. Added the possibility to retrieve an object's center in worldspace.

Oh, and thanx for trying to call calcCenter() on a dummy-object... :lol:  It was bugged because it caused a division by zero in this case. Why the VM thinks that setting it to NaN instead is a better idea than throwing the exception, is still beyond my understanding. But it sometimes does this and after it, floating point code slows down to a crawl... :?

JonnyDee

Thanks for the new version - getTransformedCenter() now works a treat!  For some reason my camera is moving in an elliptical path, rather than a circle as I intended, but I think that is my code  :D.

JonnyDee

Hmmm - i've had a further look at it, and I can't work out why the camera should be moving in an ellipse.  Also, the player doesn't rotate, unless I remove the cameraHolder rotation.  If I remove that line, the player rotates correctly, but obviously then the camera doesn't!  I'm wondering if this is related to the way i'm setting the rotation pivot? I tried setting it to player.getYAxis(), but then the camera doesn't rotate at all.  I have posted my code below in case i'm being an idiot, (very likely  :D )

playerRotation = xDif * rotationMultiplier;

// Rotate the player and cameraHolder
cameraHolder.setRotationPivot(player.getTransformedCenter());
player.rotateY((float)Math.toRadians(playerRotation));
cameraHolder.rotateY((float)Math.toRadians(playerRotation));

// Move the camera back to the cameraHolder and look at the player
theWorld.getCamera().setPosition(               cameraHolder.getTransformedCenter());
theWorld.getCamera().lookAt(player.getTransformedCenter());

EgonOlsen

First, you can't do that:

cameraHolder.setRotationPivot(player.getTransformedCenter());

because you are mixing worldspace and object-space coordinates here, which isn't what you want.
Second, it seems to be my fault...the getTransformedCenter() seems to be fishy. I'm at work, so i could only do some small tests and don't have the source available, but the returned center seems to be a screwed up variant of the actual center. I really wonder why it worked in my short test yesterday... :?:
I got the thing you want working even with the buggy method, but i had to swap coordinates in the SimpleVector and rotate the dummy object around x instead of y....strange...
I'll have a look at the problem, but i'm not sure if i manage to do so today.

EgonOlsen

Fixed it...try the updated version using the same link as above. I was mixing up the coordinates in the method, so the results were totally wrong *most* of the time, but somehow not in my test-case...
Anyway, it should work now. I got it working like this:


      cube=Primitives.getCube(20);
      cube.setEnvmapped(Object3D.ENVMAP_ENABLED);
      theWorld.addObject(cube);
      cube.build();

      dummy=Object3D.createDummyObj();
      dummy.setCenter(new SimpleVector(0,-40,-100));
      dummy.setRotationPivot(new SimpleVector(0,0,0));


The thing is, that it works this way, but without any other objects in the scene, you won't notice it (except for the fact that the lighting will change).
Anyway, what i basically did was to offset the center of the dummy so that it starts at the point where the camera should be placed in the beginning. You have to make sure, that the RotationPivots of the dummy and the player are describing the same position in WORLDSPACE after doing the transformation (despite the rotation pivot never actually get transformed...). This can be tricky if they differ in OBJECTSPACE...which they don't in this example (they are both placed at the origin). However, maybe drawing this on a piece of paper helps...at least it helped me. There may be other options to get the same effect, but this should work. Then again, i haven't stressed this method very much, so i'm not sure which fancy tricks it will allow for and which not.
I hope it was somehow understandable what i tried to explain...

JonnyDee

Got it!  Looks like a serious misunderstanding on my part of the difference between objectspace and worldspace  :oops:...  Using your example as a starting point i've now got the behaviour I was after, thanks for the help and the library update.