Sign in to follow this  

Sprite Falling Through P2 Sprites

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.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');
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()) {
		   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();
		if (bodiful) {
			ground.body.static = true;
        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!


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.