Vehicle Physics

I am trying to convert the old vehicle.js script to the new script writing style (YES, thousands of ‘this…:wink: )
I am stuck with
var ammoVec = new Ammo.btVector3();
&
var motionState = new Ammo.btDefaultMotionState(transform);

How do I write this in the new scripting style to get access to the ammo physics stuff?

Or better - has anyone currently translated or build up the car physics for playcanvas and can share the script?

Both of those should work as-is. Have you enabled physics in the project settings?

1 Like

Ha, cool, forgot there is a checkbox :slight_smile:

Next thing where it hangs is:
this.initialRot = this.entity.getRotation().clone();
but I don’t understand what the clone is for and why it doesn’t find the entitiy
[vehicle.js?id=11160347:49]: Cannot read property ‘getRotation’ of undefined


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

Looks like you are carrying over a lot of of the Script 1.0 idioms across which are not needed.

For that specific error, you are making this.entity null in the first line of this function:

Vehicle.prototype.newVehicle = function (entity) {
    this.entity = entity;

this.entity is already assigned in Scripts 2.0 so you don’t need to manually assign like you had to in Scripts 1.0.

So change it to:

Vehicle.prototype.newVehicle = function () {

Same with app. this.app is auto assigned in Scripts 2.0.

Did you solve it?:roll_eyes:

Did you ever figure this out? I’m trying to do the exact same thing!

@evandarcy if you look here he clearly has figured it out. he left a project link with the code

I stopped translating it - just don’t have time for fun stuff :thinking:

I tried using that code and attempted to correct the errors but I got stuck and couldn’t progress. Something about the wheels not being defined. Maybe you could help?

yes i will take a look at it and see if i cant fix it up for you.

@yaustar you said the

should work as is right?
well its popping those errors it shouldnt be the script seems fine i cant see anything wrong with what pepone3d did with it why is this calling errors?
LINK TO PROJECT: https://playcanvas.com/project/614809/overview/vehicle-script-new
also i noticed this came from a playhack: https://playcanvas.com/editor/scene/353606
and as far as i could tell it was translated correctly.
EDIT: I had forgot to enable physics but now it is saying these are errors:

  this.trans = new Ammo.btTransform();
.....
    this.initialRot = this.entity.getRotation().clone();
    this.initialPos = this.entity.getPosition().clone();

Please refer back to the post here and the reply that came after: Vehicle Physics

1 Like

Ok i kinda fixed the script up a little bit but its calling an error here: var numWheels = this.wheelsConfig.length;
inside the initialize function and its correct isnt it? it should work now right?

If you look where this.wheelsConfig is defined in the script, you see the function that it’s in (create) is never called so this.wheelsConfig is undefined.

Ok thx i will look through it again and fix it up some more if i run in more issues ill let you know

So should i redifine this.wheelConfig or how would i call it from the other function (create) as like this.wheelConfig(); or is there another way to call it?
EDIT: Also @yaustar it calls this undefined as well

        if (DEBUG_DRAW) {
            this.debug.wheels[i].setPosition(p.x(), p.y(), p.z());
            this.debug.wheels[i].setRotation(this.quat);
        }
    }

Which is in the update function. it calls now error anywhere else for the debug_draw but here

I would use the original script (wherever it is) as the base to decide what and when functions should be called. Right now, you are working off a derived script that may be missing logic from the original.

your right here is the original script: https://playcanvas.com/editor/scene/353606
i think that if i just restart and translate the script myself i might have better luck if you come up with any suggestions or i run into another problem ill create a newer topic

@yaustar i have switched the script myself its kinda messy right now i have somewhere to be here in ten minutes so i dont have time to look it through right now but you can look it through as the editor is here and the code i transfered is here
If you find something that is incorrect please let me know i will be back soon