About the object "LookAt" , need help

Started by gamenewer, June 09, 2014, 05:17:06 PM

Previous topic - Next topic

gamenewer

Maybe, but when camera behind the car , it follow the car indeed

gamenewer

I  dont know why this happen ? the camera should  always behind the car  :(

EgonOlsen

Yes, it should. All that the orginal code does is to interpolate the current position behind the car by taking the two former positions into account. I don't see, how it should flip in the way it does for you. It translates the camera back along the negative z-axis of the car and that will always point away from the back of the car.
Then again, i'm not really sure how your exact code looks like. Your first code snippet misses some parts that the latter does but that one again missing the actual camera positioning, does a collision detection without actually using the result for anything and doesn't use the car but a cube object. I'm a little bit confused what you are actually using. Maybe you can post the real snippet again with all the camera setting and collision stuff includes...just to be sure that we are talking about the same thing.

gamenewer

#18
Thanks for your patient reply,  Maybe   I    find the reason for this issue,    because the car  looks invert ,  I  change the direction(Z) vector as following :

                                SimpleVector posAhead = Interp(lookAheadAmount+percent);
            SimpleVector poscurrent = cube.getTransformedCenter();
            posAhead.sub(poscurrent);
            /*
            the car is invert , if I use   posAhead.scalarMul(-1f); the car looks ok,
            I think  if here is the reason for issue

            */
            posAhead.scalarMul(-1f);
            Matrix rotY=posAhead.getRotationMatrix( new SimpleVector(0,-1,0));
            cube.setRotationMatrix(rotY);


So   I    remove this line : 
                               
                                posAhead.scalarMul(-1f);

In this case , the camera is behind the car , of course ,the car looks invert.  but  a new issue appears,  the distance between car and camera becomes  far and far, and the car out of view at last ,   here is camera postion set code:


                      Camera camera = world.getCamera();
            SimpleVector oldCamPos=camera.getPosition();
            SimpleVector oldestCamPos=new SimpleVector(oldCamPos);
            oldCamPos.scalarMul(9f);

            SimpleVector carCenter=cube.getTransformedCenter();
            SimpleVector camPos=new SimpleVector(carCenter);
            SimpleVector zOffset=cube.getZAxis();
            SimpleVector yOffset=new SimpleVector(0, -10, 0); //-100
            zOffset.scalarMul(-20); //-250

            camPos.add(zOffset);
            camPos.add(yOffset);

            camPos.add(oldCamPos);
           
            camPos.scalarMul(0.1f);

            SimpleVector delta=camPos.calcSub(carCenter);
            //distance between camera and car
            float len=delta.length();

            Logger.log("-->distance between camera and  car --->" +  len ); // the len becomes  biger and biger and  the car out of  view at last
           
                      camera.setPosition(camPos);
            camera.lookAt(cube.getTransformedCenter());


I try to change the  ZOffset  , but  no effect  , distance between camera and car should constant  and  a little  change. Could you help me? Thanks a lot.




I captured  some pictures help me to explain clearn.

[attachment deleted by admin]

EgonOlsen

I'm still confused by the naming...cube is the actual car? If that's the case, i'm not sure, how much of this code is from the car example and how much has been modified. For example, i can't spot any use of the oldestCamPos vector here.
Anyway...what the car example does is to calculate the new position and mix it with the former one to smooth it out.
You might want to try to limit yourself to the first step for now. Just set the camera at the car's position, get the z axis and move it back based in this. Try to get that working first ans then try to add the smoothing back into it.

gamenewer

#20
Sorry  , infact  the cube is the car (I use the example hellow jpact-ae and not change the name ;))  . 

I set the carmera position by car   offset Z and offsetY  ,  the camera works  no problem, It can follow the car always .    but the game looks very bad , the car looks static always  even though  the road is  moving.  So  must let camera does smooth following.

The distance between camera and the car becomes biger and biger   , because set camera position by use the new position between camera old position  and the wanted position,so  when times go and go the camera position becomes far and far from the car... 

I reomve  this line world.checkCameraCollisionEllipsoid(delta.normalize(), new SimpleVector(10, 10, 10), len, 3);   because I'm l not sure  if this may cause issue, so I use camera.setPosition(camPos); instead. but it's the same  effect.

BTW: I only take moveCamera function from the car example .

EgonOlsen

Maybe it helps to tweak the weights used for the interpolation. Your current code takes 9 times the old position plus one time the new one divided by 10 (i.e. multiplied by 0.1). You might want to change these values to give the new position a higher weigth.

gamenewer

#22
Thanks for reply , I try many times , It's hard to tweak   following code.

oldCamPos.scalarMul(9f);
camPos.scalarMul(0.1f);


I read another topic , but I'm  still  not understand how to limit the distance between camera and car to a constant  by:
oldCamPos.scalarMul(9f);camPos.scalarMul(0.1f);

In other words , I want the camera behind the car   about  60(+-)m  far , How can I set the  parameter scalarMul(9f) ?

Note, the car speed is vary in my game.

Could you give me some advise? thank you .

(I try  increase and deincrease the  scalarMul  and divid (mul+1) , but the distance still can't contral  to a suitabe,  car still far and far away camera  and out of view at last)

EgonOlsen

Well, that code was never meant to keep a constant distance to the object. The idea was to provide a smooth camera movement  but not at a constant distance. If you want that, you might have to create your own method or maybe take the final position calculated by this method, calculate the direction vector from the car to that position and multiply it with the constant distance. Then add it to the car's position and take the result as the new position. I'm not sure of that would be any better than just taking the z axis as mentioned in a former Post.

gamenewer

#24
Ok,thanks for your help , I will do this by another way later.

Now, I want   carmera to do a smooth  rotation with car , I write the code like this,

Matrix  carmx = car.getRotationMatrix();
Matrix  cameramx= camera.getBack();
cameramx.interpolate(cameramx,carmx ,0.25f);
camera.setBack(cameramx);

Is it ok ? thanks a lot :)

EgonOlsen

It's worth a try...interpolation of matrices works fine as long as the matrices aren't too different, which they shouldn't be in this case.

gamenewer

#26
Oh,It's my  mistake,    I  should  get the car Y rotation and let carmera do a smooth Y rotation by car.   I fixed distance between  car and carmera,  and  plan camera smooth rotation by car.
so . I search the forum  and write the code like this:

float carangle = car.getTransformedCenter().calcAngle(car.getXAxis());       
float cameraangle = camera.getPosition().calcAngle(camera.getXAxis());
      
cameraangle = slerp(cameraangle,carangle,0.01f);               
camera.rotateCameraAxis(camera.getYAxis(),cameraangle);

but it works   wrong . I guess the camera lookat do a rotation action , I want this rotation action smooth,  damping effect . How can I do this? thank you