Skip to content

Commit 7a8c96d

Browse files
committed
Working gravity + contacts + platformer demo :)
1 parent 1e29e28 commit 7a8c96d

3 files changed

Lines changed: 214 additions & 7 deletions

File tree

examples/wip/platforms.js

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
3+
4+
function preload() {
5+
6+
game.load.spritesheet('dude', 'assets/games/starstruck/dude.png', 32, 48);
7+
game.load.image('background', 'assets/games/starstruck/background2.png');
8+
game.load.image('box', 'assets/sprites/block.png');
9+
10+
}
11+
12+
var player;
13+
var facing = 'left';
14+
var jumpTimer = 0;
15+
var cursors;
16+
var jumpButton;
17+
var box1;
18+
var box2;
19+
20+
function create() {
21+
22+
game.stage.backgroundColor = '#000000';
23+
24+
bg = game.add.tileSprite(0, 0, 800, 600, 'background');
25+
bg.fixedToCamera = true;
26+
27+
// map = game.add.tilemap('level1');
28+
29+
// map.addTilesetImage('tiles-1');
30+
31+
// map.setCollisionByExclusion([ 13, 14, 15, 16, 46, 47, 48, 49, 50, 51 ]);
32+
33+
// layer = map.createLayer('Tile Layer 1');
34+
35+
// Un-comment this on to see the collision tiles
36+
// layer.debug = true;
37+
38+
// layer.resizeWorld();
39+
40+
// game.physics.setBoundsToWorld();
41+
42+
game.physics.gravity.y = 20;
43+
game.physics.friction = 0.5;
44+
45+
// Materials
46+
var groundMaterial = game.physics.createMaterial('ground');
47+
var characterMaterial = game.physics.createMaterial('character');
48+
var boxMaterial = game.physics.createMaterial('box');
49+
50+
player = game.add.sprite(32, 320, 'dude');
51+
player.physicsEnabled = true;
52+
player.body.fixedRotation = true;
53+
player.body.setMaterial(characterMaterial);
54+
55+
player.animations.add('left', [0, 1, 2, 3], 10, true);
56+
player.animations.add('turn', [4], 20, true);
57+
player.animations.add('right', [5, 6, 7, 8], 10, true);
58+
59+
box1 = game.add.sprite(200, 300, 'box');
60+
box1.physicsEnabled = true;
61+
// box1.body.fixedRotation = true;
62+
box1.body.setMaterial(boxMaterial);
63+
64+
box2 = game.add.sprite(400, 300, 'box');
65+
box2.physicsEnabled = true;
66+
// box2.body.fixedRotation = true;
67+
box2.body.setMaterial(boxMaterial);
68+
69+
// Set the material along the ground
70+
game.physics.setWorldMaterial(groundMaterial, false, false, false, true);
71+
72+
var groundCharacterCM = game.physics.createContactMaterial(groundMaterial, characterMaterial, { friction: 0.0 }); // no friction between character and ground
73+
var boxCharacterCM = game.physics.createContactMaterial(boxMaterial, characterMaterial, { friction: 0.0 }); // No friction between character and boxes
74+
var boxGroundCM = game.physics.createContactMaterial(boxMaterial, groundMaterial, { friction: 0.6 }); // Between boxes and ground
75+
76+
// game.camera.follow(player);
77+
78+
cursors = game.input.keyboard.createCursorKeys();
79+
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
80+
81+
}
82+
83+
function update() {
84+
85+
player.body.velocity.x = 0;
86+
87+
if (cursors.left.isDown)
88+
{
89+
player.body.moveLeft(150);
90+
91+
if (facing != 'left')
92+
{
93+
player.animations.play('left');
94+
facing = 'left';
95+
}
96+
}
97+
else if (cursors.right.isDown)
98+
{
99+
player.body.moveRight(150);
100+
101+
if (facing != 'right')
102+
{
103+
player.animations.play('right');
104+
facing = 'right';
105+
}
106+
}
107+
else
108+
{
109+
if (facing != 'idle')
110+
{
111+
player.animations.stop();
112+
113+
if (facing == 'left')
114+
{
115+
player.frame = 0;
116+
}
117+
else
118+
{
119+
player.frame = 5;
120+
}
121+
122+
facing = 'idle';
123+
}
124+
}
125+
126+
if (jumpButton.isDown && game.time.now > jumpTimer && checkIfCanJump())
127+
{
128+
player.body.moveUp(300);
129+
jumpTimer = game.time.now + 750;
130+
}
131+
132+
}
133+
134+
function checkIfCanJump(){
135+
var yAxis = p2.vec2.fromValues(0,1);
136+
var result = false;
137+
for(var i=0; i<game.physics.world.narrowphase.contactEquations.length; i++){
138+
var c = game.physics.world.narrowphase.contactEquations[i];
139+
if(c.bi === player.body.data || c.bj === player.body.data){
140+
var d = p2.vec2.dot(c.ni,yAxis); // Normal dot Y-axis
141+
if(c.bi === player.body.data) d *= -1;
142+
if(d > 0.5) result = true;
143+
}
144+
}
145+
return result;
146+
}
147+
148+
149+
function render () {
150+
151+
// if (player.debug)
152+
// {
153+
// game.debug.renderPhysicsBody(player.body);
154+
// game.debug.renderBodyInfo(player, 16, 24);
155+
// }
156+
157+
}

