Skip to content

Commit 188d623

Browse files
committed
Edge points in and working.
1 parent b742439 commit 188d623

3 files changed

Lines changed: 137 additions & 12 deletions

File tree

examples/camera2.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ function create() {
6969
br = points[2];
7070
bl = points[3];
7171

72-
s.anchor.setTo(0, 0);
73-
s.angle = 5;
74-
75-
// get the distance between top-left and bottom-right
76-
// distance = Phaser.Math.distance(0,0,s.width,s.height);
72+
s.anchor.setTo(2, 0.5);
7773

7874
// PIXI worldTransform order:
7975

@@ -94,10 +90,10 @@ function update() {
9490

9591
s.angle += 0.5;
9692

97-
if (s.scale.x < 2)
93+
if (s.scale.x > -2)
9894
{
99-
s.scale.x += 0.01;
100-
s.scale.y += 0.01;
95+
s.scale.x -= 0.01;
96+
s.scale.y -= 0.01;
10197
}
10298

10399
}
@@ -108,17 +104,23 @@ function render() {
108104

109105
// var p1 = getLocalPosition(midpoint.x, midpoint.y, s);
110106

107+
var offsetX = s.anchor.x * s.width;
108+
var offsetY = s.anchor.y * s.height;
109+
110+
var sx = s.x - offsetX;
111+
var sy = s.y - offsetY;
112+
111113
// top left
112-
var p1 = getLocalPosition(s.x, s.y, s);
114+
var p1 = getLocalPosition(sx, sy, s);
113115

114116
// top right
115-
var p2 = getLocalPosition(s.x + s.width, s.y, s);
117+
var p2 = getLocalPosition(sx + s.width, sy, s);
116118

117119
// bottom left
118-
var p3 = getLocalPosition(s.x, s.y + s.height, s);
120+
var p3 = getLocalPosition(sx, sy + s.height, s);
119121

120122
// bottom right
121-
var p4 = getLocalPosition(s.x + s.width, s.y + s.height, s);
123+
var p4 = getLocalPosition(sx + s.width, sy + s.height, s);
122124

123125
p1.add(s.x, s.y);
124126
p2.add(s.x, s.y);
@@ -139,6 +141,8 @@ function render() {
139141
game.debug.renderText('ty: ' + tr.y, 32, 265);
140142
game.debug.renderText('px: ' + p2.x, 32, 280);
141143
game.debug.renderText('py: ' + p2.y, 32, 295);
144+
game.debug.renderText('ox: ' + offsetX, 32, 350);
145+
game.debug.renderText('oy: ' + offsetY, 32, 370);
142146

143147
}
144148

examples/camera3.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
(function () {
14+
15+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
16+
17+
function preload() {
18+
game.load.image('mushroom', 'assets/sprites/mana_card.png');
19+
}
20+
21+
function create() {
22+
23+
s = game.add.sprite(game.world.centerX, game.world.centerY, 'mushroom');
24+
25+
s.anchor.setTo(0.5, 0.5);
26+
27+
}
28+
29+
function update() {
30+
31+
s.angle += 0.5;
32+
33+
if (s.scale.x > -2)
34+
{
35+
s.scale.x -= 0.01;
36+
s.scale.y -= 0.01;
37+
}
38+
39+
}
40+
41+
function render() {
42+
43+
game.debug.renderPoint(s.topLeft, 'rgb(255,0,0)');
44+
game.debug.renderPoint(s.topRight, 'rgb(0,255,0)');
45+
game.debug.renderPoint(s.bottomLeft, 'rgb(0,0,255)');
46+
game.debug.renderPoint(s.bottomRight, 'rgb(255,0,255)');
47+
48+
}
49+
50+
})();
51+
52+
</script>
53+
54+
</body>
55+
</html>

src/gameobjects/Sprite.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,23 @@ Phaser.Sprite = function (game, x, y, key, frame) {
106106

107107
this.worldView = new Phaser.Rectangle(x, y, this.width, this.height);
108108

109+
// Edge points
110+
this.topLeft = new Phaser.Point(x, y);
111+
this.topRight = new Phaser.Point(x + this.width, y);
112+
this.bottomRight = new Phaser.Point(x + this.width, y + this.height);
113+
this.bottomLeft = new Phaser.Point(x, y + this.height);
114+
115+
this.offset = new Phaser.Point();
116+
117+
// help avoid gc spikes by using temp. vars
118+
this._a00 = 0;
119+
this._a01 = 0;
120+
this._a02 = 0;
121+
this._a10 = 0;
122+
this._a11 = 0;
123+
this._a12 = 0;
124+
this._id = 0;
125+
109126
};
110127

111128
Phaser.Sprite.prototype = Object.create(PIXI.Sprite.prototype);
@@ -123,10 +140,59 @@ Phaser.Sprite.prototype.update = function() {
123140
this.position.x = this._x - (this.game.world.camera.x * this.scrollFactor.x);
124141
this.position.y = this._y - (this.game.world.camera.y * this.scrollFactor.y);
125142

143+
// Update the edge points (sx, sy)
144+
this.offset.setTo(this.x - (this.anchor.x * this.width), this.y - (this.anchor.y * this.height));
145+
146+
// var sx = s.x - offsetX;
147+
// var sy = s.y - offsetY;
148+
149+
// top left
150+
this.getLocalPosition(this.topLeft, this.offset.x, this.offset.y);
151+
// var p1 = getLocalPosition(sx, sy, s);
152+
153+
// top right
154+
this.getLocalPosition(this.topRight, this.offset.x + this.width, this.offset.y);
155+
// var p2 = getLocalPosition(sx + s.width, sy, s);
156+
157+
// bottom left
158+
this.getLocalPosition(this.bottomLeft, this.offset.x, this.offset.y + this.height);
159+
// var p3 = getLocalPosition(sx, sy + s.height, s);
160+
161+
// bottom right
162+
this.getLocalPosition(this.bottomRight, this.offset.x + this.width, this.offset.y + this.height);
163+
// var p4 = getLocalPosition(sx + s.width, sy + s.height, s);
164+
165+
// p1.add(s.x, s.y);
166+
// p2.add(s.x, s.y);
167+
// p3.add(s.x, s.y);
168+
// p4.add(s.x, s.y);
169+
126170
// this.checkBounds();
127171

128172
}
129173

174+
Phaser.Sprite.prototype.getLocalPosition = function(p, x, y) {
175+
176+
this._a00 = this.worldTransform[0]; // scaleX
177+
this._a01 = this.worldTransform[1]; // skewY
178+
this._a02 = this.worldTransform[2]; // translateX
179+
this._a10 = this.worldTransform[3]; // skewX
180+
this._a11 = this.worldTransform[4]; // scaleY
181+
this._a12 = this.worldTransform[5]; // translateY
182+
183+
this._a01 *= -1;
184+
this._a10 *= -1;
185+
186+
this._id = 1 / (this._a00 * this._a11 + this._a01 * -this._a10);
187+
188+
p.x = (this._a11 * this._id * x + -this._a01 * this._id * y + (this._a12 * this._a01 - this._a02 * this._a11) * this._id) * this.scale.x;
189+
p.y = (this._a00 * this._id * y + -this._a10 * this._id * x + (-this._a12 * this._a00 + this._a02 * this._a10) * this._id) * this.scale.y;
190+
p.add(this.x, this.y);
191+
192+
return p;
193+
194+
}
195+
130196
Object.defineProperty(Phaser.Sprite.prototype, 'angle', {
131197

132198
get: function() {

0 commit comments

Comments
 (0)