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]
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.
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]
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]
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).
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.
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 :)
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 ~
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 ~
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
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);
*/
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...!?
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 ;)
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]
That's means that the camera's position actually doesn't change?
Maybe, but when camera behind the car , it follow the car indeed
I dont know why this happen ? the camera should always behind the car :(
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.
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]
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.
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 .
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.
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)
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.
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 :)
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.
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