Jump to content

Phaser lock constraint


Yanifska
 Share

Recommended Posts

Hi, 

 

I have obstacles attached to a revolving planet with a lockConstraint.

When the player hit an obstacle I want him to get stuck on the obstcale.

I try to do that also with a lock constraint but I got many problems:

 

1. the obstacle rotate a littlebit on it own axis

2.the character don't get it's rotation updated according to the planet rotation

 

here are bits of code

// set up the obstacle groupthis.obstacles = this.game.add.group();this.obstacles.createMultiple(20, 'kit');this.obstacles.forEach(function(obstacle) {obstacle.anchor.setTo(0.5,1);this.game.physics.p2.enable(obstacle, true);obstacle.kinematic = true;}, this);
//for now a timer add the obstaclesaddObstacle: function () {var obstacle = this.obstacles.getFirstDead();obstacle.reset(960, 960);obstacle.scale.setTo(this.game.rnd.integerInRange(3, 7),this.game.rnd.integerInRange(3, 7));obstacle.body.setRectangle(obstacle.width, obstacle.height);obstacle.angle = 90;obstacle.body.setCollisionGroup(this.obstacleCollisionGroup);obstacle.body.collides(this.playerCollisionGroup);//correct the position according to the rotation of the planetvar correctAngle = this.planetDummy.rotation * -1;var x1 = this.ground.height + obstacle.height*0.5;var y1 = 0;var x2 = x1*Math.cos(correctAngle) - y1*Math.sin(correctAngle);var y2 = x1*Math.sin(correctAngle) + y1*Math.cos(correctAngle);var initRotation = obstacle.rotation - this.planetDummy.rotation;this.game.physics.p2.createLockConstraint( this.planetDummy, obstacle, [ x2*-1, y2*-1], initRotation);},
//the playerthis.player = this.game.add.sprite(BasicGame.gameWidth * 0.5, this.ground.y - this.groundblock1.height - 100, 'thief');this.player.anchor.x = 0.5;this.player.anchor.y = 2;  this.game.physics.p2.enable(this.player,true);this.player.body.setRectangle(90,200);this.player.body.fixedRotation = true;//add a sensor to detect side collisionthis.playersensor = this.player.body.addRectangle(15, this.player.height * 1.2, this.player.width * 0.33 ,0);this.playersensor.sensor=true;this.player.body.setCollisionGroup(this.playerCollisionGroup);this.player.body.collides(this.obstacleCollisionGroup);this.player.body.collides(this.groundCollisionGroup);//if the sensor detect a collision it runs the hitObstacle functionthis.player.body.onBeginContact.add(this.hitObstacle,this);
//when the character sensor detect a collisionhitObstacle: function (bodyA,shapeA,shapeB,contactEquation) {if(shapeA.sensor && !this.gameOver){this.gameOver = true;his.player.body.fixedRotation = false;//so it will get its rotation updatedthis.groundDummy.body.clearCollision(true,true);var lockConstraint = this.game.physics.p2.createLockConstraint(bodyA, this.player, [bodyA.sprite.width*0.5, bodyA.sprite.y - this.player.y], 0);lockConstraint.collideConnected = false;}},
// in the update functionthis.planetDummy.body.rotateLeft(12);this.player.body.velocity.x = 0;this.obstacles.forEach( this.obstacleOut, this);

any idea ?

thank

Yaniv

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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