Jump to content

P2 Sprites not colliding when .outOfBoundsKill = true;


gregkarber
 Share

Recommended Posts

I am working on an infinite climber of sorts, and I'm trying to create tiles on the side of the screen the player is going. However, if I enable .checkWorldBounds and .outOfBoundsKill, which is required for performance, the tiles that are created do not collide with my sprite.

	place_block: function(x,y) {		x = x*TILE;		y = y*TILE;		var ground = this.platforms.getFirstDead();		ground.reset(x,y);		game.physics.p2.enable(ground);		ground.checkWorldBounds = true;		ground.outOfBoundsKill = true;		ground.body.static = true;                ground.body.setMaterial(this.platformMaterial);	},

This all works perfectly during my setup, when the only sprites being spawned are the ones currently visible:

	learn_box: function() { // learn the borders of the box to start		this.edges[0] = Math.floor((this.game.camera.view.top)/TILE)-1;		this.edges[1] = Math.floor((this.game.camera.view.right)/TILE)-2;		this.edges[2] = Math.floor((this.game.camera.view.bottom)/TILE)+1;		this.edges[3] = Math.floor((this.game.camera.view.left)/TILE)-1;		console.log("up: " + this.edges[0] + " down: " + this.edges[2] + " left: " + this.edges[3] + " right: " + this.edges[1]);	},	load_sprites: function(name) {		for (var b = this.edges[3]; b <= this.edges[1]; b++) {			for (var c = this.edges[0]; c <= this.edges[2]; c++) {				if (this.tile_lookup(b,c) == "1") this.place_block(b,c);			}		}	},

However, for sprites that are created with the following code, it doesn't work:

    check_new: function() { // checks to see if new borders exist        this.newedge[0] = Math.floor((this.game.camera.view.top)/TILE)-1;        this.newedge[1] = Math.floor((this.game.camera.view.right)/TILE)+1;        this.newedge[2] = Math.floor((this.game.camera.view.bottom)/TILE)+1;        this.newedge[3] = Math.floor((this.game.camera.view.left)/TILE)-1;                if (this.newedge[0] < this.edges[0]) this.load_line(0);        if (this.newedge[0] > this.edges[0]) this.load_line(2);        if (this.newedge[1] < this.edges[1]) this.load_line(3);        if (this.newedge[1] > this.edges[1]) this.load_line(1);                this.new_to_old();    },        new_to_old: function() {        for (var a = 0; a < 4; a++) this.edges[a] = this.newedge[a];    },        load_line: function(dir) {        //console.log("dir: " + dir);            for (var c = this.newedge[((dir + 3) % 4)]; c <= this.newedge[((dir+1) % 4)]; c++) {                if ((dir % 2) == 0) {                    if (this.tile_lookup(c, this.newedge[dir]) == "1") this.place_block(c, this.newedge[dir]);                } else if (this.tile_lookup(this.newedge[dir], c) == "1") this.place_block(this.newedge[dir], c);            }    },

Even if I adjust it to not create the ground sprites until they are visible on-screen, it still seems to create empty sprites, sprites that are visible but do not seem to have their P2 physics bodies enabled.

 

If I comment out .outOfBoundsKill and .checkWorldBounds, then it works as intended, but of course I quickly run out of sprites to use (and it gets real slow real quick).

 

Can anybody see what my problem is? Any help would be great. I'm so frustrated at my inability to figure this out.

 

EDIT: It occurred to me that you guys might want my create() code, as well. Here it is:

	create: function() {		this.game.world.setBounds(0*TILE, 0*TILE, 100*TILE, 100*TILE);		game.physics.startSystem(Phaser.Physics.P2JS);		game.physics.p2.defaultRestitution = 0.8;		game.physics.p2.gravity.y = 300;		game.physics.p2.setImpactEvents(true);				//var mountain_group =  game.physics.p2.createCollisionGroup(); 		//var lettuce_collide = game.physics.p2.createCollisionGroup(); 				this.last_platform = 0;				this.trees1 = game.add.group();		this.trees1.createMultiple(200, 'tree1');		this.trees2 = game.add.group();		this.trees2.createMultiple(200, 'tree2');		this.trees3 = game.add.group();		this.trees3.createMultiple(200, 'tree3');				this.lettuce = this.game.add.sprite(4*TILE, 88*TILE, 'lettuce');		game.physics.p2.enable(this.lettuce);				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.edges = new Array();		this.newedge = new Array();				this.lettuce.scale.x = .5;		this.lettuce.scale.y = .5;		this.lettuce.body.setCircle(11);		this.lettuce.body.dynamic = true;		//this.lettuce.body.applyDamping(500);		this.lettuce.body.data.gravityScale = 5;				this.platforms = game.add.group();		this.platforms.createMultiple(100, 'square');				//this.lettuce.body.setCollisionGroup(lettuce_collide);				//this.lettuce.body.collides(mountain_group);				var space_key = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);		space_key.onDown.add(this.jump, this);				this.cursors = game.input.keyboard.createCursorKeys();		this.game.camera.position.y = this.lettuce.y;		this.game.camera.setPosition(this.lettuce.x, this.lettuce.y);		this.lettuce.body.setMaterial(this.spriteMaterial);		    	//  4 trues = the 4 faces of the world in left, right, top, bottom order    	game.physics.p2.setWorldMaterial(this.worldMaterial, true, true, true, true);    	var contactMaterial = game.physics.p2.createContactMaterial(this.spriteMaterial, this.platformMaterial);    	contactMaterial.friction = 7;     // Friction to use in the contact of these two materials.    	contactMaterial.restitution = 0;  // Restitution (i.e. how bouncy it is!) to use in the contact of these two materials.    	contactMaterial.stiffness = 1e7;    // Stiffness of the resulting ContactEquation that this ContactMaterial generate.    	contactMaterial.relaxation = 10;     // Relaxation of the resulting ContactEquation that this ContactMaterial generate.    	contactMaterial.frictionStiffness = 1e7;    // Stiffness of the resulting FrictionEquation that this ContactMaterial generate.    	contactMaterial.frictionRelaxation = 3;     // Relaxation of the resulting FrictionEquation that this ContactMaterial generate.    	contactMaterial.surfaceVelocity = 0;        // Will add surface velocity to this material. If bodyA rests on top if bodyB, and the surface velocity is positive, bodyA will slide to the right.                var contactMaterial2 = game.physics.p2.createContactMaterial(this.smoothMaterial, this.platformMaterial);    	contactMaterial2.friction = 0;     // Friction to use in the contact of these two materials.    	contactMaterial2.restitution = 0;  // Restitution (i.e. how bouncy it is!) to use in the contact of these two materials.    	contactMaterial2.stiffness = 1e7;    // Stiffness of the resulting ContactEquation that this ContactMaterial generate.    	contactMaterial2.relaxation = 10;     // Relaxation of the resulting ContactEquation that this ContactMaterial generate.    	contactMaterial2.frictionStiffness = 1e7;    // Stiffness of the resulting FrictionEquation that this ContactMaterial generate.    	contactMaterial2.frictionRelaxation = 3;     // Relaxation of the resulting FrictionEquation that this ContactMaterial generate.    	contactMaterial2.surfaceVelocity = 0;        // Will add surface velocity to this material. If bodyA rests on top if bodyB, and the surface velocity is positive, bodyA will slide to the right.                        this.learn_box();		this.clear_grid();		this.build_ground();		this.load_sprites();				this.game.camera.follow(this.lettuce);	},

Thank you, anybody who might help.

Link to comment
Share on other sites

I tried writing a function that deletes sprites when they exit the camera viewpoint, thinking that outOfBoundsKill was where the problem was, but it still occurs.

 

It seems like sprites summoned using .reset that have already been created and killed don't pull their bodies with them, and even if you enable the body again, nothing happens.

 

**EDIT:** Based on this theory, I fixed it by adding sprite.body.destroy() before I killed each sprite. Is there some way to do that with sprite.outOfBoundsKill = true, but I couldn't figure it out.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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