Skip to content

Commit ca9321e

Browse files
committed
Updated physics body to use localTransform. Updated tanks demo.
1 parent 83a0086 commit ca9321e

7 files changed

Lines changed: 46 additions & 56 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Version 1.0.7 (in progress in the dev branch)
4141
* Added World.postUpdate - all sprite position changes, as a result of physics, happen here before the render.
4242
* Complete overhaul of Physics.Arcade.Body - now significantly more stable and faster too.
4343
* Updated ArcadePhysics.separateX/Y to use new body system - much better results now.
44-
* QuadTree bug found in 1.0.5 now fixed. The QuadTree is updated properly now using worldTransform values.
44+
* QuadTree bug found in 1.0.5 now fixed. The QuadTree is updated properly now using localTransform values.
4545
* Fixed the Bounce.In and Bounce.InOut tweens (thanks XekeDeath)
4646
* Renamed Phaser.Text.text to Phaser.Text.content to avoid conflict and overwrite from Pixi local var.
4747
* Renamed Phaser.Text.style to Phaser.Text.font to avoid conflict and overwrite from Pixi local var.
905 Bytes
Loading

examples/buttons/rotated buttons.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,41 @@
55

66
<script type="text/javascript">
77

8-
9-
108
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create,update : update });
119

1210
function preload() {
1311

1412
game.load.spritesheet('button', 'assets/buttons/button_sprite_sheet.png', 193, 71);
1513
game.load.image('background','assets/misc/starfield.jpg');
1614

17-
1815
}
19-
var button,
20-
background;
2116

17+
var button;
18+
var background;
2219

2320
function create() {
2421

2522
game.stage.backgroundColor = '#cccccc';
2623

27-
// the numbers given in parameters are the indexes of the frames, in this order :
28-
// over,out,down
29-
button = game.add.button(game.world.centerX, game.world.centerY, 'button', actionOnClick, this, 1, 0, 2);
24+
// The numbers given in parameters are the indexes of the frames, in this order: over, out, down
25+
button = game.add.button(game.world.centerX, game.world.centerY, 'button', actionOnClick, this, 2, 1, 0);
3026

31-
//set the anchor of the sprite in the center, otherwise it would rotate around the top-left corner
27+
// Set the anchor of the sprite in the center, otherwise it would rotate around the top-left corner
3228
button.anchor.setTo(0.5,0.5);
3329

34-
35-
3630
}
3731

3832
function actionOnClick () {
3933

4034
alert("Though I'm turning around, you can still click on me");
4135

42-
4336
}
4437

4538
function update () {
4639

47-
button.angle+=1;
40+
button.angle += 1;
4841
}
4942

50-
51-
5243
</script>
5344

5445
<?php

examples/games/tanks.php

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,30 @@
1010
function preload() {
1111

1212
game.load.atlas('tank', 'assets/games/tanks/tanks.png', 'assets/games/tanks/tanks.json');
13-
game.load.image('bullet', 'assets/sprites/enemy-bullet.png');
13+
game.load.image('bullet', 'assets/games/tanks/bullet.png');
1414
game.load.image('earth', 'assets/games/tanks/scorched_earth.png');
1515

1616
}
1717

18+
var land;
19+
20+
var shadow;
1821
var tank;
1922
var turret;
20-
var shadow;
2123

22-
var currentSpeed = 0;
23-
24-
var land;
24+
var enemy;
2525

26+
var currentSpeed = 0;
2627
var cursors;
27-
var bullets;
2828

29+
var bullets;
2930
var fireRate = 100;
3031
var nextFire = 0;
3132

