Skip to content

Commit 170776a

Browse files
committed
rnd updated so the array picks use length -1 (fixes phaserjs#541)
1 parent 384451b commit 170776a

4 files changed

Lines changed: 213 additions & 63 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
// var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
3+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
4+
5+
function preload() {
6+
7+
game.load.image('block', 'assets/sprites/block.png');
8+
game.load.spritesheet('ninja-tiles', 'assets/physics/ninja-tiles128.png', 128, 128, 34);
9+
10+
}
11+
12+
var sprite1;
13+
var sprite2;
14+
var tile;
15+
var cursors;
16+
17+
18+
function create() {
19+
20+
game.physics.startSystem(Phaser.Physics.NINJA);
21+
22+
sprite1 = game.add.sprite(100, 450, 'block');
23+
sprite1.name = 'blockA';
24+
25+
sprite2 = game.add.sprite(600, 450, 'block');
26+
sprite2.name = 'blockB';
27+
sprite2.tint = Math.random() * 0xffffff;
28+
29+
game.physics.ninja.enableAABB([sprite1, sprite2]);
30+
31+
// sprite1.body.immovable = true;
32+
sprite1.body.immovable = false;
33+
// sprite2.body.immovable = true;
34+
sprite2.body.immovable = false;
35+
36+
cursors = game.input.keyboard.createCursorKeys();
37+
38+
game.input.onDown.add(t, this);
39+
40+
}
41+
42+
function t() {
43+
44+
sprite1.body.shape.oldpos.x = sprite1.body.shape.pos.x - 20;
45+
sprite2.body.shape.oldpos.x = sprite2.body.shape.pos.x + 20;
46+
47+
}
48+
49+
function update() {
50+
51+
// Whichever one goes first utterly changes how the collision happens!
52+
game.physics.ninja.collide(sprite1, sprite2);
53+
// game.physics.ninja.collide(sprite2, sprite1);
54+
55+
56+
57+
58+
// game.physics.ninja.collide(sprite1, tile);
59+
// game.physics.ninja.collide(sprite2, tile);
60+
61+
// sprite1.body.moveRight(5);
62+
63+
if (cursors.left.isDown)
64+
{
65+
sprite1.body.moveLeft(20);
66+
}
67+
else if (cursors.right.isDown)
68+
{
69+
sprite1.body.moveRight(20);
70+
}
71+
72+
// if (cursors.up.isDown && sprite1.body.touching.down)
73+
if (cursors.up.isDown)
74+
{
75+
// sprite1.body.moveUp(1000);
76+
sprite1.body.moveUp(30);
77+
}
78+
// else if (cursors.down.isDown)
79+
// {
80+
// sprite1.body.moveDown(20);
81+
// }
82+
83+
}
84+
85+
function render() {
86+
87+
game.debug.text('left: ' + sprite1.body.touching.left, 32, 32);
88+
game.debug.text('right: ' + sprite1.body.touching.right, 256, 32);
89+
game.debug.text('up: ' + sprite1.body.touching.up, 32, 64);
90+
game.debug.text('down: ' + sprite1.body.touching.down, 256, 64);
91+
92+
}

src/math/RandomDataGenerator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ Phaser.RandomDataGenerator.prototype = {
212212
* @return {any} A random member of the array.
213213
*/
214214
pick: function (ary) {
215-
return ary[this.integerInRange(0, ary.length)];
215+
return ary[this.integerInRange(0, ary.length - 1)];
216216
},
217217

218218
/**
@@ -222,7 +222,7 @@ Phaser.RandomDataGenerator.prototype = {
222222
* @return {any} A random member of the array.
223223
*/
224224
weightedPick: function (ary) {
225-
return ary[~~(Math.pow(this.frac(), 2) * ary.length)];
225+
return ary[~~(Math.pow(this.frac(), 2) * (ary.length - 1))];
226226
},
227227

228228
/**

src/physics/ninja/AABB.js

Lines changed: 118 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -212,82 +212,140 @@ Phaser.Physics.Ninja.AABB.prototype = {
212212
*/
213213
reportCollisionVsBody: function (px, py, dx, dy, obj) {
214214

215-
// Here - we check if obj is immovable, etc and then we canswap the p/o values below depending on which is heavy
216-
// But then still need to work out how to split force
217-
218-
var p = this.pos;
219-
var o = this.oldpos;
220-
221-
// Calc velocity
222-
var vx = p.x - o.x;
223-
var vy = p.y - o.y;
224-
225-
// Find component of velocity parallel to collision normal
226-
var dp = (vx * dx + vy * dy);
227-
var nx = dp * dx; //project velocity onto collision normal
228-
229-
var ny = dp * dy; //nx,ny is normal velocity
230-
231-
var tx = vx - nx; //px,py is tangent velocity
232-
var ty = vy - ny;
215+
var vx1 = this.pos.x - this.oldpos.x; // Calc velocity of this object
216+
var vy1 = this.pos.y - this.oldpos.y;
217+
var dp1 = (vx1 * dx + vy1 * dy); // Find component of velocity parallel to collision normal
218+
var nx1 = dp1 * dx; // Project velocity onto collision normal
219+
var ny1 = dp1 * dy; // nx, ny is normal velocity
220+
221+
var dx2 = dx * -1;
222+
var dy2 = dy * -1;
223+
var vx2 = obj.pos.x - obj.oldpos.x; // Calc velocity of colliding object
224+
var vy2 = obj.pos.y - obj.oldpos.y;
225+
var dp2 = (vx2 * dx2 + vy2 * dy2); // Find component of velocity parallel to collision normal
226+
var nx2 = dp2 * dx2; // Project velocity onto collision normal
227+
var ny2 = dp2 * dy2; // nx, ny is normal velocity
228+
229+
console.log(this.body.sprite.name, 'hit', obj.body.sprite.name, 'at', px, py, 'dx', dx, dy, 'dx2', dx2, dy2);
230+
console.log(this.body.sprite.name, 'x', (this.pos.x + this.xw), obj.body.sprite.name, 'x', (obj.pos.x - obj.xw));
231+
console.log('vx1', vx1, vy1, 'dp1', dp1, 'nx1', nx1, ny1);
232+
console.log('vx2', vx2, vy2, 'dp2', dp2, 'nx2', nx2, ny2);
233233

234234
// We only want to apply collision response forces if the object is travelling into, and not out of, the collision
235-
var b, bx, by, fx, fy;
236235

237-
if (dp < 0)
236+
if (this.body.immovable && obj.body.immovable)
238237
{
239-
fx = tx * this.body.friction;
240-
fy = ty * this.body.friction;
238+
// Split the separation then return, no forces applied as they come to a stand-still
239+
px *= 0.5;
240+
py *= 0.5;
241241

242-
b = 1 + this.body.bounce;
242+
this.pos.add(px, py);
243+
this.oldpos.set(this.pos.x, this.pos.y);
243244

244-
bx = (nx * b);
245-
by = (ny * b);
245+
obj.pos.subtract(px, py);
246+
obj.oldpos.set(obj.pos.x, obj.pos.y);
247+
248+
return;
246249
}
247-
else
250+
else if (!this.body.immovable && !obj.body.immovable)
248251
{
249-
// Moving out of collision, do not apply forces
250-
bx = by = fx = fy = 0;
252+
px *= 0.5;
253+
py *= 0.5;
254+
255+
this.pos.add(px, py);
256+
this.oldpos.set(this.pos.x, this.pos.y);
257+
258+
obj.pos.subtract(px, py);
259+
obj.oldpos.set(obj.pos.x, obj.pos.y);
260+
261+
// x velocity
262+
var nv1 = Math.sqrt((vx2 * vx2 * 1) / 1) * ((vx2 > 0) ? 1 : -1);
263+
var nv2 = Math.sqrt((vx1 * vx1 * 1) / 1) * ((vx1 > 0) ? 1 : -1);
264+
var avg = (nv1 + nv2) * 0.5;
265+
nv1 -= avg;
266+
nv2 -= avg;
267+
268+
this.pos.x += avg + nv1 * this.body.bounce;
269+
obj.pos.x += avg + nv2 * obj.body.bounce;
270+
271+
// y velocity
272+
var nv1 = Math.sqrt((vy2 * vy2 * 1) / 1) * ((vy2 > 0) ? 1 : -1);
273+
var nv2 = Math.sqrt((vy1 * vy1 * 1) / 1) * ((vy1 > 0) ? 1 : -1);
274+
var avg = (nv1 + nv2) * 0.5;
275+
nv1 -= avg;
276+
nv2 -= avg;
277+
278+
this.pos.y += avg + nv1 * this.body.bounce;
279+
obj.pos.y += avg + nv2 * obj.body.bounce;
280+
}
281+
else if (!this.body.immovable)
282+
{
283+
/*
284+
if (dp1 < 0)
285+
{
286+
this.pos.add(px, py);
287+
// px + bounce + friction
288+
this.oldpos.x += px + (nx1 * (1 + this.body.bounce)) + ((vx2 - nx1) * this.body.friction);
289+
this.oldpos.y += py + (ny1 * (1 + this.body.bounce)) + ((vy2 - ny1) * this.body.friction);
290+
}
291+
else
292+
{
293+
// Moving out of collision, do not apply forces
294+
this.pos.add(px, py);
295+
this.oldpos.add(px, py);
296+
}
297+
*/
298+
}
299+
else if (!obj.body.immovable)
300+
{
301+
/*
302+
if (dp2 < 0)
303+
{
304+
obj.pos.add(px, py);
305+
// px + bounce + friction
306+
obj.oldpos.x += px + (nx2 * (1 + obj.body.bounce)) + ((vx2 - nx2) * obj.body.friction);
307+
obj.oldpos.y += py + (ny2 * (1 + obj.body.bounce)) + ((vy2 - ny2) * obj.body.friction);
308+
}
309+
else
310+
{
311+
// Moving out of collision, do not apply forces
312+
obj.pos.add(px, py);
313+
obj.oldpos.add(px, py);
314+
}
315+
*/
251316
}
252317

253-
// working version
254-
// p.x += px;
255-
// p.y += py;
256-
// o.x += px + bx + fx;
257-
// o.y += py + by + fy;
258-
259-
// Project object out of collision (applied to the position value)
260-
p.x += px;
261-
p.y += py;
262-
263-
// obj.pos.x += px;
264-
// obj.pos.y += py;
265-
266-
267-
// Apply bounce+friction impulses which alter velocity (applied to old position, thus setting a sort of velocity up)
268-
var rx = px + bx + fx;
269-
var ry = py + by + fy;
270-
271-
// let's pretend obj is immovable
272-
// rx *= -1;
273-
// ry *= -1;
274-
275-
276-
// Now let's share the load
277-
o.x += rx;
278-
o.y += ry;
279318

280-
// work out objs velocity
281319

320+
/*
321+
if (!body1.immovable && !body2.immovable)
322+
{
323+
this._overlap *= 0.5;
282324
283-
// rx *= -1;
284-
// ry *= -1;
325+
body1.x = body1.x - this._overlap;
326+
body2.x += this._overlap;
285327
286-
// obj.oldpos.x += rx;
287-
// obj.oldpos.y += ry;
328+
this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1);
329+
this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1);
330+
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
331+
this._newVelocity1 -= this._average;
332+
this._newVelocity2 -= this._average;
288333
334+
body1.velocity.x = this._average + this._newVelocity1 * body1.bounce.x;
335+
body2.velocity.x = this._average + this._newVelocity2 * body2.bounce.x;
336+
}
337+
else if (!body1.immovable)
338+
{
339+
body1.x = body1.x - this._overlap;
340+
body1.velocity.x = this._velocity2 - this._velocity1 * body1.bounce.x;
341+
}
342+
else if (!body2.immovable)
343+
{
344+
body2.x += this._overlap;
345+
body2.velocity.x = this._velocity1 - this._velocity2 * body2.bounce.x;
346+
}
289347
290-
// console.log(this.body.sprite.name, 'o.x', rx, ry, obj);
348+
*/
291349

292350
},
293351

src/physics/ninja/World.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ Phaser.Physics.Ninja.prototype = {
176176
* @method Phaser.Physics.Ninja#enableBody
177177
* @param {object} object - The game object to create the physics body on. A body will only be created if this object has a null `body` property.
178178
*/
179-
enableBody: function (object) {
179+
enableBody: function (object, type, id, radius) {
180180

181181
if (object.hasOwnProperty('body') && object.body === null)
182182
{

0 commit comments

Comments
 (0)