[SOLVED] Thrust controls of drone car

https://playcanvas.com/editor/scene/1167506

This drone car simulator is a work in progress to demonstrate controls using WASD and IJKL to propel the drone car in different directions.

How would I go about coding the thrust upwards, on button press ‘pc.KEY.I’, of the drone car with gravity acting on it?

To get into 3rd person press ‘x’

I want to develop animations for the dynamic rocket cages to tilt accordingly; for pitch forward ‘pc.KEY_W’ and pitch backward ‘pc.KEY_S’, along with the actual movement of the drone car in the respective direction. As well as, ‘pc.KEY_J’ for Yaw Left, ‘pc.KEY_L’ for Yaw Right, ‘pc.KEY_A’ for roll left, and ‘pc.KEY_D’ roll right.

I’m guessing the pseudo code for something like this would be

  1. if (this.app.keyboard.wasReleased(pc.KEY_I)){
    asset.model.LorenzDroneCar(up);
    }

or something, hopefully you get the point! :stuck_out_tongue:

Well, applying an upward impulse to a body is as simple as this:

// --- given the entity this script is applied to has a dynamic rigid body
// --- applied on press, put in in your script update method
if (this.app.keyboard.isPressed(pc.KEY_I)){
   this.entity.rigidbody.applyImpulse(0, 10, 0);
}

You can check the API here, it allows you also to set an offset based on the entity center on the point where the impulse is applied. That may be useful for your drone car given it has offset engines:

https://developer.playcanvas.com/en/api/pc.RigidBodyComponent.html#applyImpulse

2 Likes

Thank you for the response!

Am I doing this right?

droneCarMovement

I also added the script to the Lorenz Drone Car entity:

The drone car does not seem to be reacting to the button press. Do I have to configure something else, or perhaps write more code?

Everything seems correct, I think you need to play with the impulse value. The 10 value was as an example, try adding an attribute to easily play with it and increase it.

1 Like

impulseAttribute100

So, I set the impulse to 100, and 1000, and I tried running the sim again, but no response. I tried adding an attribute for the impulse, but I have a feeling it’s not set up properly.

I’m new to coding, so I need a detailed explanation of everything if possible!

One error that I see know in your project is that you define the .update() in the DroneCarMovement script twice. Javascript is quite flexible to accept that with no error but it can’t hold two definitions for that function.

Meaning only your second update method is executing which basically does nothing since it’s empty.

Get rid of it and try again. Also you were missing Ammo hence the following error, press that button to Import Ammo and enable physics:

image

Next an additional issue:

  • Your Drone Car body is missing a collision component. A rigid body can’t work without a collision component.

I’ve made some changes here myself, forking your project, seems to be working as a starting point for you:

https://playcanvas.com/project/815504/overview/lorenz-simulator

1 Like

Thank you it works! But it’s so buggy :sob: I’m looking into moving the drone around using the impulse function and other buttons.

It seems to work for the ‘L’ button, but everything else breaks.

Hi @Lureking,

This is because you keep redefining your update function. All of the keyboard checks you’re doing should go under one copy of DroneCarMovement.prototype.update(). You can den separate them with else if statements.

1 Like
DroneCarMovement.prototype.update = function (){
if (this.app.keyboard.isPressed(pc.KEY_I)){
    this.entity.rigidbody.applyImpulse(0, this.impulse, 0);
}


else if (this.app.keyboard.isPressed(pc.KEY_K)){
    this.entity.rigidbody.applyImpulse(0, -this.impulse, 0);
}


else if (this.app.keyboard.isPressed(pc.KEY_J)){
    this.entity.rigidbody.applyImpulse(this.impulse, 0, 0);
}


else (this.app.keyboard.isPressed(pc.KEY_L));{
    this.entity.rigidbody.applyImpulse(-this.impulse, 0, 0);
}
};

This is what I have so far, but the drone car still only reacts to KeyPressL.

Hi @Lureking! That’s probably because you forgot to save your changes. There is also a wrong sign on line 31.

2 Likes

Just to add to errors on line 31. That line should also be an else if, not just else.

2 Likes

Thanks everyone so far for the help! I’m almost done with the thrust controls; I just need to implement yaw left and yaw right using the applyTorque.

I wrote the following code using Forces and Impulses | Learn PlayCanvas as reference:

But, the drone car disappears when I press A and D. Am I missing something?

What value is this.torque?

I just added the following code at the top, sorry I forgot this!

DroneCarMovement.attributes.add('torque', {
type: 'number',
default: 100,
description: 'Adjusts the torque'
});

It’s no longer disappearing, but the drone car is still not rotating.

Hi @Lureking,

After adding those lines to the top, did you then click ‘parse’ on your script component in the editor?


Yes.

I’m not sure, but I think there has to be a value input for your attributes. I don’t see that when you parse the script.

I inputted a value of 100 and assigned it to the keyPress function. Is that what you mean? If not, can you detail what you think it is?

What I meant has now been fixed. It looks like that was a bug because the buttons where also not visible.

Before (in your video):

After (in your project):
image

Your script is correct and line 45 and 48 are also executed when pressing the A or D key, so there must be something wrong with applyTorque.

As you using applyImpulse for movement, you are more likely to want to use applyTorqueImpulse instead of applyTorque to get that immediate rotation force.

https://developer.playcanvas.com/en/api/pc.RigidBodyComponent.html#applyTorqueImpulse

applyTorque is similar applyForce where it applies acceleration and therefore takes time to reach the (rotational) velocity.

2 Likes