3233
function create() {
3334

3435
// Resize our game world to be a 2000x2000 square
35-
// game.world.setBounds(-1000, -1000, 2000, 2000);
36-
game.world.setBounds(0, 0, 1000, 1000);
37-
38-
console.log(game.world.bounds.right, 'bot', this.game.world.bounds.bottom);
39-
console.log(game.camera.bounds.right, 'cbot', this.game.camera.bounds.bottom);
36+
game.world.setBounds(-1000, -1000, 2000, 2000);
4037

4138
// Our tiled scrolling background
4239
land = game.add.tileSprite(0, 0, 800, 600, 'earth');
@@ -46,37 +43,46 @@ function create() {
4643
shadow = game.add.sprite(0, 0, 'tank', 'shadow');
4744
shadow.anchor.setTo(0.5, 0.5);
4845

46+
// Our bullet group
47+
bullets = game.add.group();
48+
bullets.createMultiple(50, 'bullet');
49+
bullets.setAll('anchor.x', 0.5);
50+
bullets.setAll('anchor.y', 0.5);
51+
bullets.setAll('outOfBoundsKill', true);
52+
4953
// The base of our tank
5054
tank = game.add.sprite(0, 0, 'tank', 'tank1');
5155
tank.anchor.setTo(0.5, 0.5);
5256
tank.animations.add('move', ['tank1', 'tank2', 'tank3', 'tank4', 'tank5', 'tank6'], 20, true);
53-
tank.play('move');
57+
// tank.play('move');
5458

5559
// This will force it to decelerate and limit its speed
5660
tank.body.drag.setTo(200, 200);
5761
tank.body.maxVelocity.setTo(400, 400);
5862
tank.body.collideWorldBounds = true;
5963

60-
// Our bullet group
61-
bullets = game.add.group();
62-
bullets.createMultiple(50, 'bullet');
63-
bullets.setAll('anchor.x', 0.5);
64-
bullets.setAll('anchor.y', 0.5);
65-
bullets.setAll('outOfBoundsKill', true);
6664

6765
// Finally the turret that we place on-top of the tank body
6866
turret = game.add.sprite(0, 0, 'tank', 'turret');
69-
turret.anchor.setTo(0.5, 0.5);
67+
turret.anchor.setTo(0.3, 0.5);
68+
69+
enemy = game.add.sprite(900, 400, 'tank', 'tank1');
70+
enemy.anchor.setTo(0.5, 0.5);
71+
enemy.body.immovable = true;
72+
enemy.body.collideWorldBounds = true;
7073

7174
game.camera.follow(tank);
72-
// game.camera.deadzone = new Phaser.Rectangle(100, 100, 600, 400);
75+
game.camera.deadzone = new Phaser.Rectangle(100, 100, 600, 400);
76+
game.camera.focusOnXY(0, 0);
7377

7478
cursors = game.input.keyboard.createCursorKeys();
7579

7680
}
7781

7882
function update() {
7983

84+
game.physics.collide(tank, enemy);
85+
8086
if (cursors.left.isDown)
8187
{
8288
tank.angle -= 4;
@@ -139,7 +145,7 @@ function fire() {
139145

140146
bullet.reset(turret.x, turret.y);
141147

142-
game.physics.moveToPointer(bullet, 1000);
148+
bullet.rotation = game.physics.moveToPointer(bullet, 1000);
143149
}
144150

145151
}
@@ -151,15 +157,10 @@ function render() {
151157
// game.debug.renderText('sr: ' + tank.body.right, 32, 100);
152158
// game.debug.renderText('sb: ' + tank.body.bottom, 32, 132);
153159

154-
game.debug.renderSpriteCorners(tank, true, true);
160+
// game.debug.renderSpriteCorners(tank, true, true);
155161

156162
game.debug.renderCameraInfo(game.camera, 500, 32);
157-
158-
game.debug.renderLocalTransformInfo(tank, 32, 32);
159-
game.debug.renderWorldTransformInfo(tank, 32, 200);
160-
161-
162-
// game.debug.renderSpriteInfo(sprite, 32, 450);
163+
game.debug.renderSpriteInfo(tank, 32, 450);
163164

164165
}
165166

src/physics/arcade/ArcadePhysics.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,8 +1286,8 @@ Phaser.Physics.Arcade.prototype = {
12861286

12871287
pointer = pointer || this.game.input.activePointer;
12881288

1289-
this._dx = displayObject.x - pointer.x;
1290-
this._dy = displayObject.y - pointer.y;
1289+
this._dx = displayObject.worldX - pointer.x;
1290+
this._dy = displayObject.worldY - pointer.y;
12911291

12921292
return Math.sqrt(this._dx * this._dx + this._dy * this._dy);
12931293

@@ -1340,8 +1340,8 @@ Phaser.Physics.Arcade.prototype = {
13401340

13411341
pointer = pointer || this.game.input.activePointer;
13421342

1343-
this._dx = pointer.x - displayObject.x;
1344-
this._dy = pointer.y - displayObject.y;
1343+
this._dx = pointer.worldX - displayObject.x;
1344+
this._dy = pointer.worldY - displayObject.y;
13451345

13461346
return Math.atan2(this._dy, this._dx);
13471347

src/physics/arcade/Body.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ Phaser.Physics.Arcade.Body.prototype = {
105105

106106
this.embedded = false;
107107

108-
this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
109-
this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
108+
this.preX = (this.sprite.localTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
109+
this.preY = (this.sprite.localTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
110110
this.preRotation = this.sprite.angle;
111111

112112
this.x = this.preX;
@@ -221,12 +221,12 @@ Phaser.Physics.Arcade.Body.prototype = {
221221
this.angularVelocity = 0;
222222
this.angularAcceleration = 0;
223223

224-
this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
225-
this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
224+
this.preX = (this.sprite.localTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
225+
this.preY = (this.sprite.localTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
226226
this.preRotation = this.sprite.angle;
227227

228-
this.x = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
229-
this.y = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
228+
this.x = (this.sprite.localTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
229+
this.y = (this.sprite.localTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
230230
this.rotation = this.sprite.angle;
231231

232232
},

src/utils/Debug.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,6 @@ Phaser.Utils.Debug.prototype = {
539539
this.line('scaleY: ' + sprite.localTransform[4]);
540540
this.line('transX: ' + sprite.localTransform[2]);
541541
this.line('transY: ' + sprite.localTransform[5]);
542-
this.line('sX: ' + sprite._sx);
543-
this.line('sY: ' + sprite._sy);
544542

545543
},
546544

0 commit comments

Comments
 (0)