Vehicle Physics


#1

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?


Updated Vehicle.js script?
#2

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


#3

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



#4

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


#5

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.


#6

Did you solve it?:roll_eyes:


#7

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


#8

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


#9

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


#10

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?


#11

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


#12

@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();

#13

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


#14

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?


#15

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.


#16

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


#17

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


#18

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.


#19

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


#20

@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