src/physics/InversePointProxy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Object.defineProperty(Phaser.Physics.InversePointProxy.prototype, "x", {
3434

3535
set: function (value) {
3636

37-
this.destination[0] *= -value;
37+
this.destination[0] = -value;
3838

3939
}
4040

@@ -54,7 +54,7 @@ Object.defineProperty(Phaser.Physics.InversePointProxy.prototype, "y", {
5454

5555
set: function (value) {
5656

57-
this.destination[1] *= -value;
57+
this.destination[1] = -value;
5858

5959
}
6060

src/physics/World.js

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ Phaser.Physics.World = function (game) {
5454
*/
5555
this.bounds = null;
5656

57+
/**
58+
* @property {array} _wallShapes - The wall bounds shapes.
59+
* @private
60+
*/
61+
this._wallShapes = [ null, null, null, null ];
62+
5763
/**
5864
* @property {Phaser.Signal} onBodyAdded - Dispatched when a new Body is added to the World.
5965
*/
@@ -216,6 +222,45 @@ Phaser.Physics.World.prototype = {
216222

217223
},
218224

225+
/**
226+
* Sets the given material against the 4 bounds of this World.
227+
*
228+
* @method Phaser.Physics#setWorldMaterial
229+
* @param {Phaser.Physics.Material} material - The material to set.
230+
* @param {boolean} [left=true] - If true will set the material on the left bounds wall.
231+
* @param {boolean} [right=true] - If true will set the material on the right bounds wall.
232+
* @param {boolean} [top=true] - If true will set the material on the top bounds wall.
233+
* @param {boolean} [bottom=true] - If true will set the material on the bottom bounds wall.
234+
*/
235+
setWorldMaterial: function (material, left, right, top, bottom) {
236+
237+
if (typeof left === 'undefined') { left = true; }
238+
if (typeof right === 'undefined') { right = true; }
239+
if (typeof top === 'undefined') { top = true; }
240+
if (typeof bottom === 'undefined') { bottom = true; }
241+
242+
if (left && this._wallShapes[0])
243+
{
244+
this._wallShapes[0].material = material;
245+
}
246+
247+
if (right && this._wallShapes[1])
248+
{
249+
this._wallShapes[1].material = material;
250+
}
251+
252+
if (top && this._wallShapes[2])
253+
{
254+
this._wallShapes[2].material = material;
255+
}
256+
257+
if (bottom && this._wallShapes[3])
258+
{
259+
this._wallShapes[3].material = material;
260+
}
261+
262+
},
263+
219264
/**
220265
* Sets the bounds of the Physics world to match the given world pixel dimensions.
221266
* You can optionally set which 'walls' to create: left, right, top or bottom.
@@ -264,22 +309,26 @@ Phaser.Physics.World.prototype = {
264309

265310
if (left)
266311
{
267-
this.bounds.addShape(new p2.Plane(), [this.game.math.px2p(-hw), 0], 1.5707963267948966 );
312+
this._wallShapes[0] = new p2.Plane();
313+
this.bounds.addShape(this._wallShapes[0], [this.game.math.px2p(-hw), 0], 1.5707963267948966 );
268314
}
269315

270316
if (right)
271317
{
272-
this.bounds.addShape(new p2.Plane(), [this.game.math.px2p(hw), 0], -1.5707963267948966 );
318+
this._wallShapes[1] = new p2.Plane();
319+
this.bounds.addShape(this._wallShapes[1], [this.game.math.px2p(hw), 0], -1.5707963267948966 );
273320
}
274321

275322
if (top)
276323
{
277-
this.bounds.addShape(new p2.Plane(), [0, this.game.math.px2p(-hh)], -3.141592653589793 );
324+
this._wallShapes[2] = new p2.Plane();
325+
this.bounds.addShape(this._wallShapes[2], [0, this.game.math.px2p(-hh)], -3.141592653589793 );
278326
}
279327

280328
if (bottom)
281329
{
282-
this.bounds.addShape(new p2.Plane(), [0, this.game.math.px2p(hh)] );
330+
this._wallShapes[3] = new p2.Plane();
331+
this.bounds.addShape(this._wallShapes[3], [0, this.game.math.px2p(hh)] );
283332
}
284333

285334
this.world.addBody(this.bounds);
@@ -514,9 +563,10 @@ Phaser.Physics.World.prototype = {
514563
* @method Phaser.Physics.World#createContactMaterial
515564
* @param {Phaser.Physics.Material} [materialA] - The first Material to create the ContactMaterial from. If undefined it will create a new Material object first.
516565
* @param {Phaser.Physics.Material} [materialB] - The second Material to create the ContactMaterial from. If undefined it will create a new Material object first.
566+
* @param {object} [options] - Material options object.
517567
* @return {Phaser.Physics.ContactMaterial} The Contact Material that was created.
518568
*/
519-
createContactMaterial: function (materialA, materialB) {
569+
createContactMaterial: function (materialA, materialB, options) {
520570

521571
if (typeof materialA === 'undefined') { materialA = this.createMaterial(); }
522572
if (typeof materialB === 'undefined') { materialB = this.createMaterial(); }

0 commit comments

Comments
 (0)