Fixed issues with the contacts and world bounds. New labs demo created.

This commit is contained in:
photonstorm 2014-02-20 04:21:14 +00:00
parent 9fcab5ef26
commit 91c0299d4c
4 changed files with 195 additions and 36 deletions

View file

@ -23,14 +23,15 @@ function create() {
bg = game.add.tileSprite(0, 0, 800, 600, 'background');
bg.fixedToCamera = true;
// game.physics.gravity.y = 20;
game.physics.gravity.y = 20;
game.physics.friction = 0.5;
game.physics.setBoundsToWorld();
// game.physics.setBoundsToWorld();
var playerCG = game.physics.createCollisionGroup();
var boxCG = game.physics.createCollisionGroup();
player = game.add.sprite(50, 550, 'dude');
player.name = 'player';
player.physicsEnabled = true;
player.body.fixedRotation = true;
player.body.setCollisionGroup(playerCG);
@ -44,6 +45,7 @@ function create() {
for (var i = 0; i < 10; i++)
{
var box = boxes.create(200 + (i * 50), 550, 'box');
box.name = 'box' + i;
box.scale.set(0.5);
box.physicsEnabled = true;
box.body.setCollisionGroup(boxCG);
@ -62,7 +64,7 @@ function create() {
function gotBox(body1, body2, shape1, shape2) {
console.log('gotBox');
console.log('gotBox', body1.sprite.name, body2.sprite.name);
body2.sprite.kill();

View file

@ -1,5 +1,6 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
// var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
@ -24,26 +25,14 @@ function create() {
bg = game.add.tileSprite(0, 0, 800, 600, 'background');
bg.fixedToCamera = true;
// map = game.add.tilemap('level1');
game.physics.gravity.y = 20.0;
// map.addTilesetImage('tiles-1');
game.world.setBounds(0, 0, 2000, 600);
// map.setCollisionByExclusion([ 13, 14, 15, 16, 46, 47, 48, 49, 50, 51 ]);
// layer = map.createLayer('Tile Layer 1');
// Un-comment this on to see the collision tiles
// layer.debug = true;
// layer.resizeWorld();
// game.physics.setBoundsToWorld();
// game.physics.gravity.y = 9.78;
game.physics.setBoundsToWorld(true, true, false, true, false);
game.physics.world.gravity[1] = -20;
game.physics.friction = 0.5;
game.physics.enableBodySleeping = true;
game.physics.world.solver.stiffness = 1e20;
game.physics.world.solver.relaxation = 3;
@ -65,12 +54,12 @@ function create() {
boxes = game.add.group();
for (var i = 0; i < 50; i++)
for (var i = 0; i < 100; i++)
{
var box = boxes.create(game.rnd.integerInRange(200, 700), game.rnd.integerInRange(-200, 400), 'box');
// box.scale.set(0.5);
box.scale.set(game.rnd.realInRange(0.2, 0.7));
var box = boxes.create(game.rnd.integerInRange(200, 1700), game.rnd.integerInRange(-200, 400), 'box');
box.scale.set(game.rnd.realInRange(0.2, 0.6));
box.physicsEnabled = true;
box.body.allowSleep = true;
box.body.mass = 10;
box.body.setMaterial(boxMaterial);
box.body.fixedRotation = true;
@ -83,10 +72,7 @@ function create() {
var boxCharacterCM = game.physics.createContactMaterial(boxMaterial, characterMaterial, { friction: 0.0 }); // No friction between character and boxes
var boxGroundCM = game.physics.createContactMaterial(boxMaterial, groundMaterial, { friction: 0.6 }); // Between boxes and ground
console.log(groundCharacterCM);
console.log(boxGroundCM);
// game.camera.follow(player);
game.camera.follow(player);
cursors = game.input.keyboard.createCursorKeys();
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
@ -95,6 +81,8 @@ function create() {
function update() {
bg.tilePosition.x = -game.camera.view.x;
if (cursors.left.isDown)
{
player.body.moveLeft(200);

View file

@ -0,0 +1,157 @@
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.spritesheet('dude', 'assets/games/starstruck/dude.png', 32, 48);
game.load.image('background', 'assets/games/starstruck/background2.png');
game.load.image('box', 'assets/sprites/block.png');
}
var player;
var facing = 'left';
var jumpTimer = 0;
var cursors;
var jumpButton;
var boxes;
function create() {
game.stage.backgroundColor = '#000000';
bg = game.add.tileSprite(0, 0, 800, 600, 'background');
bg.fixedToCamera = true;
game.physics.gravity.y = 20.0;
game.world.setBounds(0, 0, 2000, 600);
game.physics.setBoundsToWorld(true, true, false, true, false);
game.physics.friction = 0.5;
game.physics.enableBodySleeping = true;
game.physics.world.solver.stiffness = 1e20;
game.physics.world.solver.relaxation = 3;
// Materials
var groundMaterial = game.physics.createMaterial('ground');
var characterMaterial = game.physics.createMaterial('character');
var boxMaterial = game.physics.createMaterial('box');
player = game.add.sprite(100, -400, 'dude');
player.physicsEnabled = true;
player.body.fixedRotation = true;
player.body.setMaterial(characterMaterial);
player.body.mass = 1;
player.body.damping = 0.5;
player.animations.add('left', [0, 1, 2, 3], 10, true);
player.animations.add('turn', [4], 20, true);
player.animations.add('right', [5, 6, 7, 8], 10, true);
boxes = game.add.group();
for (var i = 0; i < 100; i++)
{
var box = boxes.create(game.rnd.integerInRange(200, 1700), game.rnd.integerInRange(-200, 400), 'box');
box.scale.set(game.rnd.realInRange(0.2, 0.6));
box.physicsEnabled = true;
box.body.allowSleep = true;
box.body.mass = 10;
box.body.setMaterial(boxMaterial);
box.body.fixedRotation = true;
}
// Set the material along the ground
game.physics.setWorldMaterial(groundMaterial);
var groundCharacterCM = game.physics.createContactMaterial(groundMaterial, characterMaterial, { friction: 0.0 }); // no friction between character and ground
var boxCharacterCM = game.physics.createContactMaterial(boxMaterial, characterMaterial, { friction: 0.0 }); // No friction between character and boxes
var boxGroundCM = game.physics.createContactMaterial(boxMaterial, groundMaterial, { friction: 0.6 }); // Between boxes and ground
game.camera.follow(player);
cursors = game.input.keyboard.createCursorKeys();
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
}
function update() {
bg.tilePosition.x = -game.camera.view.x;
if (cursors.left.isDown)
{
player.body.moveLeft(200);
if (facing != 'left')
{
player.animations.play('left');
facing = 'left';
}
}
else if (cursors.right.isDown)
{
player.body.moveRight(200);
if (facing != 'right')
{
player.animations.play('right');
facing = 'right';
}
}
else
{
player.body.velocity.x = 0;
if (facing != 'idle')
{
player.animations.stop();
if (facing == 'left')
{
player.frame = 0;
}
else
{
player.frame = 5;
}
facing = 'idle';
}
}
if (jumpButton.isDown && game.time.now > jumpTimer && checkIfCanJump())
{
player.body.moveUp(300);
jumpTimer = game.time.now + 750;
}
}
function checkIfCanJump(){
var yAxis = p2.vec2.fromValues(0,1);
var result = false;
for(var i=0; i<game.physics.world.narrowphase.contactEquations.length; i++){
var c = game.physics.world.narrowphase.contactEquations[i];
if(c.bi === player.body.data || c.bj === player.body.data){
var d = p2.vec2.dot(c.ni,yAxis); // Normal dot Y-axis
if(c.bi === player.body.data) d *= -1;
if(d > 0.5) result = true;
}
}
return result;
}
function render () {
// if (player.debug)
// {
// game.debug.renderPhysicsBody(player.body);
// game.debug.renderBodyInfo(player, 16, 24);
// }
}

View file

@ -149,12 +149,16 @@ Phaser.Physics.World = function (game, config) {
*/
this._collisionGroupID = 2;
this.nothingCollisionGroup = new Phaser.Physics.CollisionGroup(1);
this.boundsCollisionGroup = new Phaser.Physics.CollisionGroup(2);
this.everythingCollisionGroup = new Phaser.Physics.CollisionGroup(2147483648);
this.boundsCollidesWith = [];
// Group vs. Group callbacks
// this.setBoundsToWorld(true, true, true, true, false);
// By default we want everything colliding with everything
this.setBoundsToWorld(true, true, true, true, false);
};
@ -244,8 +248,8 @@ Phaser.Physics.World.prototype = {
*/
beginContactHandler: function (event) {
console.log('beginContactHandler');
console.log(event);
// console.log('beginContactHandler');
// console.log(event);
if (event.bodyA.id > 1 && event.bodyB.id > 1)
{
@ -265,8 +269,8 @@ Phaser.Physics.World.prototype = {
*/
endContactHandler: function (event) {
console.log('endContactHandler');
console.log(event);
// console.log('endContactHandler');
// console.log(event);
if (event.bodyA.id > 1 && event.bodyB.id > 1)
@ -388,9 +392,11 @@ Phaser.Physics.World.prototype = {
if (setCollisionGroup)
{
this._wallShapes[0].collisionGroup = this.boundsCollisionGroup.mask;
// this._wallShapes[0].collisionGroup = this.everythingCollisionGroup.mask;
// this._wallShapes[0].collisionMask = this.everythingCollisionGroup.mask;
}
this.bounds.addShape(this._wallShapes[0], [this.game.math.px2p(-hw), 0], 1.5707963267948966 );
this.bounds.addShape(this._wallShapes[0], [this.game.math.px2pi(-hw), 0], 1.5707963267948966 );
}
if (right)
@ -400,9 +406,11 @@ Phaser.Physics.World.prototype = {
if (setCollisionGroup)
{
this._wallShapes[1].collisionGroup = this.boundsCollisionGroup.mask;
// this._wallShapes[1].collisionGroup = this.everythingCollisionGroup.mask;
// this._wallShapes[1].collisionMask = this.everythingCollisionGroup.mask;
}
this.bounds.addShape(this._wallShapes[1], [this.game.math.px2p(hw), 0], -1.5707963267948966 );
this.bounds.addShape(this._wallShapes[1], [this.game.math.px2pi(hw), 0], -1.5707963267948966 );
}
if (top)
@ -412,9 +420,11 @@ Phaser.Physics.World.prototype = {
if (setCollisionGroup)
{
this._wallShapes[2].collisionGroup = this.boundsCollisionGroup.mask;
// this._wallShapes[2].collisionGroup = this.everythingCollisionGroup.mask;
// this._wallShapes[2].collisionMask = this.everythingCollisionGroup.mask;
}
this.bounds.addShape(this._wallShapes[2], [0, this.game.math.px2p(-hh)], -3.141592653589793 );
this.bounds.addShape(this._wallShapes[2], [0, this.game.math.px2pi(-hh)], -3.141592653589793 );
}
if (bottom)
@ -424,9 +434,11 @@ Phaser.Physics.World.prototype = {
if (setCollisionGroup)
{
this._wallShapes[3].collisionGroup = this.boundsCollisionGroup.mask;
// this._wallShapes[3].collisionGroup = this.everythingCollisionGroup.mask;
// this._wallShapes[3].collisionMask = this.everythingCollisionGroup.mask;
}
this.bounds.addShape(this._wallShapes[3], [0, this.game.math.px2p(hh)] );
this.bounds.addShape(this._wallShapes[3], [0, this.game.math.px2pi(hh)] );
}
this.world.addBody(this.bounds);