Troubleshoot AI Vehicle Movement

Hi,

I am using the update method from AutonomousVehicleControl for a vehicle movement using the following code:

[java]

   private Vector3f vector1 = new Vector3f();
   private Vector3f vector2 = new Vector3f();
   private Vector3f vector3 = new Vector3f();//valideLocations[3];
   private Vector3f vector4 = new Vector3f();//valideLocations[4];
   static final Quaternion ROTATE_RIGHT = new Quaternion().fromAngleAxis(FastMath.HALF_PI, Vector3f.UNIT_Y);
   private Plane plane = new Plane();

   private float speed = -800f;

   Spatial map;

        public void CarMoveAt(Vector3f targetLocation) {
                        vehicle.getPhysicsLocation(vector1);
                        vector2.set(targetLocation);
                        vector2.subtractLocal(vector1);
                        float distance = vector2.length();
                        float checkRadius = 4;
                        if (distance <= checkRadius) {
                            //moving = false;
                            vehicle.accelerate(0);
                            vehicle.brake(10);
                        } else {
                            vector2.set(targetLocation);
                            vector2.normalizeLocal();
                            vehicle.getForwardVector(vector3);
                            vector4.set(vector3);
                            ROTATE_RIGHT.multLocal(vector4);
                            plane.setOriginNormal(map.getWorldTranslation(), vector4);
    
                            float dot = 1 - vector3.dot(vector2);
                            float angle = vector3.angleBetween(vector2);
    
                            float anglemult = FastMath.PI / 4.0f;
                            float speedmult = 0.3f;//0.3f;
    
                            if (angle > FastMath.QUARTER_PI) {
                                angle = FastMath.QUARTER_PI;
                            }
                            //left or right
                            if (plane.whichSide(targetLocation) == Plane.Side.Negative) {
                                anglemult *= -1;
                            }
                            //backwards
                            if (dot > 1) {
                                speedmult *= -1;
                                anglemult *= -1;
                            }
                            vehicle.steer(angle * anglemult);
                            vehicle.accelerate(-speed * speedmult);
                            vehicle.brake(0);
                        }
        }

[/java]

I have seen just one post on this problem at Move vehicle towards a point, tried to run the solution, but with no succes.

The problem is the same, at some point, after some steering to the direction idk why the method to decide if the target is right or left side to the plane normal is changing values alternatively, causing the car to steer in zig-zag and lose control of direction.

Please surround your code with a triple back ticks to make it readable.

Please help? I have tried literally everything but with no useā€¦

Do you have a single test class which somebody can copy past an run it? Would increase the chances that you get help. But currently we see only a snippet and I personally can not make much sense what you try and where you call this. Per frame maybe? Maybe then you also need tpf as a parameter in your calculation?

but a single test class would help, even for your self.

I am running it on an extended SimpleApplication class, I will attach a demo project with everything that need to be set for testing. Sorry, i thought the problem was so common that you guys know already the answear xD.

project download link:
https://we.tl/ZnZ0poNsUN

I think you misunderstood me. I meant boil your problem down to a one single test class like this https://jmonkeyengine.github.io/wiki/jme3/beginner/hello_picking.html. It is some work, but it is mostly worth the effort. I started many many times a thread here and compiled my problem in a single test class, which others just can copy past and run it to see what is my problem, I think I only did post 2 of them in the end as I found often the problem my self by make this single class test.

I just had a quick glance at your project. You call the CareMoveAt in the update loop. But you do not use tpf to make smooth turns and moves, so no wonder it makes a zig-zag course. I mean it gets called at least 50-60 times per second. tpf gives the time fragment of a single frame (tpf == time per frame). So you somehow have to smooth/slowdown you movements and turns by multiplying it with tpf.

I hope I could help.

Btw. not sure, but did you the tutorials? The beginners section in the wiki? If not I highly suggest you to do that, then you see how you have to use that tpf (there is at least one sample with something turning around an axis).