# Circular Motion & Gravity

## Recommended Posts

Hi All,

I have implemented planatary gravity using the below link

Now i need the object just move over the planet with gravity .

How to do it kindly help please ?????????????

##### Share on other sites

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 by samid737
corrected applyforce
##### Share on other sites

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

##### Share on other sites

you can do this in two ways:

``````function addCrate(e){
var crateSprite = game.add.sprite(e.x, e.y, "crate");
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));
} ``````

##### Share on other sites

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

##### Share on other sites

I have changed the frixtion and now it moves smoothly

is there a way to lock rotation while jumping

setFixedRotation(false) is not working

##### Share on other sites

you can try angularDamping, won't fully lock it, but gets pretty close:

``````crateSprite.body.angularDamping=1000000000;
``````

• ### Recently Browsing   0 members

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