Some problems about setting the camera

I am learning to make a car game by modifing the jme3test.bullet.TestFancyCar.

The first problem I encounted is How to set the camera over behind the car?

I have try below:

cam.setLocation(carNode.getWorldTranslation().add(0, 4, 10));

Quaternion q=carNode.getWorldRotation().clone();

cam.setRotation(q);

But the camera doesn’t look forward ,but backward.Why?

Any help is appreciate O(∩_∩)O

The fancy car is going backwards, as stated in the code. Check this document about how to use the Quaternions, Vectors etc. And please use the troubleshooting groups next time.

Hello



I asked myself the same question the other day and came up with a solution

I dont know if its the best way, but it works. I just get some small vehicle shacking when turning, which makes me mad :slight_smile:



BTW always be careful to clone() translations and rotations if you dont want to modify the translations/rotations per se.



[java]

public static final Quaternion PITCH011_25 = new Quaternion().fromAngleAxis(FastMath.PI/16, new Vector3f(1,0,0));



@Override

public void simpleUpdate(float tpf) {

//TODO: add update code



//have seen this in some codes, i think its good to have it

rootNode.updateLogicalState(tpf);

rootNode.updateGeometricState();



//get the translation and rotation of the vehicle

Vector3f vLoc=vehicle.getWorldTranslation();

Quaternion qRot=vehicle.getWorldRotation().clone();



//pitch the rotation a bit down — so you look towards the bottom, used for setting camera rotation later

qRot.normalize();

qRot.multLocal(PITCH011_25);



//get the forward direction of the vehicle

Vector3f vRot= vehicle.getForwardVector(null).mult(5f);





//set the pitched down rotation of the car - look a bit to the bottom

cam.setRotation(qRot);



//calculate the position of the camera (above car), using the vehicles position and the vehicles forward direction

Vector3f vPos=new Vector3f(vLoc.x-vRot.x,vLoc.y+3.5f,vLoc.z-vRot.z);

//set the position of camera

cam.setLocation(vPos);

}

[/java]

nego said:
BTW always be careful to clone() translations and rotations if you dont want to modify the translations/rotations per se.

When you don't use the xxxLocal() methods you also get a new vector, so simply doing this the first time is enough. Also note that in your code qRot.normalize(); doesnt do anything, you'd have to call normalizeLocal();
1 Like

Could you do this with a ChaseCam? Just wondering… Perhaps modifying the ChaseCam so that follows the spatial and the rotation of the spatial? I would do this if I knew a bit more about 3D programming… But until then, I can only suggest this :P.



I’m just saying I think it would be a good idea (to make the ChaseCam have the ability to follow the rotation of the spatial)…





-NomNom

Thank you so much helping me . I will try that!

And …I was sorry for posting this topic to dev group, I didn’t notice the note.

nego said:
Hello

I asked myself the same question the other day and came up with a solution
I dont know if its the best way, but it works. I just get some small vehicle shacking when turning, which makes me mad :)

BTW always be careful to clone() translations and rotations if you dont want to modify the translations/rotations per se.

[java]
public static final Quaternion PITCH011_25 = new Quaternion().fromAngleAxis(FastMath.PI/16, new Vector3f(1,0,0));

@Override
public void simpleUpdate(float tpf) {
//TODO: add update code

//have seen this in some codes, i think its good to have it
rootNode.updateLogicalState(tpf);
rootNode.updateGeometricState();

//get the translation and rotation of the vehicle
Vector3f vLoc=vehicle.getWorldTranslation();
Quaternion qRot=vehicle.getWorldRotation().clone();

//pitch the rotation a bit down --- so you look towards the bottom, used for setting camera rotation later
qRot.normalize();
qRot.multLocal(PITCH011_25);

//get the forward direction of the vehicle
Vector3f vRot= vehicle.getForwardVector(null).mult(5f);


//set the pitched down rotation of the car - look a bit to the bottom
cam.setRotation(qRot);

//calculate the position of the camera (above car), using the vehicles position and the vehicles forward direction
Vector3f vPos=new Vector3f(vLoc.x-vRot.x,vLoc.y+3.5f,vLoc.z-vRot.z);
//set the position of camera
cam.setLocation(vPos);
}
[/java]