Jump to content

Circular Motion & Gravity


GideonSam
 Share

Recommended Posts

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
Link to comment
Share on other sites

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

 Share

  • Recently Browsing   0 members

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