About the object "LookAt" , need help

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

Previous topic - Next topic

gamenewer

I reference   this topic -   Simulate "camera.lookAt(SimpleVector)" for an Obje ,   set my car rotation matrix, but  it's not ok ,the  car's wheels are up,
what's wrong ?   help me , please ,thanks  ...

[attachment deleted by admin]

EgonOlsen

Hard to tell, because i'm not sure which code exactly you are using. The topic you mention is rather old. Maybe there are better solutions now that didn't exist back then. But to tell that, we need to know what exactly you are doing and what your input values are.

gamenewer

Hello EgonOlsen,  I use code like this:


//get the car current  position
SimpleVector poscur = car.getTransformedCenter();

//get the car lookat waypoint , the  nav point is ahead of car
SimpleVector posLookat = getLookatwaypoint();

posLookat .scalarMul(-1f);
posLookat .add(poscur );
Matrix rotY=posLookat .getRotationMatrix();
car.setRotationMatrix(rotY);


If  I change code  as follow:
.....
poscur .scalarMul(-1f);
poscur .add(posLookat );
Matrix rotY=poscur .getRotationMatrix();
car.setRotationMatrix(rotY);

the effect is  the car 's forward becomes back , please look at the attache picture ,
How can I do ?  thank s   ~




[attachment deleted by admin]

gamenewer

And  I   have another question ,    I  put some object on my road to use as "waypoint",  I load the model  and  get the waypoint position like this:

Object3D [] obj = Loader.loadOBJ(res.getAssets().open("road.obj"),null,100f);
int k =0;
for(int i = 0; i < obj.length; i++){
if(obj.getName().contains("w.0")){
obj.build();
waypoint[k++] = obj.getTransformedCenter();                        
}
else if(obj.getName().contains("Plane"))
{
saidao = obj;
Logger.log("get the road!!!!!!");
}
}

saidao.rotateX((float) (Math.PI) / 1f);
....

actually, the waypoint get by this method is not correct , because  the road  rotateX  late !  So the waypoint  must do  some actions  to location correct position, but  I don't know how to to do this , please help me ,  Thank you very much !!


[attachment deleted by admin]

EgonOlsen

The contract of the getRotationMatrix()-method in SimpleVector is, that it returns a rotation matrix that will transform the vector (0,0,1) to point into the direction of your point. However, it's doesn't say anything about the orientation of the rest of the coordinate system. You might want to have a look at this method instead: http://www.jpct.net/jpct-ae/doc/com/threed/jpct/SimpleVector.html#getRotationMatrix(com.threed.jpct.SimpleVector). In your case, i would say that the up-vector is simply (0,-1,0).

EgonOlsen

I'm not sure if i got the second question...so you define these waypoint by using the center of some objects but then rotate the road and the positions are wrong? If so, try to load and rotate the road before the waypoints and make the waypoint object children of the road (at least temporarily) so that getTransformedCenter() of the objects reflects the transformation of the road too.

gamenewer

mmm  , thanks for help , first add waypoint object to  road as child, get the waypoint  position after rotate the road ,this can get postion ok  :)

gamenewer

thanks for your help , I  change the code like this


//get the car current  position
SimpleVector poscur = car.getTransformedCenter();

//get the car lookat waypoint , the  nav point is ahead of car
SimpleVector posLookat = getLookatwaypoint();

posLookat .scalarMul(-1f);
posLookat .add(poscur );
Matrix rotY=posLookat .getRotationMatrix(new SimpleVector(0,-1,0));
car.setRotationMatrix(rotY);

the car  rotation  and lookat seems  ok , Thank again ~

gamenewer

Thanks EgonOlsen, Now , the car can run along the road by my desinged path , the camera follow behind the car .

But it  seems the car is static , because the  camera position  is set by car position, I pick  code  from other topic is :

      SimpleVector oldCamPos = camera.getPosition();
      oldCamPos.scalarMul(9f);
      SimpleVector carCenter = car.getTransformedCenter();
      SimpleVector zOffset = car.getZAxis();
      zOffset.scalarMul(-250f);
      SimpleVector camPos = new SimpleVector(carCenter);
      camPos.add(camYOffset);
      camPos.add(zOffset);
      camPos.add(oldCamPos);
      camPos.scalarMul(0.1f);
      camera.setPosition(camPos);
      camera.lookAt(carCenter);

      Is there  some way  to let camera  smooth follow the car ?   Camera have little movement up and down or right to left  ,  thanks a lot ~

gamenewer

hello , I have a new question ,  camera follow the car use the code as show before ,  if the car speed slow, the camera close to the car ,but if the car speed is  high  ,  the camera is become very far to the  car ,   this is not my expect ,  I  want  the distance between camera and car is suitable, not so far ,  I change the   zOffset.scalarMul(Xf)  , but it seems no effect , How should I do ?  Thanks very much

gamenewer

If  I   don't use the   camera old position,  the camera looks  not smooth follow the car , this is not my expect ~~

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

EgonOlsen

That camera code looks like it has been taken from the car example of desktop jPCT, hasn't it? If that's the case, it should create a kind of smooth and somehow delayed camera movement that follows the car. I'm not sure what your issue with it actually is...!?

gamenewer

Yes ,  it take from the car example of desktop jPCT,  The issue is  , if the car's speed is slow,  the carmera is  closer to the car , but  if   the speed of car is high , the camera is far   to the car ,   in other words , the distance between camera  and car  is becomes very big , close and far , I hope you  can see  my explain ;)

gamenewer

hello EgonOlsen, I captured some picture for explain my issue,  when car start move , speed from 0 to  maxspeed, the  camera first  at front of car and becomes at behind of car .    The road  path is  closed ( loop) , when car run at the startpoint again  , the  camera will  at  front of car again and at behind the car later. I use the code take from car example desktop, and change some values like this:

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, -30, 0); //here changed
            zOffset.scalarMul(-300f); //here changed

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

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

            SimpleVector delta=camPos.calcSub(oldestCamPos);
            float len=delta.length();

            if (len!=0) {
               world.checkCameraCollisionEllipsoid(delta.normalize(), new SimpleVector(10, 10, 10), len, 3);
            }

            camera.lookAt(cube.getTransformedCenter());



BTW,  I move the car only by  set  it's position ,  Is there something wrong ?    Thanks very much for your help  :)

[attachment deleted by admin]

EgonOlsen

That's means that the camera's position actually doesn't change?