Skip to content

Commit 90c0937

Browse files
committed
Velocity integration tidied up. Now moving to sync Body with Sprite center point.
1 parent fbe508a commit 90c0937

10 files changed

Lines changed: 291 additions & 316 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Significant API changes:
6969
* Body.collideCallback allows you to set a callback that is fired whenever the Body is hit on any of its active faces.
7070
* Body.allowCollision has been renamed to Body.checkCollision.
7171
* Body.rebound is a boolean that controls if a body will exchange velocity on collision. Set to false to allow it to be 'pushed' (see new examples).
72+
* Removed Body.deltaAbsX and deltaAbsY as they are no longer used internally.
73+
* Body.screenX and screenY moved to getters, no longer calculated every frame.
7274

7375

7476
New features:

examples/collision/sprite vs sprite.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
33

44
function preload() {
55

@@ -40,3 +40,12 @@ function collisionHandler (obj1, obj2) {
4040
game.stage.backgroundColor = '#992d2d';
4141

4242
}
43+
44+
function render() {
45+
46+
// game.debug.renderBodyInfo(sprite1, 16, 16);
47+
48+
game.debug.renderPolygon(sprite1.body.polygons);
49+
game.debug.renderPolygon(sprite2.body.polygons);
50+
51+
}

examples/physics/mass velocity test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ function create() {
1515

1616
aliens = game.add.group();
1717

18+
game.physics.gravity.y = 100;
19+
1820
for (var i = 0; i < 50; i++)
1921
{
2022
var s = aliens.create(game.world.randomX, game.world.randomY, 'baddie');
@@ -27,6 +29,7 @@ function create() {
2729
}
2830

2931
car = game.add.sprite(400, 300, 'car');
32+
car.name = 'car';
3033
car.anchor.setTo(0.5, 0.5);
3134
car.body.collideWorldBounds = true;
3235
// car.body.bounce.setTo(0.8, 0.8);
@@ -62,7 +65,17 @@ function update() {
6265

6366
function render() {
6467

68+
for (var i = 0; i < aliens._container.children.length; i++)
69+
{
70+
game.debug.renderPolygon(aliens._container.children[i].body.polygons);
71+
}
72+
73+
game.debug.renderPolygon(car.body.polygons);
74+
75+
// game.debug.renderBodyInfo(aliens._container.children[0], 32, 32);
76+
game.debug.renderBodyInfo(aliens._container.children[0], 32, 32);
77+
6578
// game.debug.renderBodyInfo(car, 16, 24);
66-
game.debug.renderBodyInfo(aliens.getFirstAlive(), 16, 24);
79+
// game.debug.renderBodyInfo(aliens.getFirstAlive(), 16, 24);
6780

6881
}

examples/wip/platform.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,8 @@ function create() {
6767

6868
function launch() {
6969

70-
game.time._x = true;
71-
72-
// sprite.body.velocity.x = -200;
73-
// sprite.body.velocity.y = -200;
70+
sprite.body.velocity.x = -200;
71+
sprite.body.velocity.y = -200;
7472

7573
}
7674

examples/wip/tween-relative.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,19 @@ function create() {
2929

3030
function move() {
3131

32+
console.log('moving');
33+
3234
if (sprite.x === 100)
3335
{
3436
// Here you'll notice we are using a relative value for the tween.
3537
// You can specify a number as a string with either + or - at the start of it.
3638
// When the tween starts it will take the sprites current X value and add +300 to it.
3739

38-
game.add.tween(sprite).to( { x: '+300' }, 2000, Phaser.Easing.Linear.None, true);
40+
// game.add.tween(sprite).to( { x: '+300' }, 2000, Phaser.Easing.Linear.None, true);
3941
}
4042
else if (sprite.x === 400)
4143
{
42-
game.add.tween(sprite).to( { x: '-300' }, 2000, Phaser.Easing.Linear.None, true);
44+
// game.add.tween(sprite).to( { x: '-300' }, 2000, Phaser.Easing.Linear.None, true);
4345
}
4446

4547
}
@@ -48,10 +50,17 @@ function render() {
4850

4951
if (sprite.x === 100 || sprite.x === 400)
5052
{
51-
game.debug.renderText('Click sprite to tween', 32, 32);
53+
// game.debug.renderText('Click sprite to tween', 32, 32);
5254
}
5355

5456
game.debug.renderText('x: ' + arrowStart.x, arrowStart.x, arrowStart.y - 4);
5557
game.debug.renderText('x: ' + arrowEnd.x, arrowEnd.x, arrowEnd.y - 4);
5658

59+
game.debug.renderText('sprite.x: ' + sprite.x + ' deltaX: ' + sprite.deltaX, 32, 32);
60+
game.debug.renderText('sprite.y: ' + sprite.y + ' deltaY: ' + sprite.deltaY, 32, 48);
61+
62+
game.debug.renderPolygon(sprite.body.polygons);
63+
64+
game.debug.renderPoint(sprite.center);
65+
5766
}

src/core/World.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ Phaser.World.prototype.preUpdate = function () {
7777
if (currentNode['preUpdate'] && !currentNode.preUpdate())
7878
{
7979
currentNode = currentNode.last._iNext;
80-
} else {
80+
}
81+
else
82+
{
8183
currentNode = currentNode._iNext;
8284
}
8385

@@ -106,7 +108,9 @@ Phaser.World.prototype.update = function () {
106108
if (currentNode['update'] && !currentNode.update())
107109
{
108110
currentNode = currentNode.last._iNext;
109-
} else {
111+
}
112+
else
113+
{
110114
currentNode = currentNode._iNext;
111115
}
112116

src/gameobjects/Sprite.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,6 @@ Phaser.Sprite.prototype.updateAnimation = function() {
507507
this._cache.halfHeight = Math.floor(this._cache.height / 2);
508508

509509
this._cache.dirty = true;
510-
511510
}
512511

513512
};
@@ -993,6 +992,34 @@ Phaser.Sprite.prototype.play = function (name, frameRate, loop, killOnComplete)
993992

994993
};
995994

995+
/**
996+
* Returns the delta x value. The difference between Sprite.x now and in the previous step.
997+
* @name Phaser.Sprite#deltaX
998+
* @property {number} deltaX - The delta value. Positive if the motion was to the right, negative if to the left.
999+
* @readonly
1000+
*/
1001+
Object.defineProperty(Phaser.Sprite.prototype, 'deltaX', {
1002+
1003+
get: function() {
1004+
return this.world.x - this._cache.prevX;
1005+
}
1006+
1007+
});
1008+
1009+
/**
1010+
* Returns the delta x value. The difference between Sprite.y now and in the previous step.
1011+
* @name Phaser.Sprite#deltaY
1012+
* @property {number} deltaY - The delta value. Positive if the motion was downwards, negative if upwards.
1013+
* @readonly
1014+
*/
1015+
Object.defineProperty(Phaser.Sprite.prototype, 'deltaY', {
1016+
1017+
get: function() {
1018+
return this.world.y - this._cache.prevY;
1019+
}
1020+
1021+
});
1022+
9961023
/**
9971024
* Indicates the rotation of the Sprite, in degrees, from its original orientation. Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation.
9981025
* Values outside this range are added to or subtracted from 360 to obtain a value within the range. For example, the statement player.angle = 450 is the same as player.angle = 90.

src/physics/arcade/ArcadePhysics.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ Phaser.Physics.Arcade = function (game) {
2929
*/
3030
this.gravity = new Phaser.Point();
3131

32-
/**
33-
* @property {Phaser.Rectangle} bounds - The bounds inside of which the physics world exists. Defaults to match the world bounds.
34-
*/
35-
// this.bounds = new Phaser.Rectangle(0, 0, game.world.width, game.world.height);
36-
3732
/**
3833
* @property {Phaser.QuadTree} quadTree - The world QuadTree.
3934
*/
@@ -91,6 +86,12 @@ Phaser.Physics.Arcade = function (game) {
9186
*/
9287
this._dy = 0;
9388

89+
/**
90+
* @property {Phaser.Point} _p - Internal cache var.
91+
* @private
92+
*/
93+
this._p = new Phaser.Point(0, 0);
94+
9495
/**
9596
* @property {number} _gravityX - Internal cache var.
9697
* @private
@@ -123,6 +124,12 @@ Phaser.Physics.Arcade.RECT = 0;
123124
*/
124125
Phaser.Physics.Arcade.CIRCLE = 1;
125126

127+
/**
128+
* @constant
129+
* @type {number}
130+
*/
131+
Phaser.Physics.Arcade.POLYGON = 2;
132+
126133
Phaser.Physics.Arcade.prototype = {
127134

128135
/**
@@ -194,8 +201,9 @@ Phaser.Physics.Arcade.prototype = {
194201
// pos = pos + dt*(vel + temp/2)
195202
// vel = vel + temp
196203

197-
body.motionVelocity.x = (body.acceleration.x + this._gravityX) * this.game.time.physicsElapsed;
198-
body.motionVelocity.y = (body.acceleration.y + this._gravityY) * this.game.time.physicsElapsed;
204+
this._p.setTo((body.acceleration.x + this._gravityX) * this.game.time.physicsElapsed, (body.acceleration.y + this._gravityY) * this.game.time.physicsElapsed);
205+
206+
return this._p;
199207

200208
},
201209

0 commit comments

Comments
 (0)