Jump to content

P2 physics and changing body shape


rpiller
 Share

Recommended Posts

I'm trying to change the body shape to a circle but this is giving me an error of: "undefined is not a function" when the addShape() code below is in. If I comment that line out the game runs but of course my player doesn't have a body to collide with. Any ideas on how to add a shape using P2 physics?

player = game.add.sprite(150, 320, 'player');player.anchor.setTo(0.5, 0.5);player.scale = { x: 2, y: 2 };game.physics.p2.enable(player);player.body.fixedRotation = true;player.body.clearShapes();player.body.addShape(p2.Circle);
Link to comment
Share on other sites

Your error is that you pass the class p2.Circle itself. You have to pass an instance of p2.Circle. But you do not have to it manually in your case. Use the convenience method setCircle. This will clear all shapes and create a single circle. Use the body debug flag in p2.enable to actually see what you're creating.

player = game.add.sprite(150, 320, 'player');player.anchor.setTo(0.5, 0.5);player.scale = { x: 2, y: 2 };game.physics.p2.enable(player);//use this if you want to see the shapes. This enables debugging for the body.//Remove in production!//game.physics.p2.enable(player, true);player.body.fixedRotation = true;//variant 1:player.body.setCircle(20); //clear shapes & add a circle with radius of 20px//variant 2://to do it the manual wayvar radius = game.physics.p2.pxm(20) //convert 20px to metersvar circleShape = new p2.Circle(radius) //create a circle shapeplayer.body.clearShapes() //clear all previous shapesplayer.body.addShape(circleShape)//you can also add the circle at [10,15] from the center of the body//player.body.addShape(circleShape, 10, 15)
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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