Change velocity to new direction

Hi,

I am currently trying to get a car moving. Sofar I add a Force to let it speed up and brake (add force backwards).

The car can rotate, too, but now I've got the problem that the car isn't moving towards the direction it faces, but to the direction the last force was added.

If I speed up (add force) and then want the car to roll out and then steer to left or right, the car won't move to the new direction, but to the same direction it drove before I steered the car.



Is there a way to fix that?



I think there can be done something with the setLinearVelocity-Method right? However I don't know how to work with the given vector, because it always has values != 0, even though my car is standing still, so I don't how I should set the new vector.



Hope you can help me out. Thanks.

You'd need to provide more details on how you made your car.



Did you have a look at TestVehicle? It shows the very basics of making cars. (cvs version was broken, so update from cvs if you don't use the beta)

I have a car model loaded and i am adding Force to the whole car.

I tried to do it with adding force to the wheels, but that did not work (see here: http://www.jmonkeyengine.com/jmeforum/index.php?topic=5434.0 ).



So basically I have a Block, which contains the car, and I am adding Force to it, so it can move forward.

When ever I speed up, the force will be added to the direction the car is faced (by getting the LocalRotation of the car), but when I steer either left or right, the velocity does not rotate with the car, but stays the same.



Now I need to know how I could restrict the movement of the car, so it only moves to the direction it is facing.



Hope the problem is clear now.

Ok, if you don't have wheels physically, you can use surface motion like in Lesson 8.

Ok thanks.  Erm… do u know how I get the right direction (as a Vector3f) from my car? The getLocalRotation returns an Quaternion, but don't know how I convert that into an Vector3f.

For surface motion you don't need it. To get a vector pointing into the direction of a spatial multiply the vector that points to the front of your unrotated model (e.g. (1, 0, 0)) with the world rotation. (see jME Wiki)

Thanks, however I don't get it to work yet.

I dont know which wiki page you ment, but I have now the following for move forward:

    private class ForwardAction extends InputAction {
        public void performAction( InputActionEvent evt ) {
            direction = node.getWorldRotation().mult(new Vector3f(0,0,1));
            node.getMaterial().setSurfaceMotion( direction );
        }



When my car is loaded and I press "w" it will move forward. When I steer to either right or left it will move to that direction. So that part works fine.
But if I then press "w" again (for now it has the constant speed, but later on i will change it, where you have to keep "w" pressed to move), the car won't move forward, but to some other direction.

The same happens when my car is loaded and I first rotate it a bit and then press "w". Then it does not move forward either, but to some other direction.

So I guess I "calculate" my direction wrong.

I kind of feel that I don't even get the easiest part right, but I hope you guys can help my out there.



Here is how my car steers (in case it matters)

    private class LeftAction extends InputAction {
        public void performAction( InputActionEvent evt ) {
            Matrix3f incr = new Matrix3f();
            Matrix3f tempMa = new Matrix3f();
            Matrix3f tempMb = new Matrix3f();
            float speed = 2;
            incr.loadIdentity();
            incr.fromAngleAxis(speed * evt.getTime(),new Vector3f(0,1,0));
            node.getLocalRotation().fromRotationMatrix(
                    incr.mult(node.getLocalRotation().toRotationMatrix(tempMa),
                            tempMb));
            node.getLocalRotation().normalize();
        }

Note:

irrisor said:

For surface motion you don't need it.

Surface motion is in local coordinate system! This means you must not multiply your direction with world rotation.

Oh… I somehow thought the  2nd sentence was the answer, and did not pay much attention to the first one.



My mistake…



Big Thanks for helping my out here. (Works now)