[SOLVED] Basic AI for (automated) flying dronecars

I’m trying to simulate the 8-lane-‘z’-axis-convention described in the video. I have eight flying dronecars setup across the map.

I want to make them go back-and-forth, to-and-from each of the 8 “Lorenz Hubs”. I know there’s a way to make things go back and forth: Using the Tween library | Learn PlayCanvas

But is it possible to make an entity move from one position (x1, y1, z1) to a second (x2, y2, z2) in a yoyo fashion?

Hi @Lureking! It is not entirely clear to me whether the drone should visit all 8 points, or fly up and down between 2 points. Does the drone also have to avoid obstacles and other drones?

Basically, there are 8 dronecars that I want to automate to go one time from point a to point b. It should take about 1 minute for them to travel from point a to point b, so that the user interacting with the environment is able to fly next to the automated flying dronecars. After the automated flying dronecar reaches point b: it should respawn to point a and fly to point b again.

I know I said yo-yo-fashion, but I changed my mind because each story, separated by a certain level of height, represents a specific direction the dronecars are allowed to travel so that they don’t crash into eachother!

Alright, then it’s quite easy to achieve. There are several options for doing this. If it’s not a problem to use a kinematic rigidbody you can use the lookAt function to turn the drone in the right direction and then move the drone forward using translateLocal. You can check the distance to the destination to know if the drone has arrived. (As alternative, you could use a tween or the example project camera following a path, but this wouldn’t be my preference).

I’m worried that the dronecar will fall to the ground if I use kinematic rigidbody. Is there a way to make it so that the dronecar doesn’t fall to the ground, but still travels through the air?

A kinematic rigidbody does nothing until you do it by script. That is the great advantage of a kinematic rigidbody. A dynamic rigidbody, on the other hand, will normally fall down.

  • Static - A physical object that never moves
  • Dynamic - A physical object that will move in response to an applied force
  • Kinematic - A physical object that can only be positioned explicitly via the API
1 Like

Would you be able to walk me through how I would program this? Would I attach the lookAt and translateLocal functions to the Lorenz dronecar or to the Lorenz hub? I tried attaching the scripts found here PlayCanvas 3D HTML5 Game Engine to the Lorenz hub, but I’m getting a TypeError: Cannot Read property 'getPosition' of null.

I see that the Camera following a path | Learn PlayCanvas uses lookAt as well. I also noticed that each of the examples you mentioned requires a lot of coding, which I’m going to need help with! :sweat_smile:

That’s why it has not my preference. As long as it is possible, I always recommend writing your own code because then you know how it works and you can always make the AI ​​better by expanding your code.

I have written the script below for you to get started. This script has not been tested yet and will not work right away. We probably need to adjust a few things to get the right result based on your test result.

var Drone = pc.createScript('drone');

Drone.prototype.initialize = function () {
    this.orgin = this.app.root.findByName('entity name of orgin point');
    this.target = this.app.root.findByName('entity name of target point');
    this.distance = this.entity.getPosition().distance(this.target.getPosition()); 
};

Drone.prototype.update = function (dt) {
    // update the distance to target
    this.distance = this.entity.getPosition().distance(this.target.getPosition()); 

    // if not arrived
    if (this.distance > 1) {
        // look at the target (probably your model need to be rotated 180 degrees)
        this.entity.lookAt(this.target.getPosition());

        // move forward (if there is no rigidbody or the rigidbody is kinematic)
        this.entity.translateLocal(0,0,-0.1);
    }  
    // if arrived
    else {
        // respawn
        this.entity.setPosition(this.orgin.getPosition());
    }
};
1 Like

Thank you! I’m still getting used to the coding world, so not everything is intuitive yet, but with your help I’m definitely learning, so thanks again!

I attached the script to the N Level 1 Dronecar, and checked to see if it did anything, but it does not seem to be moving.

I’m not sure if it’s something with the code, or if I attached the script to the wrong entity.

image

You need to rename this to the actual orgin and target name of the entities in your project.

For example:

this.orgin = this.app.root.findByName('Lorenz Hub and Dronecars 2');
this.target = this.app.root.findByName('Lorenz Hub and Dronecars 3');

image

Please apply this, otherwise it is impossible for me to be able to look around in your project normally.

1 Like


That’s weird, I applied the clipping to be 9999999999, I don’t know why it’s not showing up for you. I added you to the team so that you can write the camera clipping to what you need it to be.

That’s strange, the default value of 1000 is visible to me. I also don’t recommend setting the value excessively high as I don’t know if this could adversely affect the editor’s performance.

By the way, your Camera Clip Near doesn’t seem to be correct anymore. I think you better set this back to 0.1.

Note: I see the background color is also another color on my side. So it could be that this settings are personally if it is not a bug. If you want a blue color as background for your game, you have to change the clear color of the camera component.

I updated the code to
this.origin=this.app.root.findByName('Lorenz Hub and Dronecars 5');
this.target=this.app.root.findByName('Lorenz Hub and Dronecars 2');
but I’m getting a TypeError that getPosition is not a function. Is this supposed to be a this.target.pc.vec3?

Also, the clear color on my end is light blue. Probably a bug similar to the problem we were having with the clipping.

On which line you get the error? I’m on mobile right now so it’s hard to see it by myself, because your game is crashing in the launch mode. Also on line 19 you write getposition instead of getPosition.

1 Like

Nice catch with line 19. I’m not sure what line it is; it just says, this.target.getPosititon is not a function, which is line 7, and line 14.

There is a typo on line 7 and 14. You write getPosititon instead of getPosition.

1 Like

Okay that was the problem, sorry about that. There’s no error now (the error showing up is for the player I’m inserting into the scene), but it made the map really buggy.

Sorry, what is the video about? Is there still a problem related to the topic? Otherwise you can also send me a private message.

Okay, so it’s moving! :smiley: Like you mentioned in PM’s: my map is extremely large because when I CAD’d everything I made it to scale. Does this affect the performance of the GPU? Would scaling down make it more efficient?

For now, I’m going to increase the translateLocal value.

I think it’s better to scale the entity of the model down or scale it down in CAD. If your model is huge it effect everything. For example your forces has to be bigger, your far clip of the camera has to be bigger, your textures has to be bigger, etc.