# www.jpct.net

## jPCT-AE - a 3d engine for Android => Support => Topic started by: gamenewer on June 09, 2014, 05:17:06 pm

Title: About the object "LookAt" , need help
Post by: gamenewer on June 09, 2014, 05:17:06 pm
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  ...

Title: Re: About the object "LookAt" , need help
Post by: EgonOlsen on June 09, 2014, 09:57:29 pm
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.
Title: Re: About the object "LookAt" , need help
Post by: gamenewer on June 10, 2014, 02:20:48 am
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);
Matrix rotY=posLookat .getRotationMatrix();
car.setRotationMatrix(rotY);

If  I change code  as follow:
.....
poscur .scalarMul(-1f);
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   ~

Title: Re: About the object "LookAt" , need help
Post by: gamenewer on June 10, 2014, 04:12:31 pm
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:

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;
}
}

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 !!

Title: Re: About the object "LookAt" , need help
Post by: EgonOlsen on June 10, 2014, 09:17:51 pm
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) (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).
Title: Re: About the object "LookAt" , need help
Post by: EgonOlsen on June 10, 2014, 09:20:42 pm
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.
Title: Re: About the object "LookAt" , need help
Post by: gamenewer on June 11, 2014, 03:43:15 am
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  :)
Title: Re: About the object "LookAt" , need help
Post by: gamenewer on June 11, 2014, 06:48:13 am
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);
Matrix rotY=posLookat .getRotationMatrix(new SimpleVector(0,-1,0));
car.setRotationMatrix(rotY);

the car  rotation  and lookat seems  ok , Thank again ~
Title: Re: About the object "LookAt" , need help
Post by: gamenewer on June 11, 2014, 07:33:37 am
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.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 ~
Title: Re: About the object "LookAt" , need help
Post by: gamenewer on June 12, 2014, 03:07:52 pm
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
Title: Re: About the object "LookAt" , need help
Post by: gamenewer on June 12, 2014, 04:58:25 pm
If  I   don't use the   camera old position,  the camera looks  not smooth follow the car , this is not my expect ~~

/*
camPos.scalarMul(0.1f);
*/
Title: Re: About the object "LookAt" , need help
Post by: EgonOlsen on June 12, 2014, 08:44:07 pm
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...!?
Title: Re: About the object "LookAt" , need help
Post by: gamenewer on June 13, 2014, 01:51:04 am
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 ;)
Title: Re: About the object "LookAt" , need help
Post by: gamenewer on June 13, 2014, 10:56:11 am
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.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  :)

Title: Re: About the object "LookAt" , need help
Post by: EgonOlsen on June 13, 2014, 12:50:18 pm
That's means that the camera's position actually doesn't change?
Title: Re: About the object "LookAt" , need help
Post by: gamenewer on June 13, 2014, 12:53:24 pm
Maybe, but when camera behind the car , it follow the car indeed
Title: Re: About the object "LookAt" , need help
Post by: gamenewer on June 13, 2014, 02:15:46 pm
I  dont know why this happen ? the camera should  always behind the car  :(
Title: Re: About the object "LookAt" , need help
Post by: EgonOlsen 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.
Title: Re: About the object "LookAt" , need help
Post by: gamenewer 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 poscurrent = cube.getTransformedCenter();
/*
the car is invert , if I use   posAhead.scalarMul(-1f); the car looks ok,
I think  if here is the reason for issue
*/
cube.setRotationMatrix(rotY);

So   I    remove this line :

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.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.

Title: Re: About the object "LookAt" , need help
Post by: EgonOlsen 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.
Title: Re: About the object "LookAt" , need help
Post by: gamenewer 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 .
Title: Re: About the object "LookAt" , need help
Post by: EgonOlsen 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.
Title: Re: About the object "LookAt" , need help
Post by: gamenewer 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)
Title: Re: About the object "LookAt" , need help
Post by: EgonOlsen 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.
Title: Re: About the object "LookAt" , need help
Post by: gamenewer 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 :)
Title: Re: About the object "LookAt" , need help
Post by: EgonOlsen 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.
Title: Re: About the object "LookAt" , need help
Post by: gamenewer 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