GideonSam Posted August 7, 2017 Share Posted August 7, 2017 Hi All, I have implemented planatary gravity using the below link http://www.emanueleferonato.com/2015/06/19/simulate-planet-gravity-with-phaser-box2d-as-seen-on-angry-birds-space/ Now i need the object just move over the planet with gravity . How to do it kindly help please ????????????? Link to comment Share on other sites More sharing options...
samid737 Posted August 7, 2017 Share Posted August 7, 2017 (edited) If you want your body to move along the surface of the planet, you can add force in the local x direction of the block: if(distance<p.width/2+p.gravityRadius/2){ // calculating angle between the planet and the crate var angle = Phaser.Math.angleBetween(c.x,c.y,p.x,p.y); //add force in the local x direction by pressing D key if (game.input.keyboard.isDown(Phaser.Keyboard.D)){ c.body.applyForce(-10*Math.cos(angle+90),-10*Math.sin(angle+90)); } // add gravity force to the crate in the direction of planet center c.body.applyForce(p.gravityForce*Math.cos(angle)*forceReducer,p.gravityForce*Math.sin(angle)*forceReducer); } Edited August 8, 2017 by samid737 corrected applyforce Link to comment Share on other sites More sharing options...
GideonSam Posted August 8, 2017 Author Share Posted August 8, 2017 Thanks Samid for you reply it works fine but what i want to replicate is Crate should move in a circle by default with gravity On key press it should jump Is it possible to achieve. Kindly help Link to comment Share on other sites More sharing options...
samid737 Posted August 8, 2017 Share Posted August 8, 2017 you can do this in two ways: function addCrate(e){ var crateSprite = game.add.sprite(e.x, e.y, "crate"); crateGroup.add(crateSprite); game.physics.box2d.enable(crateSprite); //crateSprite.body.velocity.x=200; //without force for(i=0;i<4;i++){crateSprite.body.applyForce(10,0);}; //short impulse } when adding the crate, you either set the velocity or you apply the force for a very short amount of time(impulse). To jump vertically up on the planet, you do the same as gravity but opposite direction (minus sign): if (game.input.keyboard.isDown(Phaser.Keyboard.D)){ c.body.applyForce(-10*Math.cos(angle),-10*Math.sin(angle)); } Link to comment Share on other sites More sharing options...
GideonSam Posted August 8, 2017 Author Share Posted August 8, 2017 Thanks samid the jump works great but the movement stops after some time Is it due to friction and can i lock rotation of the crate Link to comment Share on other sites More sharing options...
GideonSam Posted August 8, 2017 Author Share Posted August 8, 2017 I have changed the frixtion and now it moves smoothly is there a way to lock rotation while jumping setFixedRotation(false) is not working Link to comment Share on other sites More sharing options...
samid737 Posted August 8, 2017 Share Posted August 8, 2017 you can try angularDamping, won't fully lock it, but gets pretty close: crateSprite.body.angularDamping=1000000000; Link to comment Share on other sites More sharing options...
Recommended Posts