Edge points in and working.

This commit is contained in:
Richard Davey 2013-09-01 03:15:27 +01:00
parent b742439db8
commit 188d6239a3
3 changed files with 137 additions and 12 deletions

View file

@ -69,11 +69,7 @@
br = points[2];
bl = points[3];
s.anchor.setTo(0, 0);
s.angle = 5;
// get the distance between top-left and bottom-right
// distance = Phaser.Math.distance(0,0,s.width,s.height);
s.anchor.setTo(2, 0.5);
// PIXI worldTransform order:
@ -94,10 +90,10 @@
s.angle += 0.5;
if (s.scale.x < 2)
if (s.scale.x > -2)
{
s.scale.x += 0.01;
s.scale.y += 0.01;
s.scale.x -= 0.01;
s.scale.y -= 0.01;
}
}
@ -108,17 +104,23 @@
// var p1 = getLocalPosition(midpoint.x, midpoint.y, s);
var offsetX = s.anchor.x * s.width;
var offsetY = s.anchor.y * s.height;
var sx = s.x - offsetX;
var sy = s.y - offsetY;
// top left
var p1 = getLocalPosition(s.x, s.y, s);
var p1 = getLocalPosition(sx, sy, s);
// top right
var p2 = getLocalPosition(s.x + s.width, s.y, s);
var p2 = getLocalPosition(sx + s.width, sy, s);
// bottom left
var p3 = getLocalPosition(s.x, s.y + s.height, s);
var p3 = getLocalPosition(sx, sy + s.height, s);
// bottom right
var p4 = getLocalPosition(s.x + s.width, s.y + s.height, s);
var p4 = getLocalPosition(sx + s.width, sy + s.height, s);
p1.add(s.x, s.y);
p2.add(s.x, s.y);
@ -139,6 +141,8 @@
game.debug.renderText('ty: ' + tr.y, 32, 265);
game.debug.renderText('px: ' + p2.x, 32, 280);
game.debug.renderText('py: ' + p2.y, 32, 295);
game.debug.renderText('ox: ' + offsetX, 32, 350);
game.debug.renderText('oy: ' + offsetY, 32, 370);
}

55
examples/camera3.php Normal file
View file

@ -0,0 +1,55 @@
<!DOCTYPE HTML>
<html>
<head>
<title>phaser.js - a new beginning</title>
<?php
require('js.php');
?>
</head>
<body>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('mushroom', 'assets/sprites/mana_card.png');
}
function create() {
s = game.add.sprite(game.world.centerX, game.world.centerY, 'mushroom');
s.anchor.setTo(0.5, 0.5);
}
function update() {
s.angle += 0.5;
if (s.scale.x > -2)
{
s.scale.x -= 0.01;
s.scale.y -= 0.01;
}
}
function render() {
game.debug.renderPoint(s.topLeft, 'rgb(255,0,0)');
game.debug.renderPoint(s.topRight, 'rgb(0,255,0)');
game.debug.renderPoint(s.bottomLeft, 'rgb(0,0,255)');
game.debug.renderPoint(s.bottomRight, 'rgb(255,0,255)');
}
})();
</script>
</body>
</html>

View file

@ -106,6 +106,23 @@ Phaser.Sprite = function (game, x, y, key, frame) {
this.worldView = new Phaser.Rectangle(x, y, this.width, this.height);
// Edge points
this.topLeft = new Phaser.Point(x, y);
this.topRight = new Phaser.Point(x + this.width, y);
this.bottomRight = new Phaser.Point(x + this.width, y + this.height);
this.bottomLeft = new Phaser.Point(x, y + this.height);
this.offset = new Phaser.Point();
// help avoid gc spikes by using temp. vars
this._a00 = 0;
this._a01 = 0;
this._a02 = 0;
this._a10 = 0;
this._a11 = 0;
this._a12 = 0;
this._id = 0;
};
Phaser.Sprite.prototype = Object.create(PIXI.Sprite.prototype);
@ -123,10 +140,59 @@ Phaser.Sprite.prototype.update = function() {
this.position.x = this._x - (this.game.world.camera.x * this.scrollFactor.x);
this.position.y = this._y - (this.game.world.camera.y * this.scrollFactor.y);
// Update the edge points (sx, sy)
this.offset.setTo(this.x - (this.anchor.x * this.width), this.y - (this.anchor.y * this.height));
// var sx = s.x - offsetX;
// var sy = s.y - offsetY;
// top left
this.getLocalPosition(this.topLeft, this.offset.x, this.offset.y);
// var p1 = getLocalPosition(sx, sy, s);
// top right
this.getLocalPosition(this.topRight, this.offset.x + this.width, this.offset.y);
// var p2 = getLocalPosition(sx + s.width, sy, s);
// bottom left
this.getLocalPosition(this.bottomLeft, this.offset.x, this.offset.y + this.height);
// var p3 = getLocalPosition(sx, sy + s.height, s);
// bottom right
this.getLocalPosition(this.bottomRight, this.offset.x + this.width, this.offset.y + this.height);
// var p4 = getLocalPosition(sx + s.width, sy + s.height, s);
// p1.add(s.x, s.y);
// p2.add(s.x, s.y);
// p3.add(s.x, s.y);
// p4.add(s.x, s.y);
// this.checkBounds();
}
Phaser.Sprite.prototype.getLocalPosition = function(p, x, y) {
this._a00 = this.worldTransform[0]; // scaleX
this._a01 = this.worldTransform[1]; // skewY
this._a02 = this.worldTransform[2]; // translateX
this._a10 = this.worldTransform[3]; // skewX
this._a11 = this.worldTransform[4]; // scaleY
this._a12 = this.worldTransform[5]; // translateY
this._a01 *= -1;
this._a10 *= -1;
this._id = 1 / (this._a00 * this._a11 + this._a01 * -this._a10);
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;
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;
p.add(this.x, this.y);
return p;
}
Object.defineProperty(Phaser.Sprite.prototype, 'angle', {
get: function() {