Skip to content

Commit 99e0934

Browse files
committed
More late-night math.
1 parent 5d4da0c commit 99e0934

3 files changed

Lines changed: 91 additions & 69 deletions

File tree

examples/camera2.php

Lines changed: 87 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -23,117 +23,137 @@ function preload() {
2323
var r;
2424
var p;
2525

26+
var tl;
27+
var tr;
28+
var bl;
29+
var br;
30+
2631
var half = { width: 0, height: 0 };
2732
var angle = 0;
2833
var distance = 0;
2934

35+
/**
36+
* Returns an array containing 4 Point objects corresponding to the 4 corners of the sprite bounds.
37+
* @method getAsPoints
38+
* @param {Sprite} sprite The Sprite that will have its cameraView property modified
39+
* @return {Array} An array of Point objects.
40+
*/
41+
function getAsPoints(sprite) {
42+
var out = [];
43+
// top left
44+
out.push(new Phaser.Point(sprite.x, sprite.y));
45+
// top right
46+
out.push(new Phaser.Point(sprite.x + sprite.width, sprite.y));
47+
// bottom right
48+
out.push(new Phaser.Point(sprite.x + sprite.width, sprite.y + sprite.height));
49+
// bottom left
50+
out.push(new Phaser.Point(sprite.x, sprite.y + sprite.height));
51+
52+
return out;
53+
}
54+
55+
var midpoint;
56+
3057
function create() {
3158

3259
// Make our game world 2000x2000 pixels in size (the default is to match the game size)
3360
game.world.setSize(2000, 2000);
3461

3562
s = game.add.sprite(400, 300, 'mushroom');
36-
console.log(s.width, s.height);
3763

38-
// get the distance between top-left and bottom-right
39-
distance = Phaser.Math.distance(0,0,s.width,s.height);
64+
midpoint = new Phaser.Point(s.x + s.width / 2, s.y + s.height / 2);
4065

41-
// draw line from x/y
42-
s.anchor.setTo(1, 0);
66+
var points = getAsPoints(s);
67+
tl = points[0];
68+
tr = points[1];
69+
br = points[2];
70+
bl = points[3];
4371

44-
// c = new Phaser.Circle(s.x, s.y, distance);
72+
s.anchor.setTo(0, 0);
73+
s.angle = 5;
4574

46-
p = new Phaser.Point();
75+
// get the distance between top-left and bottom-right
76+
// distance = Phaser.Math.distance(0,0,s.width,s.height);
4777

4878
// PIXI worldTransform order:
4979

50-
// 0 = scaleX
51-
// 1 = skewY
80+
// |a c tx|
81+
// |b d ty|
82+
// |0 0 1|
83+
84+
// 0 = scaleX (a)
85+
// 1 = skewY (c)
5286
// 2 = translateX
53-
// 3 = skewX
54-
// 4 = scaleY
87+
// 3 = skewX (b)
88+
// 4 = scaleY (d)
5589
// 5 = translateY
5690

5791
}
5892

59-
function getMidPoint(x, y, width, height, angle_degrees) {
60-
var angle_rad = angle_degrees * Math.PI / 180;
61-
var cosa = Math.cos(angle_rad);
62-
var sina = Math.sin(angle_rad);
63-
var wp = width/2;
64-
var hp = height/2;
65-
return { px: ( x + wp * cosa - hp * sina ),
66-
py: ( y + wp * sina + hp * cosa ) };
67-
}
68-
69-
7093
function update() {
7194

72-
73-
s.rotation += 0.01;
74-
// var newWidth = (s.width * Math.cos(s.rotation)) + (s.height * Math.sin(s.rotation));
75-
// var newHeight = (s.width * Math.sin(s.rotation)) + (s.height * Math.cos(s.rotation));
76-
// r.x = cx(s) - newWidth / 2;
77-
// r.y = cy(s);
78-
// r.width = newWidth;
79-
// r.height = newHeight;
80-
95+
s.angle += 0.5;
8196
}
8297

8398
function render() {
8499

85-
// var tt = getMidPoint(s.x,s.y,s.width,s.height,s.angle);
86-
// p.setTo(tt.px,tt.py);
87-
88-
var originAngle = Math.atan2(centerY - originY, centerX - originX);
100+
game.debug.renderSpriteInfo(s, 32, 32);
89101

90-
// game.debug.renderCameraInfo(game.camera, 32, 32);
91-
// game.debug.renderInputInfo(32, 100);
92-
// game.debug.renderSpriteInfo(s, 32, 32);
93-
// game.debug.renderSpriteBounds(s);
102+
// var p1 = getLocalPosition(midpoint.x, midpoint.y, s);
94103

95-
//p.rotate(s.x, s.y, s.angle, asDegrees, distance) {
104+
var p1 = getLocalPosition(tl.x, tl.y, s);
105+
var p2 = getLocalPosition(tr.x, tr.y, s);
106+
var p3 = getLocalPosition(bl.x, bl.y, s);
107+
var p4 = getLocalPosition(br.x, br.y, s);
96108

97-
game.debug.renderPoint(p);
109+
// p1.add(100, 100);
110+
// p2.add(100, 100);
111+
// p3.add(100, 100);
112+
// p4.add(100, 100);
113+
// p1.add(s.x + (s.anchor.x * (s.width / 2)), s.y + (s.anchor.y * (s.height / 2)));
114+
p1.add(s.x, s.y);
115+
p2.add(s.x, s.y);
116+
p3.add(s.x, s.y);
117+
p4.add(s.x, s.y);
98118

99-
// game.debug.renderRectangle(r);
100-
// game.debug.renderCircle(c);
119+
// game.debug.renderPoint(tl, 'rgb(255,0,0)');
120+
// game.debug.renderPoint(tr, 'rgb(0,255,0)');
121+
// game.debug.renderPoint(bl, 'rgb(0,0,255)');
122+
// game.debug.renderPoint(br, 'rgb(255,0,255)');
101123

102-
// var p = getLocalPosition(game.input.x, game.input.y, s);
124+
game.debug.renderPoint(p1, 'rgb(255,0,0)');
125+
game.debug.renderPoint(p2, 'rgb(0,255,0)');
126+
game.debug.renderPoint(p3, 'rgb(0,0,255)');
127+
game.debug.renderPoint(p4, 'rgb(255,0,255)');
103128

104-
// game.debug.renderPoint(p, 'rgb(255,0,255)');
105-
// game.debug.renderPixel(game.input.x, game.input.y);
129+
game.debug.renderText('tx: ' + tr.x, 32, 250);
130+
game.debug.renderText('ty: ' + tr.y, 32, 265);
131+
game.debug.renderText('px: ' + p2.x, 32, 280);
132+
game.debug.renderText('py: ' + p2.y, 32, 295);
106133

107134
}
108135

109-
function transformBox ( m, t, a ) {
136+
function getLocalPosition (x, y, displayObject) {
137+
138+
var a00 = displayObject.worldTransform[0]; // scaleX
139+
var a01 = displayObject.worldTransform[1]; // skewY
140+
var a02 = displayObject.worldTransform[2]; // translateX
141+
var a10 = displayObject.worldTransform[3]; // skewX
142+
var a11 = displayObject.worldTransform[4]; // scaleY
143+
var a12 = displayObject.worldTransform[5]; // translateY
110144

111-
// m = 3x3 matrix (may need to check entries)
112-
// t = translation matrix
113-
// a = rect
145+
a01 *= -1;
146+
a10 *= -1;
114147

148+
var id = 1 / (a00 * a11 + a01 * -a10);
115149

150+
var dx = a11 * id * x + -a01 * id * y + (a12 * a01 - a02 * a11) * id;
151+
var dy = a00 * id * y + -a10 * id * x + (-a12 * a00 + a02 * a10) * id;
116152

117-
}
153+
return new Phaser.Point(dx, dy);
118154

119-
function getLocalPosition (x, y, displayObject)
120-
{
121-
// Maps the point over the DO to local un-rotated, un-scaled space
122-
// So you can then compare this point against a hitArea that was defined for the sprite to get detection
123-
124-
var worldTransform = displayObject.worldTransform;
125-
var global = { x: x, y: y };
126-
127-
// do a cheeky transform to get the mouse coords;
128-
var a00 = worldTransform[0], a01 = worldTransform[1], a02 = worldTransform[2],
129-
a10 = worldTransform[3], a11 = worldTransform[4], a12 = worldTransform[5],
130-
id = 1 / (a00 * a11 + a01 * -a10);
131-
// set the mouse coords...
132-
return new PIXI.Point(a11 * id * global.x + -a01 * id * global.y + (a12 * a01 - a02 * a11) * id,
133-
a00 * id * global.y + -a10 * id * global.x + (-a12 * a00 + a02 * a10) * id)
134155
}
135156

136-
137157
})();
138158

139159
</script>

src/geom/Point.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ Phaser.Point.prototype = {
4848
},
4949

5050
/**
51-
* Sets the x and y values of this MicroPoint object to the given coordinates.
51+
* Sets the x and y values of this Point object to the given coordinates.
5252
* @method setTo
5353
* @param {Number} x - The horizontal position of this point.
5454
* @param {Number} y - The vertical position of this point.
55-
* @return {MicroPoint} This MicroPoint object. Useful for chaining method calls.
55+
* @return {Point} This Point object. Useful for chaining method calls.
5656
**/
5757
setTo: function (x, y) {
5858
this.x = x;

src/utils/Debug.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,11 @@ Phaser.Utils.Debug.prototype = {
397397
if (typeof color === "undefined") { color = 'rgb(255,255,255)'; }
398398
if (typeof font === "undefined") { font = '16px Courier'; }
399399

400+
this.start();
400401
this.context.font = font;
401402
this.context.fillStyle = color;
402403
this.context.fillText(text, x, y);
404+
this.stop();
403405

404406
}
405407

0 commit comments

Comments
 (0)