Author Topic: About the object "LookAt" , need help  (Read 3134 times)

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: About the object "LookAt" , need help
« Reply #15 on: June 13, 2014, 12:53:24 pm »
Maybe, but when camera behind the car , it follow the car indeed

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: About the object "LookAt" , need help
« Reply #16 on: June 13, 2014, 02:15:46 pm »
I  dont know why this happen ? the camera should  always behind the car  :(

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: About the object "LookAt" , need help
« Reply #17 on: June 13, 2014, 09:12:58 pm »
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.

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: About the object "LookAt" , need help
« Reply #18 on: June 14, 2014, 04:48:46 am »
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]
« Last Edit: June 14, 2014, 09:41:27 am by gamenewer »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: About the object "LookAt" , need help
« Reply #19 on: June 15, 2014, 09:22:48 pm »
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.

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: About the object "LookAt" , need help
« Reply #20 on: June 16, 2014, 05:36:12 am »
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 .
« Last Edit: June 16, 2014, 05:46:58 am by gamenewer »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: About the object "LookAt" , need help
« Reply #21 on: June 16, 2014, 05:38:04 pm »
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.

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: About the object "LookAt" , need help
« Reply #22 on: June 17, 2014, 08:48:05 am »
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)
« Last Edit: June 17, 2014, 12:14:40 pm by gamenewer »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: About the object "LookAt" , need help
« Reply #23 on: June 18, 2014, 07:12:07 pm »
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.

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: About the object "LookAt" , need help
« Reply #24 on: June 19, 2014, 12:43:03 pm »
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 :)
« Last Edit: June 19, 2014, 02:54:08 pm by gamenewer »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: About the object "LookAt" , need help
« Reply #25 on: June 19, 2014, 06:47:03 pm »
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.

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: About the object "LookAt" , need help
« Reply #26 on: June 20, 2014, 02:09:54 am »
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
« Last Edit: June 21, 2014, 05:04:29 am by gamenewer »