Added Sprite.fixedToCamera, fixed Angular Velocity and Acceleration, fixed jittery Camera, added skipQuadTree flag and created lots more examples.

This commit is contained in:
photonstorm
2013-10-08 00:58:20 +01:00
parent 1bc6ac25fa
commit c307f79102
20 changed files with 444 additions and 61 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 161 KiB

+8
View File
@@ -90,12 +90,20 @@ Version 1.0.7 (in progress in the dev branch)
* Made Sprite.body optional and added in checks, so you can safely null the Sprite body object if using your own physics system and not impact rendering.
* Fixed typo in StageScaleMode so it's not pageAlignVeritcally any longer, but pageAlignVertically.
* Fixed issue in Group.countLiving / countDead where the value was off by one (thanks mjablonski)
* Fixed issue with a jittery Camera if you moved a Sprite via velocity instead of x/y placement.
* Added Keyboard.createCursorKeys() which creates an object with 4 Key objects inside it mapped to up, down, left and right. See the new example in the input folder.
* Added Body.skipQuadTree boolean for more fine-grained control over when a body is added to the World QuadTree.
* Re-implemented Angular Velocity and Angular Acceleration on the Sprite.body and created 2 new examples to show use.
* Moved the Camera update checks to World.postUpdate, so all the sprites get the correct adjusted camera position.
* Added Sprite.fixedToCamera boolean. A Sprite that is fixed to the camera doesn't move with the world, but has its x/y coordinates relative to the top-left of the camera.
* TODO: look at Sprite.crop (http://www.html5gamedevs.com/topic/1617-error-in-spritecrop/)
* TODO: d-pad example (http://www.html5gamedevs.com/topic/1574-gameinputondown-question/)
* TODO: more touch input examples (http://www.html5gamedevs.com/topic/1556-mobile-touch-event/)
* TODO: addMarker hh:mm:ss:ms
* TODO: swap state (non-destructive shift)
* TODO: rotation offset
Version 1.0.6 (September 24th 2013)
Binary file not shown.

After

Width:  |  Height:  |  Size: 558 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB

+12 -2
View File
@@ -8,7 +8,7 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update });
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
var baddie,
keys=Phaser.Keyboard;
@@ -28,7 +28,7 @@
game.add.tileSprite(0, 0, 2000, 2000, 'background');
game.world.setBounds(1400,1400);
game.world.setBounds(0, 0, 1400,1400);
for(var i=0,nb=10;i<nb;i++){
@@ -68,6 +68,16 @@
}
function render() {
game.debug.renderCameraInfo(game.camera, 32, 32);
// game.debug.renderSpriteInfo(d, 32, 200);
// game.debug.renderWorldTransformInfo(d, 32, 200);
// game.debug.renderLocalTransformInfo(d, 32, 400);
// game.debug.renderSpriteCorners(d, false, true);
}
</script>
+90
View File
@@ -0,0 +1,90 @@
<?php
$title = "Cursor Key Movement";
require('../head.php');
?>
<script type="text/javascript">
window.onload = function () {
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render : render });
// var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.stage.backgroundColor = '#007236';
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
game.load.image('phaser', 'assets/sprites/sonic_havok_sanity.png');
}
var cursors;
function create() {
// Modify the world and camera bounds
game.world.setBounds(-1000, -1000, 2000, 2000);
for (var i = 0; i < 100; i++)
{
game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
}
// This will create a new object called "cursors", inside it will contain 4 objects: up, down, left and right.
// These are all Phaser.Key objects, so anything you can do with a Key object you can do with these.
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
// For example this checks if the up or down keys are pressed and moves the camera accordingly.
if (cursors.up.isDown)
{
// If the shift key is also pressed then the world is rotated
if (cursors.up.shiftKey)
{
game.world.rotation += 0.05;
}
else
{
game.camera.y -= 4;
}
}
else if (cursors.down.isDown)
{
if (cursors.down.shiftKey)
{
game.world.rotation -= 0.05;
}
else
{
game.camera.y += 4;
}
}
if (cursors.left.isDown)
{
game.camera.x -= 4;
}
else if (cursors.right.isDown)
{
game.camera.x += 4;
}
}
function render() {
game.debug.renderCameraInfo(game.camera, 32, 32);
}
};
</script>
<?php
require('../foot.php');
?>
+62
View File
@@ -0,0 +1,62 @@
<?php
$title = "Angular Acceleration";
require('../head.php');
?>
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('arrow', 'assets/sprites/arrow.png');
}
var sprite;
function create() {
game.stage.backgroundColor = '#0072bc';
sprite = game.add.sprite(400, 300, 'arrow');
sprite.anchor.setTo(0.5, 0.5);
// We'll set a lower max angular velocity here to keep it from going totally nuts
sprite.body.maxAngular = 500;
// Apply a drag otherwise the sprite will just spin and never slow down
sprite.body.angularDrag = 50;
}
function update() {
// Reset the acceleration
sprite.body.angularAcceleration = 0;
// Apply acceleration if the left/right arrow keys are held down
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
{
sprite.body.angularAcceleration -= 200;
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
{
sprite.body.angularAcceleration += 200;
}
}
function render() {
game.debug.renderSpriteInfo(sprite, 32, 32);
game.debug.renderText('angularVelocity: ' + sprite.body.angularVelocity, 32, 200);
game.debug.renderText('angularAcceleration: ' + sprite.body.angularAcceleration, 32, 232);
game.debug.renderText('angularDrag: ' + sprite.body.angularDrag, 32, 264);
game.debug.renderText('deltaZ: ' + sprite.body.deltaZ(), 32, 296);
}
</script>
<?php
require('../foot.php');
?>
+61
View File
@@ -0,0 +1,61 @@
<?php
$title = "Angular Velocity";
require('../head.php');
?>
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('arrow', 'assets/sprites/arrow.png');
}
var sprite;
function create() {
game.stage.backgroundColor = '#0072bc';
sprite = game.add.sprite(400, 300, 'arrow');
sprite.anchor.setTo(0.5, 0.5);
}
function update() {
sprite.body.velocity.x = 0;
sprite.body.velocity.y = 0;
sprite.body.angularVelocity = 0;
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
{
sprite.body.angularVelocity = -200;
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
{
sprite.body.angularVelocity = 200;
}
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
{
sprite.body.velocity.copyFrom(game.physics.velocityFromAngle(sprite.angle, 300));
}
}
function render() {
game.debug.renderSpriteInfo(sprite, 32, 32);
game.debug.renderText('angularVelocity: ' + sprite.body.angularVelocity, 32, 200);
game.debug.renderText('angularAcceleration: ' + sprite.body.angularAcceleration, 32, 232);
game.debug.renderText('angularDrag: ' + sprite.body.angularDrag, 32, 264);
game.debug.renderText('deltaZ: ' + sprite.body.deltaZ(), 32, 296);
}
</script>
<?php
require('../foot.php');
?>
@@ -1,49 +1,41 @@
<?php
$title = "Motion";
$title = "Mass Velocity Test";
require('../head.php');
?>
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update});
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('car', 'assets/sprites/car90.png');
game.load.image('ship', 'assets/sprites/xenon2_ship.png');
game.load.image('baddie', 'assets/sprites/space-baddie.png');
}
var car;
var ship;
var total;
var aliens;
function create() {
aliens = [];
aliens = game.add.group();
for (var i = 0; i < 50; i++)
{
var s = game.add.sprite(game.world.randomX, game.world.randomY, 'baddie');
var s = aliens.create(game.world.randomX, game.world.randomY, 'baddie');
s.name = 'alien' + s;
s.body.collideWorldBounds = true;
s.body.bounce.setTo(1, 1);
s.body.bounce.setTo(0.8, 0.8);
s.body.velocity.setTo(10 + Math.random() * 40, 10 + Math.random() * 40);
aliens.push(s);
}
car = game.add.sprite(400, 300, 'car');
car.anchor.setTo(0.5, 0.5);
car.body.collideWorldBounds = true;
car.body.bounce.setTo(0.5, 0.5);
car.body.bounce.setTo(0.8, 0.8);
car.body.allowRotation = true;
ship = game.add.sprite(400, 400, 'ship');
ship.body.collideWorldBounds = true;
ship.body.bounce.setTo(0.5, 0.5);
}
function update() {
@@ -66,11 +58,15 @@
car.body.velocity.copyFrom(game.physics.velocityFromAngle(car.angle, 300));
}
game.physics.collide(aliens);
game.physics.collide(car, aliens);
}
function render() {
game.debug.renderSpriteInfo(car, 32, 32);
}
</script>
+1 -1
View File
@@ -22,7 +22,7 @@
fuji = game.add.sprite(game.world.centerX, game.world.centerY, 'fuji');
fuji.anchor.setTo(0, 0.5);
fuji.angle = 34;
// fuji.angle = 34;
b = new Phaser.Rectangle(fuji.center.x, fuji.center.y, fuji.width, fuji.height);
+46
View File
@@ -0,0 +1,46 @@
<?php
$title = "Sprite Rotation";
require('../head.php');
?>
<script type="text/javascript">
// var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('arrow', 'assets/sprites/arrow.png');
}
var sprite;
function create() {
game.stage.backgroundColor = '#0072bc';
sprite = game.add.sprite(400, 300, 'arrow');
sprite.anchor.setTo(0.5, 0.5);
}
function update() {
sprite.rotation += 0.01;
}
function render() {
game.debug.renderSpriteInfo(sprite, 32, 32);
game.debug.renderText('angularVelocity: ' + sprite.body.angularVelocity, 32, 200);
game.debug.renderText('angularAcceleration: ' + sprite.body.angularAcceleration, 32, 232);
game.debug.renderText('angularDrag: ' + sprite.body.angularDrag, 32, 264);
game.debug.renderText('deltaZ: ' + sprite.body.deltaZ(), 32, 296);
}
</script>
<?php
require('../foot.php');
?>
+2 -4
View File
@@ -5,8 +5,6 @@
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
@@ -42,10 +40,10 @@
function update() {
map.collide(p);
// map.collide(p);
p.body.velocity.x = 0;
p.body.acceleration.y = 500;
// p.body.acceleration.y = 500;
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
{
+87
View File
@@ -0,0 +1,87 @@
<?php
$title = "Fixing a Sprite to the Camera";
require('../head.php');
?>
<script type="text/javascript">
window.onload = function () {
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render : render });
// var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.stage.backgroundColor = '#007236';
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
game.load.image('phaser', 'assets/sprites/phaser1.png');
}
var cursors;
var logo1;
var logo2;
function create() {
// Modify the world and camera bounds
game.world.setBounds(-1000, -1000, 2000, 2000);
for (var i = 0; i < 200; i++)
{
game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
}
logo1 = game.add.sprite(100, 100, 'phaser');
logo1.fixedToCamera = true;
logo2 = game.add.sprite(500, 100, 'phaser');
logo2.fixedToCamera = true;
game.add.tween(logo2).to( { y: 400 }, 2000, Phaser.Easing.Back.InOut, true, 0, 2000, true);
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
if (cursors.up.isDown)
{
game.camera.y -= 4;
}
else if (cursors.down.isDown)
{
game.camera.y += 4;
}
if (cursors.left.isDown)
{
game.camera.x -= 4;
}
else if (cursors.right.isDown)
{
game.camera.x += 4;
}
}
function render() {
game.debug.renderCameraInfo(game.camera, 32, 32);
// game.debug.renderSpriteInfo(d, 32, 200);
// game.debug.renderWorldTransformInfo(d, 32, 200);
// game.debug.renderLocalTransformInfo(d, 32, 400);
// game.debug.renderSpriteCorners(d, false, true);
}
};
</script>
<?php
require('../foot.php');
?>
+11 -7
View File
@@ -164,8 +164,7 @@ Phaser.Camera.prototype = {
*/
focusOnXY: function (x, y) {
this.view.x = Math.round(x - this.view.halfWidth);
this.view.y = Math.round(y - this.view.halfHeight);
this.setPosition(Math.round(x - this.view.halfWidth), Math.round(y - this.view.halfHeight));
},
@@ -185,6 +184,16 @@ Phaser.Camera.prototype = {
this.checkBounds();
}
if (this.view.x !== -this.displayObject.position.x)
{
this.displayObject.position.x = -this.view.x;
}
if (this.view.y !== -this.displayObject.position.y)
{
this.displayObject.position.y = -this.view.y;
}
},
updateTarget: function () {
@@ -283,9 +292,6 @@ Phaser.Camera.prototype = {
this.view.x = x;
this.view.y = y;
this.displayObject.x = -x;
this.displayObject.y = -y;
if (this.bounds)
{
this.checkBounds();
@@ -323,7 +329,6 @@ Object.defineProperty(Phaser.Camera.prototype, "x", {
set: function (value) {
this.view.x = value;
this.displayObject.position.x = -value;
if (this.bounds)
{
@@ -347,7 +352,6 @@ Object.defineProperty(Phaser.Camera.prototype, "y", {
set: function (value) {
this.view.y = value;
this.displayObject.position.y = -value;
if (this.bounds)
{
+2 -2
View File
@@ -71,8 +71,6 @@ Phaser.World.prototype.boot = function () {
*/
Phaser.World.prototype.update = function () {
this.camera.update();
this.currentRenderOrderID = 0;
if (this.game.stage._stage.first._iNext)
@@ -104,6 +102,8 @@ Phaser.World.prototype.update = function () {
*/
Phaser.World.prototype.postUpdate = function () {
this.camera.update();
if (this.game.stage._stage.first._iNext)
{
var currentNode = this.game.stage._stage.first._iNext;
+28 -6
View File
@@ -266,12 +266,12 @@ Phaser.Sprite = function (game, x, y, key, frame) {
/**
* @property {Description} velocity - Description.
*/
this.velocity = this.body.velocity;
// this.velocity = this.body.velocity;
/**
* @property {Description} acceleration - Description.
*/
this.acceleration = this.body.acceleration;
// this.acceleration = this.body.acceleration;
/**
* @property {Description} inWorld - World bounds check.
@@ -291,6 +291,13 @@ Phaser.Sprite = function (game, x, y, key, frame) {
*/
this._outOfBoundsFired = false;
/**
* A Sprite that is fixed to the camera ignores the position of any ancestors in the display list and uses its x/y coordinates as offsets from the top left of the camera.
* @property {boolean} fixedToCamera - Fixes this Sprite to the Camera.
* @default
*/
this.fixedToCamera = false;
};
// Needed to keep the PIXI.Sprite constructor in the prototype chain (as the core pixi renderer uses an instanceof check sadly)
@@ -428,8 +435,16 @@ Phaser.Sprite.prototype.postUpdate = function() {
this.body.postUpdate();
}
this._cache.x = this.x;
this._cache.y = this.y;
if (this.fixedToCamera)
{
this._cache.x = this.game.camera.view.x + this.x;
this._cache.y = this.game.camera.view.y + this.y;
}
else
{
this._cache.x = this.x;
this._cache.y = this.y;
}
if (this.position.x != this._cache.x || this.position.y != this._cache.y)
{
@@ -644,14 +659,21 @@ Phaser.Sprite.prototype.play = function (name, frameRate, loop) {
}
/**
* 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.
* 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.
* If you wish to work in radians instead of degrees use the property Sprite.rotation instead.
* @name Phaser.Sprite#angle
* @property {number} angle - Gets or sets the Sprites angle of rotation in degrees.
*/
Object.defineProperty(Phaser.Sprite.prototype, 'angle', {
get: function() {
return Phaser.Math.radToDeg(this.rotation);
return Phaser.Math.wrapAngle(Phaser.Math.radToDeg(this.rotation));
},
set: function(value) {
this.rotation = Phaser.Math.degToRad(value);
this.rotation = Phaser.Math.degToRad(Phaser.Math.wrapAngle(value));
}
});
+3 -3
View File
@@ -48,9 +48,9 @@ Phaser.Physics.Arcade.prototype = {
// If you're wondering why the velocity is halved and applied twice, read this: http://www.niksula.hut.fi/~hkankaan/Homepages/gravity.html
// Rotation
this._velocityDelta = (this.computeVelocity(0, false, body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular) - body.angularVelocity) / 2;
this._velocityDelta = (this.computeVelocity(0, body, body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular) - body.angularVelocity) / 2;
body.angularVelocity += this._velocityDelta;
body.rotation += body.angularVelocity * this.game.time.physicsElapsed;
body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);
body.angularVelocity += this._velocityDelta;
// Horizontal
@@ -100,7 +100,7 @@ Phaser.Physics.Arcade.prototype = {
if (velocity - this._drag > 0)
{
velocity = velocity - this._drag;
velocity -= this._drag;
}
else if (velocity + this._drag < 0)
{
+17 -18
View File
@@ -9,6 +9,7 @@ Phaser.Physics.Arcade.Body = function (sprite) {
this.y = sprite.y;
this.preX = sprite.x;
this.preY = sprite.y;
this.preRotation = sprite.angle;
// un-scaled original size
this.sourceWidth = sprite.currentFrame.sourceSizeW;
@@ -37,6 +38,7 @@ Phaser.Physics.Arcade.Body = function (sprite) {
this.maxAngular = 1000;
this.mass = 1;
this.skipQuadTree = false;
this.quadTreeIDs = [];
this.quadTreeIndex = -1;
@@ -105,22 +107,23 @@ Phaser.Physics.Arcade.Body.prototype = {
this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.rotation = this.sprite.angle;
this.preRotation = this.sprite.angle;
this.x = this.preX;
this.y = this.preY;
this.rotation = this.preRotation;
if (this.moves)
{
this.game.physics.updateMotion(this);
if (this.collideWorldBounds)
{
this.checkWorldBounds();
}
}
if (this.collideWorldBounds)
{
this.checkWorldBounds();
}
if (this.allowCollision.none == false && this.sprite.visible && this.sprite.alive)
if (this.skipQuadTree == false && this.allowCollision.none == false && this.sprite.visible && this.sprite.alive)
{
this.quadTreeIDs = [];
this.quadTreeIndex = -1;
@@ -159,20 +162,12 @@ Phaser.Physics.Arcade.Body.prototype = {
this.facing = Phaser.DOWN;
}
if (this.deltaX() != 0)
{
this.sprite.x += this.deltaX();
}
if (this.deltaY() != 0)
{
this.sprite.y += this.deltaY();
}
this.sprite.x += this.deltaX();
this.sprite.y += this.deltaY();
if (this.allowRotation)
{
// Needs to use rotation delta
// this.sprite.angle += this.rotation;
this.sprite.angle += this.deltaZ();
}
},
@@ -247,6 +242,10 @@ Phaser.Physics.Arcade.Body.prototype = {
deltaY: function () {
return this.y - this.preY;
},
deltaZ: function () {
return this.rotation - this.preRotation;
}
};
+1 -1
View File
@@ -120,7 +120,7 @@ Phaser.Tilemap = function (game, key, x, y, resizeWorld, tileWidth, tileHeight)
if (this.currentLayer && resizeWorld)
{
this.game.world.setSize(this.currentLayer.widthInPixels, this.currentLayer.heightInPixels, true);
this.game.world.setBounds(0, 0, this.currentLayer.widthInPixels, this.currentLayer.heightInPixels);
}
};