Jump to content

Sprite Falling Through P2 Sprites


gregkarber
 Share

Recommended Posts

I'm working on a procedurally-generated mountain climbing game where you play as a head of lettuce. You can play the working demo here.

 

I have a bunch of work to do regarding streamlining the code and the gameplay, but I can't seem to resolve a problem. When the head of lettuce falls from a substantial height, he falls through the black rectangle sprites I use for the ground.

 

This means it falls through platforms. In addition, since the ground is only one physics-enabled sprite deep, if he falls through that, he falls through the world and the game is over.

 

I feel like perhaps tweaking the Contact Materials might help solve this problem, but I can't find sufficient docs to really understand.  Can anybody help me with this? I have copy-pasted some of the relevant passages of code below, but the full text is available as an attached text file. (You can also view it here.)

 

Any help (even just directions to pursue) would be enormously helpful! Thank you very much.

this.lettuce.body.setMaterial(this.spriteMaterial);
this.spriteMaterial = game.physics.p2.createMaterial('spriteMaterial', this.lettuce.body);
this.worldMaterial = game.physics.p2.createMaterial('worldMaterial');
this.platformMaterial = game.physics.p2.createMaterial('platformMaterial');
this.smoothMaterial = game.physics.p2.createMaterial('smoothMaterial');
this.smootherMaterial = game.physics.p2.createMaterial('smootherMaterial');
this.lettuce.body.setMaterial(this.spriteMaterial);
game.physics.p2.setWorldMaterial(this.worldMaterial, true, true, true, true);

var contactMaterial = game.physics.p2.createContactMaterial(this.spriteMaterial, this.platformMaterial);
contactMaterial.friction = 7;
contactMaterial.restitution = .1;
contactMaterial.stiffness = 1e7;
contactMaterial.relaxation = 7;
contactMaterial.frictionStiffness = 1e7;
contactMaterial.frictionRelaxation = 3;
contactMaterial.surfaceVelocity = 0;

var contactMaterial2 = game.physics.p2.createContactMaterial(this.smoothMaterial, this.platformMaterial);
contactMaterial2.friction = 0;
contactMaterial2.restitution = 0;
contactMaterial2.stiffness = 1e7;
contactMaterial2.relaxation = 10;
contactMaterial2.frictionStiffness = 1e7;
contactMaterial2.frictionRelaxation = 3;
contactMaterial2.surfaceVelocity = 0;

var contactMaterial3 = game.physics.p2.createContactMaterial(this.smootherMaterial, this.platformMaterial);
contactMaterial3.friction = 0;
contactMaterial3.restitution = 0;
contactMaterial3.stiffness = 0;
contactMaterial3.relaxation = 0;
contactMaterial3.frictionStiffness = 0;
contactMaterial3.frictionRelaxation = 0;
contactMaterial3.surfaceVelocity = 0; 

And the update function, among other things, calls this: 

friction_manager: function() { // so that ceilings are smooth
		   if (this.checkIfCeiling()) {
				this.lettuce.body.setMaterial(this.smoothMaterial);
			}
		   else this.lettuce.body.setMaterial(this.spriteMaterial);
},

And this is the function that actually places and sets the platforms:

	place_block: function(x,y, bodiful) {		
		this.visiblesprites.push((Math.round(x) + ',' + Math.round(y)));
		x = x*TILE;
		y = y*TILE;
		var ground = this.platforms.getFirstDead();
		ground.reset(x,y);
		if (bodiful) {
			game.physics.p2.enable(ground);
			ground.body.static = true;
        	ground.body.setMaterial(this.platformMaterial);
        }
        else {
			ground.anchor.setTo(0.5, 0.5);
        }
	},

I also wrote this to limit the speed of the lettuce falling, in the hopes that that would fix it, but it didn't, really:

if (this.lettuce.body.velocity.y > 2000) this.lettuce.body.velocity.y = 2000;

Anyway, if anyone can offer any help, that would be hugely appreciated!

main2.txt

Link to comment
Share on other sites

  • 1 month later...
 Share

  • Recently Browsing   0 members

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