[SOLVED] Rotating Camera around Point

Hello, I want the Camera to rotate in a circle around a center and to stay focused on that center.

It works fine when use it like this (i’m just posting the relevant stuff):

camDir = Vector3f(x,y,z);             'fixed point to look at'
camPos = myRotateFunction(cam.getLocation(), camDir, offset,); 'rotating the pos ot the cam'
cam.setLocation(camPos);              ' put the cam right there'
cam.lookAt(camDir, Vector3f.UNIT_Y);  'look at the defined point'

But it is not working when i use it like this

camDir = cam.getDirection();         ' save where the cam looks now'                          
camPos = myRotateFunction(cam.getLocation(),  camDir, offset); 'rotating the pos ot the cam'
cam.setLocation(camPos); ' put the cam right there'
cam.lookAt(camDir, Vector3f.UNIT_Y); 'look at center i saved before (but it doesnt)

The problem ist that the cam always looks at the point (0,0,0).
When I use “cam.lookAtDirection” instead of “cam.lookAt” then the cam always looks at the same direction and not at the desired point. What did I get wrong ??

why not just use ChaseCamera?

https://javadoc.jmonkeyengine.org/com/jme3/input/ChaseCamera.html

So the point is: how do i get the point the cam looks at and not the direction?

Because i’m not writing a game but try to write a kind of CAD-Software.

how do i get the point the cam looks at and not the direction?

this question is not specified. When you look at Front of you, you look at many points in line in from of you.

If you want some Geometry point that is in front of you then you need use Ray.

for example:

        Vector3f origin = cam.getLocation();
        Vector3f direction = cam.getDirection();
        Ray ray = new Ray(origin, direction.normalize());
        CollisionResults results = new CollisionResults();
        rootNode.collideWith(ray, results);
        CollisionResult closestCollision = results.getClosestCollision();
        if (closestCollision != null) {
            return closestCollision.getContactPoint();
        }

Also

cam.lookAt(camDir, Vector3f.UNIT_Y);

here camDir can be just world location like Vector3f(100,100,100) or spatial.getWorldTranslation() whatever. it dont need to be direction here. where Vector3f.UNIT_Y mean that up point will be on Y.

also next:

you say: "camDir = cam.getDirection(); ’ save where the cam looks now’ "

if you want save, use

camDir = cam.getDirection().clone() - and remember, its direction vector, NOT LOCATION.
location: camDir = cam.getLocation().clone()

Why clone? because its basic Java, when you pass object, it remember its reference, so if you dont clone, it still see same Object that can have changed data somewhere else.

im not sure if getDirection make clone inside, i will check.

edit:
for getDirection im not sure if need .clone() but for cam.getLocation() you need clone to have new object.

so since you use:

camPos = myRotateFunction(cam.getLocation(), camDir, offset,); ' rotating the pos ot the cam'

i belive your “myRotateFunction” can modify exising Location object instead of clone, that could be why its wrong.

Because i’m not writing a game but try to write a kind of CAD-Software.

Yes then see blow part of my post, this should help i think.

1 Like

I went ahead and made this an appstate and published it on the store. It gets asked about quite frequently, so here it is for everybody.

https://store.jmonkeyengine.org/6ed7af21-d4d7-48e3-9614-e0341180eb7c

3 Likes

That’s awesome :slight_smile: start to love this store…

3 Likes

Thank you very much! Now I understand what went wrong.

2 Likes

Why not add optional smooth transition from a focal point to the next?