Jump to content

Phaser P2 TopDownVehicle implementation


NotABlueWhale
 Share

Recommended Posts

I've been trying to set up a top-down car game for days now. I just couldn't create car-like behaviour by myself. Then I found the TopDownVehicle class in p2's change log for version 0.7.0. The class does not seem to be implemented well in Phaser however. All over the internet I haven't found a single example case or even a mention of the class being used within Phaser.

 

Looking at the example code found here, I came up with the following first attempt:

var car = game.add.sprite(x, y, sprite, 0);        // Create the vehiclevar vehicle = new TopDownVehicle(car.body);// Add one front wheel and one back wheel - we don't actually need four :)var frontWheel = vehicle.addWheel({    localPosition: [0, 0.5] // front});frontWheel.setSideFriction(4);// Back wheelvar backWheel = vehicle.addWheel({    localPosition: [0, -0.5] // back});backWheel.setSideFriction(3); // Less side friction on back wheel makes it easier to driftvehicle.addToWorld(game.physics.p2.world);        // Steer value zero means straight forward. Positive is left and negative right.frontWheel.steerValue = Math.PI / 16;        // Engine force forwardbackWheel.engineForce = 10;backWheel.setBrakeForce(0);

Then, when I try to update the vehicle, in order to see the results, I get the following error:

vehicle.chassisBody.vectorToWorldFrame is not a function(..)

The error makes sense, since the vectorToWorldFrame function is located in vehicle.chassisBody.data.vectorToWorldFrame instead.

 

I think proper implementation of this class in Phaser could help a lot of novice game designers out. Or am I missing something in the Phaser documentation? Any leads on how to best approach this?

Link to comment
Share on other sites

this.car = game.add.sprite(x, y, sprite, 0);        // Create the vehiclethis.vehicle = new TopDownVehicle(this.car.body);// Add one front wheel and one back wheel - we don't actually need four :)this.frontWheel = this.vehicle.addWheel({    localPosition: [0, 0.5] // front});this.frontWheel.setSideFriction(4);// Back wheelthis.backWheel = this.vehicle.addWheel({    localPosition: [0, -0.5] // back});this.backWheel.setSideFriction(3); // Less side friction on back wheel makes it easier to driftthis.vehicle.addToWorld(this.game.physics.p2.world);        // Steer value zero means straight forward. Positive is left and negative right.this.frontWheel.steerValue = Math.PI / 16;        // Engine force forwardthis.backWheel.engineForce = 10;this.backWheel.setBrakeForce(0);

Try if this helps.

Link to comment
Share on other sites

  • 2 years later...

Old topic, but I'm struggling with the same problem right now. Does anyone know how to properly implement this TopDownVehicle class? I was trying to solve it basing on: click and click2 . This is how my code looks like:

        this.player = this.game.add.sprite(x, y, sprite);        
        this.physics.startSystem(Phaser.Physics.P2JS);
        this.physics.p2.enable(this.player);


        this.vehicle = new p2.TopDownVehicle(this.player.body);

        // this.player.body.addToWorld(this.game.physics.p2.world);

        this.frontWheel = this.vehicle.addWheel({
            localPosition: [0, 0.5]
        });

        this.frontWheel.setSideFriction(4);

        this.backWheel = this.vehicle.addWheel({
            localPosition: [0, -0.5]
        });

        this.backWheel.setSideFriction(3);

        this.vehicle.addToWorld(this.game.physics.p2.world); 

The error that I receive is:

this.vehicle.chassisBody.vectorToWorldFrame is not a function

I can't find anything in the phaser documentation what could help me. Do you have any suggestions how to solve it?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...