mirror of
https://github.com/photonstorm/phaser
synced 2024-11-10 15:14:47 +00:00
Lots more physics tests and updates.
PLEASE DO NOT upgrade to this release if you need your game working and it uses any of the physics functions, as they're nearly all broken here. Just pushing up so I can share it with someone.
This commit is contained in:
parent
2532df8793
commit
128c7143d5
17 changed files with 1623 additions and 99 deletions
12
README.md
12
README.md
|
@ -12,7 +12,8 @@ By Richard Davey, [Photon Storm](http://www.photonstorm.com)
|
|||
View the [Official Website](http://phaser.io)<br />
|
||||
Follow on [Twitter](https://twitter.com/photonstorm)<br />
|
||||
Join the [Forum](http://www.html5gamedevs.com/forum/14-phaser/)<br />
|
||||
Try out 200+ [Phaser Examples](http://gametest.mobi/phaser/examples/)
|
||||
Try out 200+ [Phaser Examples](http://gametest.mobi/phaser/examples/)<br />
|
||||
Read the [documentation online](http://docs.phaser.io)
|
||||
|
||||
[Subscribe to our new Phaser Newsletter](https://confirmsubscription.com/h/r/369DE48E3E86AF1E). We'll email you when new versions are released as well as send you our regular Phaser game making magazine.
|
||||
|
||||
|
@ -36,11 +37,11 @@ Phaser is everything we ever wanted from an HTML5 game framework. It powers all
|
|||
Getting Started Guides
|
||||
----------------------
|
||||
|
||||
We have a new [Getting Started Guide](http://phaser.io/getting-started-js.php) which covers all you need to begin developing games with Phaser. From setting up a web server to picking an IDE. If you're new to HTML5 game development (or are coming from another language like AS3) then we recommend starting there.
|
||||
We have a new [Getting Started Guide](http://phaser.io/getting-started-js.php) which covers all you need to begin developing games with Phaser. From setting up a web server to picking an IDE. If you're new to HTML5 game development, or are coming from another language like AS3, then we recommend starting there.
|
||||
|
||||
There is a comprehensive [How to Learn Phaser](http://gamedevelopment.tutsplus.com/articles/how-to-learn-the-phaser-html5-game-engine--gamedev-13643) guide on the GameDevTuts+ site which is well worth reading through.
|
||||
There is a comprehensive [How to Learn Phaser](http://gamedevelopment.tutsplus.com/articles/how-to-learn-the-phaser-html5-game-engine--gamedev-13643) guide on the GameDevTuts+ site which is a great place to learn where to find tutorials, examples and support.
|
||||
|
||||
There is also this great [Un-official Getting Started Guide](http://www.antonoffplus.com/coding-an-html5-game-for-30-minutes-or-an-introduction-to-the-phaser-framework) which is well worth running through as well.
|
||||
There is also an [un-official Getting Started Guide](http://www.antonoffplus.com/coding-an-html5-game-for-30-minutes-or-an-introduction-to-the-phaser-framework).
|
||||
|
||||
|
||||
Change Log
|
||||
|
@ -89,6 +90,9 @@ New features:
|
|||
* Body.friction - This now replaces Body.drag and provides for a much smoother friction experience.
|
||||
* Body.minBounceVelocity - If a Body has bounce set, this threshold controls if it should rebound or not. Use it to stop 'jittering' on bounds/tiles with super-low velocities.
|
||||
* QuadTree.populate - you can pass it a Group and it'll automatically insert all of the children ready for inspection.
|
||||
* Input.setMoveCallback allows you to set a callback that will be fired each time the activePointer receives a DOM move event.
|
||||
* Math.distancePow(x1,y1,x2,y2,power) returns the distance between two coordinates at the given power.
|
||||
* Physics.collideArray(obj, array) for when you want to collide an object against a number of sprites that aren't all in the same Group.
|
||||
|
||||
|
||||
New Examples:
|
||||
|
|
|
@ -807,6 +807,10 @@
|
|||
{
|
||||
"file": "swap+tiles.js",
|
||||
"title": "swap tiles"
|
||||
},
|
||||
{
|
||||
"file": "tile+callbacks.js",
|
||||
"title": "tile callbacks"
|
||||
}
|
||||
],
|
||||
"time": [
|
||||
|
|
153
examples/wip/2ball.js
Normal file
153
examples/wip/2ball.js
Normal file
|
@ -0,0 +1,153 @@
|
|||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('chunk', 'assets/sprites/chunk.png');
|
||||
game.load.image('arrow', 'assets/sprites/asteroids_ship.png');
|
||||
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
|
||||
game.load.image('ball', 'assets/sprites/shinyball.png');
|
||||
game.load.spritesheet('gameboy', 'assets/sprites/gameboy_seize_color_40x60.png', 40, 60);
|
||||
|
||||
}
|
||||
|
||||
var sprite;
|
||||
var sprite2;
|
||||
var sprite3;
|
||||
|
||||
var bmd;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#124184';
|
||||
|
||||
bmd = game.add.bitmapData(800, 600);
|
||||
bmd.fillStyle('#ffffff');
|
||||
var bg = game.add.sprite(0, 0, bmd);
|
||||
bg.body.moves = false;
|
||||
|
||||
test3();
|
||||
|
||||
}
|
||||
|
||||
function test3() {
|
||||
|
||||
// game.physics.gravity.y = 100;
|
||||
|
||||
sprite = game.add.sprite(500, 400, 'gameboy', 0);
|
||||
sprite.name = 'red';
|
||||
sprite.body.collideWorldBounds = true;
|
||||
sprite.body.bounce.setTo(0.5, 0.5);
|
||||
|
||||
sprite2 = game.add.sprite(0, 400, 'gameboy', 2);
|
||||
sprite2.name = 'green';
|
||||
sprite2.body.collideWorldBounds = true;
|
||||
|
||||
sprite3 = game.add.sprite(700, 400, 'gameboy', 3);
|
||||
sprite3.name = 'yellow';
|
||||
sprite3.body.collideWorldBounds = true;
|
||||
|
||||
sprite.body.velocity.x = -300;
|
||||
|
||||
game.input.onDown.add(launch3, this);
|
||||
|
||||
}
|
||||
|
||||
function launch3() {
|
||||
|
||||
sprite.body.velocity.x *= 10;
|
||||
|
||||
}
|
||||
|
||||
function test2() {
|
||||
|
||||
// game.physics.gravity.y = 100;
|
||||
|
||||
sprite = game.add.sprite(700, 400, 'ball');
|
||||
sprite.name = 'sprite1';
|
||||
sprite.body.collideWorldBounds = true;
|
||||
sprite.body.bounce.setTo(0.8, 0.8);
|
||||
|
||||
sprite2 = game.add.sprite(100, 400, 'ball');
|
||||
sprite2.name = 'sprite2';
|
||||
sprite2.body.collideWorldBounds = true;
|
||||
sprite2.body.bounce.setTo(0.8, 0.8);
|
||||
sprite2.body.immovable = true;
|
||||
|
||||
// sprite.body.velocity.x = -225;
|
||||
// sprite2.body.velocity.x = 225;
|
||||
|
||||
|
||||
game.input.onDown.addOnce(launch2, this);
|
||||
|
||||
}
|
||||
|
||||
function launch2() {
|
||||
|
||||
sprite.body.velocity.x = -225;
|
||||
sprite2.body.velocity.x = 225;
|
||||
|
||||
}
|
||||
|
||||
function test1() {
|
||||
|
||||
// game.physics.gravity.y = 100;
|
||||
|
||||
sprite = game.add.sprite(100, 400, 'ball');
|
||||
sprite.name = 'sprite1';
|
||||
sprite.body.collideWorldBounds = true;
|
||||
sprite.body.bounce.setTo(0.8, 0.8);
|
||||
|
||||
sprite2 = game.add.sprite(700, 400, 'ball');
|
||||
sprite2.name = 'sprite2';
|
||||
sprite2.body.collideWorldBounds = true;
|
||||
sprite2.body.bounce.setTo(0.8, 0.8);
|
||||
|
||||
game.input.onDown.add(launch1, this);
|
||||
|
||||
}
|
||||
|
||||
function launch1() {
|
||||
|
||||
sprite.body.velocity.x = 225;
|
||||
// sprite2.body.velocity.x = -225;
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
// game.physics.collide(sprite, sprite2);
|
||||
// game.physics.collide(sprite2, sprite3);
|
||||
game.physics.collideArray(sprite, [sprite2, sprite3]);
|
||||
|
||||
// sprite.rotation = sprite.body.angle;
|
||||
|
||||
if (sprite)
|
||||
{
|
||||
bmd.fillStyle('#ffff00');
|
||||
bmd.fillRect(sprite.body.center.x, sprite.body.center.y, 2, 2);
|
||||
}
|
||||
|
||||
if (sprite2)
|
||||
{
|
||||
bmd.fillStyle('#ff00ff');
|
||||
bmd.fillRect(sprite2.body.center.x, sprite2.body.center.y, 2, 2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
if (sprite)
|
||||
{
|
||||
game.debug.renderBodyInfo(sprite, 16, 24);
|
||||
game.debug.renderText(sprite.name + ' x: ' + sprite.x, 16, 500);
|
||||
}
|
||||
|
||||
if (sprite2)
|
||||
{
|
||||
game.debug.renderBodyInfo(sprite2, 16, 190);
|
||||
game.debug.renderText(sprite2.name + ' x: ' + sprite2.x, 400, 500);
|
||||
}
|
||||
|
||||
}
|
822
examples/wip/SAT.js
Normal file
822
examples/wip/SAT.js
Normal file
|
@ -0,0 +1,822 @@
|
|||
// Version 0.2 - Copyright 2013 - Jim Riecken <jimr@jimr.ca>
|
||||
//
|
||||
// Released under the MIT License - https://github.com/jriecken/sat-js
|
||||
//
|
||||
// A simple library for determining intersections of circles and
|
||||
// polygons using the Separating Axis Theorem.
|
||||
/** @preserve SAT.js - Version 0.2 - Copyright 2013 - Jim Riecken <jimr@jimr.ca> - released under the MIT License. https://github.com/jriecken/sat-js */
|
||||
|
||||
/*global define: false, module: false*/
|
||||
/*jshint shadow:true, sub:true, forin:true, noarg:true, noempty:true,
|
||||
eqeqeq:true, bitwise:true, strict:true, undef:true,
|
||||
curly:true, browser:true */
|
||||
|
||||
// Create a UMD wrapper for SAT. Works in:
|
||||
//
|
||||
// - Plain browser via global SAT variable
|
||||
// - AMD loader (like require.js)
|
||||
// - Node.js
|
||||
//
|
||||
// The quoted properties all over the place are used so that the Closure Compiler
|
||||
// does not mangle the exposed API in advanced mode.
|
||||
/**
|
||||
* @param {*} root - The global scope
|
||||
* @param {Function} factory - Factory that creates SAT module
|
||||
*/
|
||||
(function (root, factory) {
|
||||
"use strict";
|
||||
if (typeof define === 'function' && define['amd']) {
|
||||
define(factory);
|
||||
} else if (typeof exports === 'object') {
|
||||
module['exports'] = factory();
|
||||
} else {
|
||||
root['SAT'] = factory();
|
||||
}
|
||||
}(this, function () {
|
||||
"use strict";
|
||||
|
||||
var SAT = {};
|
||||
|
||||
//
|
||||
// ## Vector
|
||||
//
|
||||
// Represents a vector in two dimensions with `x` and `y` properties.
|
||||
|
||||
|
||||
// Create a new Vector, optionally passing in the `x` and `y` coordinates. If
|
||||
// a coordinate is not specified, it will be set to `0`
|
||||
/**
|
||||
* @param {?number=} x The x position.
|
||||
* @param {?number=} y The y position.
|
||||
* @constructor
|
||||
*/
|
||||
function Vector(x, y) {
|
||||
this['x'] = x || 0;
|
||||
this['y'] = y || 0;
|
||||
}
|
||||
SAT['Vector'] = Vector;
|
||||
// Alias `Vector` as `V`
|
||||
SAT['V'] = Vector;
|
||||
|
||||
|
||||
// Copy the values of another Vector into this one.
|
||||
/**
|
||||
* @param {Vector} other The other Vector.
|
||||
* @return {Vector} This for chaining.
|
||||
*/
|
||||
Vector.prototype['copy'] = Vector.prototype.copy = function(other) {
|
||||
this['x'] = other['x'];
|
||||
this['y'] = other['y'];
|
||||
return this;
|
||||
};
|
||||
|
||||
// Change this vector to be perpendicular to what it was before. (Effectively
|
||||
// roatates it 90 degrees in a clockwise direction)
|
||||
/**
|
||||
* @return {Vector} This for chaining.
|
||||
*/
|
||||
Vector.prototype['perp'] = Vector.prototype.perp = function() {
|
||||
var x = this['x'];
|
||||
this['x'] = this['y'];
|
||||
this['y'] = -x;
|
||||
return this;
|
||||
};
|
||||
|
||||
// Rotate this vector (counter-clockwise) by the specified angle (in radians).
|
||||
/**
|
||||
* @param {number} angle The angle to rotate (in radians)
|
||||
* @return {Vector} This for chaining.
|
||||
*/
|
||||
Vector.prototype['rotate'] = Vector.prototype.rotate = function (angle) {
|
||||
var x = this['x'];
|
||||
var y = this['y'];
|
||||
this['x'] = x * Math.cos(angle) - y * Math.sin(angle);
|
||||
this['y'] = x * Math.sin(angle) + y * Math.cos(angle);
|
||||
return this;
|
||||
};
|
||||
|
||||
// Reverse this vector.
|
||||
/**
|
||||
* @return {Vector} This for chaining.
|
||||
*/
|
||||
Vector.prototype['reverse'] = Vector.prototype.reverse = function() {
|
||||
this['x'] = -this['x'];
|
||||
this['y'] = -this['y'];
|
||||
return this;
|
||||
};
|
||||
|
||||
|
||||
// Normalize this vector. (make it have length of `1`)
|
||||
/**
|
||||
* @return {Vector} This for chaining.
|
||||
*/
|
||||
Vector.prototype['normalize'] = Vector.prototype.normalize = function() {
|
||||
var d = this.len();
|
||||
if(d > 0) {
|
||||
this['x'] = this['x'] / d;
|
||||
this['y'] = this['y'] / d;
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
// Add another vector to this one.
|
||||
/**
|
||||
* @param {Vector} other The other Vector.
|
||||
* @return {Vector} This for chaining.
|
||||
*/
|
||||
Vector.prototype['add'] = Vector.prototype.add = function(other) {
|
||||
this['x'] += other['x'];
|
||||
this['y'] += other['y'];
|
||||
return this;
|
||||
};
|
||||
|
||||
// Subtract another vector from this one.
|
||||
/**
|
||||
* @param {Vector} other The other Vector.
|
||||
* @return {Vector} This for chaiing.
|
||||
*/
|
||||
Vector.prototype['sub'] = Vector.prototype.sub = function(other) {
|
||||
this['x'] -= other['x'];
|
||||
this['y'] -= other['y'];
|
||||
return this;
|
||||
};
|
||||
|
||||
// Scale this vector. An independant scaling factor can be provided
|
||||
// for each axis, or a single scaling factor that will scale both `x` and `y`.
|
||||
/**
|
||||
* @param {number} x The scaling factor in the x direction.
|
||||
* @param {?number=} y The scaling factor in the y direction. If this
|
||||
* is not specified, the x scaling factor will be used.
|
||||
* @return {Vector} This for chaining.
|
||||
*/
|
||||
Vector.prototype['scale'] = Vector.prototype.scale = function(x,y) {
|
||||
this['x'] *= x;
|
||||
this['y'] *= y || x;
|
||||
return this;
|
||||
};
|
||||
|
||||
// Project this vector on to another vector.
|
||||
/**
|
||||
* @param {Vector} other The vector to project onto.
|
||||
* @return {Vector} This for chaining.
|
||||
*/
|
||||
Vector.prototype['project'] = Vector.prototype.project = function(other) {
|
||||
var amt = this.dot(other) / other.len2();
|
||||
this['x'] = amt * other['x'];
|
||||
this['y'] = amt * other['y'];
|
||||
return this;
|
||||
};
|
||||
|
||||
// Project this vector onto a vector of unit length. This is slightly more efficient
|
||||
// than `project` when dealing with unit vectors.
|
||||
/**
|
||||
* @param {Vector} other The unit vector to project onto.
|
||||
* @return {Vector} This for chaining.
|
||||
*/
|
||||
Vector.prototype['projectN'] = Vector.prototype.projectN = function(other) {
|
||||
var amt = this.dot(other);
|
||||
this['x'] = amt * other['x'];
|
||||
this['y'] = amt * other['y'];
|
||||
return this;
|
||||
};
|
||||
|
||||
// Reflect this vector on an arbitrary axis.
|
||||
/**
|
||||
* @param {Vector} axis The vector representing the axis.
|
||||
* @return {Vector} This for chaining.
|
||||
*/
|
||||
Vector.prototype['reflect'] = Vector.prototype.reflect = function(axis) {
|
||||
var x = this['x'];
|
||||
var y = this['y'];
|
||||
this.project(axis).scale(2);
|
||||
this['x'] -= x;
|
||||
this['y'] -= y;
|
||||
return this;
|
||||
};
|
||||
|
||||
// Reflect this vector on an arbitrary axis (represented by a unit vector). This is
|
||||
// slightly more efficient than `reflect` when dealing with an axis that is a unit vector.
|
||||
/**
|
||||
* @param {Vector} axis The unit vector representing the axis.
|
||||
* @return {Vector} This for chaining.
|
||||
*/
|
||||
Vector.prototype['reflectN'] = Vector.prototype.reflectN = function(axis) {
|
||||
var x = this['x'];
|
||||
var y = this['y'];
|
||||
this.projectN(axis).scale(2);
|
||||
this['x'] -= x;
|
||||
this['y'] -= y;
|
||||
return this;
|
||||
};
|
||||
|
||||
// Get the dot product of this vector and another.
|
||||
/**
|
||||
* @param {Vector} other The vector to dot this one against.
|
||||
* @return {number} The dot product.
|
||||
*/
|
||||
Vector.prototype['dot'] = Vector.prototype.dot = function(other) {
|
||||
return this['x'] * other['x'] + this['y'] * other['y'];
|
||||
};
|
||||
|
||||
// Get the squared length of this vector.
|
||||
/**
|
||||
* @return {number} The length^2 of this vector.
|
||||
*/
|
||||
Vector.prototype['len2'] = Vector.prototype.len2 = function() {
|
||||
return this.dot(this);
|
||||
};
|
||||
|
||||
// Get the length of this vector.
|
||||
/**
|
||||
* @return {number} The length of this vector.
|
||||
*/
|
||||
Vector.prototype['len'] = Vector.prototype.len = function() {
|
||||
return Math.sqrt(this.len2());
|
||||
};
|
||||
|
||||
// ## Circle
|
||||
//
|
||||
// Represents a circle with a position and a radius.
|
||||
|
||||
// Create a new circle, optionally passing in a position and/or radius. If no position
|
||||
// is given, the circle will be at `(0,0)`. If no radius is provided, the circle will
|
||||
// have a radius of `0`.
|
||||
/**
|
||||
* @param {Vector=} pos A vector representing the position of the center of the circle
|
||||
* @param {?number=} r The radius of the circle
|
||||
* @constructor
|
||||
*/
|
||||
function Circle(pos, r) {
|
||||
this['pos'] = pos || new Vector();
|
||||
this['r'] = r || 0;
|
||||
}
|
||||
SAT['Circle'] = Circle;
|
||||
|
||||
// ## Polygon
|
||||
//
|
||||
// Represents a *convex* polygon with any number of points (specified in counter-clockwise order)
|
||||
//
|
||||
// The edges/normals of the polygon will be calculated on creation and stored in the
|
||||
// `edges` and `normals` properties. If you change the polygon's points, you will need
|
||||
// to call `recalc` to recalculate the edges/normals.
|
||||
|
||||
// Create a new polygon, passing in a position vector, and an array of points (represented
|
||||
// by vectors relative to the position vector). If no position is passed in, the position
|
||||
// of the polygon will be `(0,0)`.
|
||||
/**
|
||||
* @param {Vector=} pos A vector representing the origin of the polygon. (all other
|
||||
* points are relative to this one)
|
||||
* @param {Array.<Vector>=} points An array of vectors representing the points in the polygon,
|
||||
* in counter-clockwise order.
|
||||
* @constructor
|
||||
*/
|
||||
function Polygon(pos, points) {
|
||||
this['pos'] = pos || new Vector();
|
||||
this['points'] = points || [];
|
||||
this.recalc();
|
||||
}
|
||||
SAT['Polygon'] = Polygon;
|
||||
|
||||
// Recalculates the edges and normals of the polygon. This **must** be called
|
||||
// if the `points` array is modified at all and the edges or normals are to be
|
||||
// accessed.
|
||||
/**
|
||||
* @return {Polygon} This for chaining.
|
||||
*/
|
||||
Polygon.prototype['recalc'] = Polygon.prototype.recalc = function() {
|
||||
// The edges here are the direction of the `n`th edge of the polygon, relative to
|
||||
// the `n`th point. If you want to draw a given edge from the edge value, you must
|
||||
// first translate to the position of the starting point.
|
||||
this['edges'] = [];
|
||||
// The normals here are the direction of the normal for the `n`th edge of the polygon, relative
|
||||
// to the position of the `n`th point. If you want to draw an edge normal, you must first
|
||||
// translate to the position of the starting point.
|
||||
this['normals'] = [];
|
||||
var points = this['points'];
|
||||
var len = points.length;
|
||||
for (var i = 0; i < len; i++) {
|
||||
var p1 = points[i];
|
||||
var p2 = i < len - 1 ? points[i + 1] : points[0];
|
||||
var e = new Vector().copy(p2).sub(p1);
|
||||
var n = new Vector().copy(e).perp().normalize();
|
||||
this['edges'].push(e);
|
||||
this['normals'].push(n);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
// Rotates this polygon counter-clockwise around the origin of *its local coordinate system* (i.e. `pos`).
|
||||
//
|
||||
// Note: You do **not** need to call `recalc` after rotation.
|
||||
/**
|
||||
* @param {number} angle The angle to rotate (in radians)
|
||||
* @return {Polygon} This for chaining.
|
||||
*/
|
||||
Polygon.prototype['rotate'] = Polygon.prototype.rotate = function(angle) {
|
||||
var i;
|
||||
var points = this['points'];
|
||||
var edges = this['edges'];
|
||||
var normals = this['normals'];
|
||||
var len = points.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
points[i].rotate(angle);
|
||||
edges[i].rotate(angle);
|
||||
normals[i].rotate(angle);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
// Translates the points of this polygon by a specified amount relative to the origin of *its own coordinate
|
||||
// system* (i.e. `pos`).
|
||||
//
|
||||
// This is most useful to change the "center point" of a polygon.
|
||||
//
|
||||
// Note: You do **not** need to call `recalc` after translation.
|
||||
/**
|
||||
* @param {number} x The horizontal amount to translate.
|
||||
* @param {number} y The vertical amount to translate.
|
||||
* @return {Polygon} This for chaining.
|
||||
*/
|
||||
Polygon.prototype['translate'] = Polygon.prototype.translate = function (x, y) {
|
||||
var i;
|
||||
var points = this['points'];
|
||||
var len = points.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
points[i].x += x;
|
||||
points[i].y += y;
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
// ## Box
|
||||
//
|
||||
// Represents an axis-aligned box, with a width and height.
|
||||
|
||||
|
||||
// Create a new box, with the specified position, width, and height. If no position
|
||||
// is given, the position will be `(0,0)`. If no width or height are given, they will
|
||||
// be set to `0`.
|
||||
/**
|
||||
* @param {Vector=} pos A vector representing the top-left of the box.
|
||||
* @param {?number=} w The width of the box.
|
||||
* @param {?number=} h The height of the box.
|
||||
* @constructor
|
||||
*/
|
||||
function Box(pos, w, h) {
|
||||
this['pos'] = pos || new Vector();
|
||||
this['w'] = w || 0;
|
||||
this['h'] = h || 0;
|
||||
}
|
||||
SAT['Box'] = Box;
|
||||
|
||||
// Returns a polygon whose edges are the same as this box.
|
||||
/**
|
||||
* @return {Polygon} A new Polygon that represents this box.
|
||||
*/
|
||||
Box.prototype['toPolygon'] = Box.prototype.toPolygon = function() {
|
||||
var pos = this['pos'];
|
||||
var w = this['w'];
|
||||
var h = this['h'];
|
||||
return new Polygon(new Vector(pos['x'], pos['y']), [
|
||||
new Vector(), new Vector(w, 0),
|
||||
new Vector(w,h), new Vector(0,h)
|
||||
]);
|
||||
};
|
||||
|
||||
// ## Response
|
||||
//
|
||||
// An object representing the result of an intersection. Contains:
|
||||
// - The two objects participating in the intersection
|
||||
// - The vector representing the minimum change necessary to extract the first object
|
||||
// from the second one (as well as a unit vector in that direction and the magnitude
|
||||
// of the overlap)
|
||||
// - Whether the first object is entirely inside the second, and vice versa.
|
||||
/**
|
||||
* @constructor
|
||||
*/
|
||||
function Response() {
|
||||
this['a'] = null;
|
||||
this['b'] = null;
|
||||
this['overlapN'] = new Vector();
|
||||
this['overlapV'] = new Vector();
|
||||
this.clear();
|
||||
}
|
||||
SAT['Response'] = Response;
|
||||
|
||||
// Set some values of the response back to their defaults. Call this between tests if
|
||||
// you are going to reuse a single Response object for multiple intersection tests (recommented
|
||||
// as it will avoid allcating extra memory)
|
||||
/**
|
||||
* @return {Response} This for chaining
|
||||
*/
|
||||
Response.prototype['clear'] = Response.prototype.clear = function() {
|
||||
this['aInB'] = true;
|
||||
this['bInA'] = true;
|
||||
this['overlap'] = Number.MAX_VALUE;
|
||||
return this;
|
||||
};
|
||||
|
||||
// ## Object Pools
|
||||
|
||||
// A pool of `Vector` objects that are used in calculations to avoid
|
||||
// allocating memory.
|
||||
/**
|
||||
* @type {Array.<Vector>}
|
||||
*/
|
||||
var T_VECTORS = [];
|
||||
for (var i = 0; i < 10; i++) { T_VECTORS.push(new Vector()); }
|
||||
|
||||
// A pool of arrays of numbers used in calculations to avoid allocating
|
||||
// memory.
|
||||
/**
|
||||
* @type {Array.<Array.<number>>}
|
||||
*/
|
||||
var T_ARRAYS = [];
|
||||
for (var i = 0; i < 5; i++) { T_ARRAYS.push([]); }
|
||||
|
||||
// ## Helper Functions
|
||||
|
||||
// Flattens the specified array of points onto a unit vector axis,
|
||||
// resulting in a one dimensional range of the minimum and
|
||||
// maximum value on that axis.
|
||||
/**
|
||||
* @param {Array.<Vector>} points The points to flatten.
|
||||
* @param {Vector} normal The unit vector axis to flatten on.
|
||||
* @param {Array.<number>} result An array. After calling this function,
|
||||
* result[0] will be the minimum value,
|
||||
* result[1] will be the maximum value.
|
||||
*/
|
||||
function flattenPointsOn(points, normal, result) {
|
||||
var min = Number.MAX_VALUE;
|
||||
var max = -Number.MAX_VALUE;
|
||||
var len = points.length;
|
||||
for (var i = 0; i < len; i++ ) {
|
||||
// The magnitude of the projection of the point onto the normal
|
||||
var dot = points[i].dot(normal);
|
||||
if (dot < min) { min = dot; }
|
||||
if (dot > max) { max = dot; }
|
||||
}
|
||||
result[0] = min; result[1] = max;
|
||||
}
|
||||
|
||||
// Check whether two convex polygons are separated by the specified
|
||||
// axis (must be a unit vector).
|
||||
/**
|
||||
* @param {Vector} aPos The position of the first polygon.
|
||||
* @param {Vector} bPos The position of the second polygon.
|
||||
* @param {Array.<Vector>} aPoints The points in the first polygon.
|
||||
* @param {Array.<Vector>} bPoints The points in the second polygon.
|
||||
* @param {Vector} axis The axis (unit sized) to test against. The points of both polygons
|
||||
* will be projected onto this axis.
|
||||
* @param {Response=} response A Response object (optional) which will be populated
|
||||
* if the axis is not a separating axis.
|
||||
* @return {boolean} true if it is a separating axis, false otherwise. If false,
|
||||
* and a response is passed in, information about how much overlap and
|
||||
* the direction of the overlap will be populated.
|
||||
*/
|
||||
function isSeparatingAxis(aPos, bPos, aPoints, bPoints, axis, response) {
|
||||
var rangeA = T_ARRAYS.pop();
|
||||
var rangeB = T_ARRAYS.pop();
|
||||
// The magnitude of the offset between the two polygons
|
||||
var offsetV = T_VECTORS.pop().copy(bPos).sub(aPos);
|
||||
var projectedOffset = offsetV.dot(axis);
|
||||
// Project the polygons onto the axis.
|
||||
flattenPointsOn(aPoints, axis, rangeA);
|
||||
flattenPointsOn(bPoints, axis, rangeB);
|
||||
// Move B's range to its position relative to A.
|
||||
rangeB[0] += projectedOffset;
|
||||
rangeB[1] += projectedOffset;
|
||||
// Check if there is a gap. If there is, this is a separating axis and we can stop
|
||||
if (rangeA[0] > rangeB[1] || rangeB[0] > rangeA[1]) {
|
||||
T_VECTORS.push(offsetV);
|
||||
T_ARRAYS.push(rangeA);
|
||||
T_ARRAYS.push(rangeB);
|
||||
return true;
|
||||
}
|
||||
// This is not a separating axis. If we're calculating a response, calculate the overlap.
|
||||
if (response) {
|
||||
var overlap = 0;
|
||||
// A starts further left than B
|
||||
if (rangeA[0] < rangeB[0]) {
|
||||
response['aInB'] = false;
|
||||
// A ends before B does. We have to pull A out of B
|
||||
if (rangeA[1] < rangeB[1]) {
|
||||
overlap = rangeA[1] - rangeB[0];
|
||||
response['bInA'] = false;
|
||||
// B is fully inside A. Pick the shortest way out.
|
||||
} else {
|
||||
var option1 = rangeA[1] - rangeB[0];
|
||||
var option2 = rangeB[1] - rangeA[0];
|
||||
overlap = option1 < option2 ? option1 : -option2;
|
||||
}
|
||||
// B starts further left than A
|
||||
} else {
|
||||
response['bInA'] = false;
|
||||
// B ends before A ends. We have to push A out of B
|
||||
if (rangeA[1] > rangeB[1]) {
|
||||
overlap = rangeA[0] - rangeB[1];
|
||||
response['aInB'] = false;
|
||||
// A is fully inside B. Pick the shortest way out.
|
||||
} else {
|
||||
var option1 = rangeA[1] - rangeB[0];
|
||||
var option2 = rangeB[1] - rangeA[0];
|
||||
overlap = option1 < option2 ? option1 : -option2;
|
||||
}
|
||||
}
|
||||
// If this is the smallest amount of overlap we've seen so far, set it as the minimum overlap.
|
||||
var absOverlap = Math.abs(overlap);
|
||||
if (absOverlap < response['overlap']) {
|
||||
response['overlap'] = absOverlap;
|
||||
response['overlapN'].copy(axis);
|
||||
if (overlap < 0) {
|
||||
response['overlapN'].reverse();
|
||||
}
|
||||
}
|
||||
}
|
||||
T_VECTORS.push(offsetV);
|
||||
T_ARRAYS.push(rangeA);
|
||||
T_ARRAYS.push(rangeB);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Calculates which Vornoi region a point is on a line segment.
|
||||
// It is assumed that both the line and the point are relative to `(0,0)`
|
||||
//
|
||||
// | (0) |
|
||||
// (-1) [S]--------------[E] (1)
|
||||
// | (0) |
|
||||
/**
|
||||
* @param {Vector} line The line segment.
|
||||
* @param {Vector} point The point.
|
||||
* @return {number} LEFT_VORNOI_REGION (-1) if it is the left region,
|
||||
* MIDDLE_VORNOI_REGION (0) if it is the middle region,
|
||||
* RIGHT_VORNOI_REGION (1) if it is the right region.
|
||||
*/
|
||||
function vornoiRegion(line, point) {
|
||||
var len2 = line.len2();
|
||||
var dp = point.dot(line);
|
||||
// If the point is beyond the start of the line, it is in the
|
||||
// left vornoi region.
|
||||
if (dp < 0) { return LEFT_VORNOI_REGION; }
|
||||
// If the point is beyond the end of the line, it is in the
|
||||
// right vornoi region.
|
||||
else if (dp > len2) { return RIGHT_VORNOI_REGION; }
|
||||
// Otherwise, it's in the middle one.
|
||||
else { return MIDDLE_VORNOI_REGION; }
|
||||
}
|
||||
// Constants for Vornoi regions
|
||||
/**
|
||||
* @const
|
||||
*/
|
||||
var LEFT_VORNOI_REGION = -1;
|
||||
/**
|
||||
* @const
|
||||
*/
|
||||
var MIDDLE_VORNOI_REGION = 0;
|
||||
/**
|
||||
* @const
|
||||
*/
|
||||
var RIGHT_VORNOI_REGION = 1;
|
||||
|
||||
// ## Collision Tests
|
||||
|
||||
// Check if two circles collide.
|
||||
/**
|
||||
* @param {Circle} a The first circle.
|
||||
* @param {Circle} b The second circle.
|
||||
* @param {Response=} response Response object (optional) that will be populated if
|
||||
* the circles intersect.
|
||||
* @return {boolean} true if the circles intersect, false if they don't.
|
||||
*/
|
||||
function testCircleCircle(a, b, response) {
|
||||
// Check if the distance between the centers of the two
|
||||
// circles is greater than their combined radius.
|
||||
var differenceV = T_VECTORS.pop().copy(b['pos']).sub(a['pos']);
|
||||
var totalRadius = a['r'] + b['r'];
|
||||
var totalRadiusSq = totalRadius * totalRadius;
|
||||
var distanceSq = differenceV.len2();
|
||||
// If the distance is bigger than the combined radius, they don't intersect.
|
||||
if (distanceSq > totalRadiusSq) {
|
||||
T_VECTORS.push(differenceV);
|
||||
return false;
|
||||
}
|
||||
// They intersect. If we're calculating a response, calculate the overlap.
|
||||
if (response) {
|
||||
var dist = Math.sqrt(distanceSq);
|
||||
response['a'] = a;
|
||||
response['b'] = b;
|
||||
response['overlap'] = totalRadius - dist;
|
||||
response['overlapN'].copy(differenceV.normalize());
|
||||
response['overlapV'].copy(differenceV).scale(response['overlap']);
|
||||
response['aInB']= a['r'] <= b['r'] && dist <= b['r'] - a['r'];
|
||||
response['bInA'] = b['r'] <= a['r'] && dist <= a['r'] - b['r'];
|
||||
}
|
||||
T_VECTORS.push(differenceV);
|
||||
return true;
|
||||
}
|
||||
SAT['testCircleCircle'] = testCircleCircle;
|
||||
|
||||
// Check if a polygon and a circle collide.
|
||||
/**
|
||||
* @param {Polygon} polygon The polygon.
|
||||
* @param {Circle} circle The circle.
|
||||
* @param {Response=} response Response object (optional) that will be populated if
|
||||
* they interset.
|
||||
* @return {boolean} true if they intersect, false if they don't.
|
||||
*/
|
||||
function testPolygonCircle(polygon, circle, response) {
|
||||
// Get the position of the circle relative to the polygon.
|
||||
var circlePos = T_VECTORS.pop().copy(circle['pos']).sub(polygon['pos']);
|
||||
var radius = circle['r'];
|
||||
var radius2 = radius * radius;
|
||||
var points = polygon['points'];
|
||||
var len = points.length;
|
||||
var edge = T_VECTORS.pop();
|
||||
var point = T_VECTORS.pop();
|
||||
|
||||
// For each edge in the polygon:
|
||||
for (var i = 0; i < len; i++) {
|
||||
var next = i === len - 1 ? 0 : i + 1;
|
||||
var prev = i === 0 ? len - 1 : i - 1;
|
||||
var overlap = 0;
|
||||
var overlapN = null;
|
||||
|
||||
// Get the edge.
|
||||
edge.copy(polygon['edges'][i]);
|
||||
// Calculate the center of the circle relative to the starting point of the edge.
|
||||
point.copy(circlePos).sub(points[i]);
|
||||
|
||||
// If the distance between the center of the circle and the point
|
||||
// is bigger than the radius, the polygon is definitely not fully in
|
||||
// the circle.
|
||||
if (response && point.len2() > radius2) {
|
||||
response['aInB'] = false;
|
||||
}
|
||||
|
||||
// Calculate which Vornoi region the center of the circle is in.
|
||||
var region = vornoiRegion(edge, point);
|
||||
// If it's the left region:
|
||||
if (region === LEFT_VORNOI_REGION) {
|
||||
// We need to make sure we're in the RIGHT_VORNOI_REGION of the previous edge.
|
||||
edge.copy(polygon['edges'][prev]);
|
||||
// Calculate the center of the circle relative the starting point of the previous edge
|
||||
var point2 = T_VECTORS.pop().copy(circlePos).sub(points[prev]);
|
||||
region = vornoiRegion(edge, point2);
|
||||
if (region === RIGHT_VORNOI_REGION) {
|
||||
// It's in the region we want. Check if the circle intersects the point.
|
||||
var dist = point.len();
|
||||
if (dist > radius) {
|
||||
// No intersection
|
||||
T_VECTORS.push(circlePos);
|
||||
T_VECTORS.push(edge);
|
||||
T_VECTORS.push(point);
|
||||
T_VECTORS.push(point2);
|
||||
return false;
|
||||
} else if (response) {
|
||||
// It intersects, calculate the overlap.
|
||||
response['bInA'] = false;
|
||||
overlapN = point.normalize();
|
||||
overlap = radius - dist;
|
||||
}
|
||||
}
|
||||
T_VECTORS.push(point2);
|
||||
// If it's the right region:
|
||||
} else if (region === RIGHT_VORNOI_REGION) {
|
||||
// We need to make sure we're in the left region on the next edge
|
||||
edge.copy(polygon['edges'][next]);
|
||||
// Calculate the center of the circle relative to the starting point of the next edge.
|
||||
point.copy(circlePos).sub(points[next]);
|
||||
region = vornoiRegion(edge, point);
|
||||
if (region === LEFT_VORNOI_REGION) {
|
||||
// It's in the region we want. Check if the circle intersects the point.
|
||||
var dist = point.len();
|
||||
if (dist > radius) {
|
||||
// No intersection
|
||||
T_VECTORS.push(circlePos);
|
||||
T_VECTORS.push(edge);
|
||||
T_VECTORS.push(point);
|
||||
return false;
|
||||
} else if (response) {
|
||||
// It intersects, calculate the overlap.
|
||||
response['bInA'] = false;
|
||||
overlapN = point.normalize();
|
||||
overlap = radius - dist;
|
||||
}
|
||||
}
|
||||
// Otherwise, it's the middle region:
|
||||
} else {
|
||||
// Need to check if the circle is intersecting the edge,
|
||||
// Change the edge into its "edge normal".
|
||||
var normal = edge.perp().normalize();
|
||||
// Find the perpendicular distance between the center of the
|
||||
// circle and the edge.
|
||||
var dist = point.dot(normal);
|
||||
var distAbs = Math.abs(dist);
|
||||
// If the circle is on the outside of the edge, there is no intersection.
|
||||
if (dist > 0 && distAbs > radius) {
|
||||
// No intersection
|
||||
T_VECTORS.push(circlePos);
|
||||
T_VECTORS.push(normal);
|
||||
T_VECTORS.push(point);
|
||||
return false;
|
||||
} else if (response) {
|
||||
// It intersects, calculate the overlap.
|
||||
overlapN = normal;
|
||||
overlap = radius - dist;
|
||||
// If the center of the circle is on the outside of the edge, or part of the
|
||||
// circle is on the outside, the circle is not fully inside the polygon.
|
||||
if (dist >= 0 || overlap < 2 * radius) {
|
||||
response['bInA'] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If this is the smallest overlap we've seen, keep it.
|
||||
// (overlapN may be null if the circle was in the wrong Vornoi region).
|
||||
if (overlapN && response && Math.abs(overlap) < Math.abs(response['overlap'])) {
|
||||
response['overlap'] = overlap;
|
||||
response['overlapN'].copy(overlapN);
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate the final overlap vector - based on the smallest overlap.
|
||||
if (response) {
|
||||
response['a'] = polygon;
|
||||
response['b'] = circle;
|
||||
response['overlapV'].copy(response['overlapN']).scale(response['overlap']);
|
||||
}
|
||||
T_VECTORS.push(circlePos);
|
||||
T_VECTORS.push(edge);
|
||||
T_VECTORS.push(point);
|
||||
return true;
|
||||
}
|
||||
SAT['testPolygonCircle'] = testPolygonCircle;
|
||||
|
||||
// Check if a circle and a polygon collide.
|
||||
//
|
||||
// **NOTE:** This is slightly less efficient than polygonCircle as it just
|
||||
// runs polygonCircle and reverses everything at the end.
|
||||
/**
|
||||
* @param {Circle} circle The circle.
|
||||
* @param {Polygon} polygon The polygon.
|
||||
* @param {Response=} response Response object (optional) that will be populated if
|
||||
* they interset.
|
||||
* @return {boolean} true if they intersect, false if they don't.
|
||||
*/
|
||||
function testCirclePolygon(circle, polygon, response) {
|
||||
// Test the polygon against the circle.
|
||||
var result = testPolygonCircle(polygon, circle, response);
|
||||
if (result && response) {
|
||||
// Swap A and B in the response.
|
||||
var a = response['a'];
|
||||
var aInB = response['aInB'];
|
||||
response['overlapN'].reverse();
|
||||
response['overlapV'].reverse();
|
||||
response['a'] = response['b'];
|
||||
response['b'] = a;
|
||||
response['aInB'] = response['bInA'];
|
||||
response['bInA'] = aInB;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
SAT['testCirclePolygon'] = testCirclePolygon;
|
||||
|
||||
// Checks whether polygons collide.
|
||||
/**
|
||||
* @param {Polygon} a The first polygon.
|
||||
* @param {Polygon} b The second polygon.
|
||||
* @param {Response=} response Response object (optional) that will be populated if
|
||||
* they interset.
|
||||
* @return {boolean} true if they intersect, false if they don't.
|
||||
*/
|
||||
function testPolygonPolygon(a, b, response) {
|
||||
var aPoints = a['points'];
|
||||
var aLen = aPoints.length;
|
||||
var bPoints = b['points'];
|
||||
var bLen = bPoints.length;
|
||||
// If any of the edge normals of A is a separating axis, no intersection.
|
||||
for (var i = 0; i < aLen; i++) {
|
||||
if (isSeparatingAxis(a['pos'], b['pos'], aPoints, bPoints, a['normals'][i], response)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// If any of the edge normals of B is a separating axis, no intersection.
|
||||
for (var i = 0;i < bLen; i++) {
|
||||
if (isSeparatingAxis(a['pos'], b['pos'], aPoints, bPoints, b['normals'][i], response)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Since none of the edge normals of A or B are a separating axis, there is an intersection
|
||||
// and we've already calculated the smallest overlap (in isSeparatingAxis). Calculate the
|
||||
// final overlap vector.
|
||||
if (response) {
|
||||
response['a'] = a;
|
||||
response['b'] = b;
|
||||
response['overlapV'].copy(response['overlapN']).scale(response['overlap']);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
SAT['testPolygonPolygon'] = testPolygonPolygon;
|
||||
|
||||
return SAT;
|
||||
}));
|
|
@ -85,6 +85,7 @@
|
|||
$f = $_GET['f'];
|
||||
?>
|
||||
<script src="wip/<?php echo $f?>" type="text/javascript"></script>
|
||||
<script src="wip/SAT.js" type="text/javascript"></script>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
|
|
64
examples/wip/multiball.js
Normal file
64
examples/wip/multiball.js
Normal file
|
@ -0,0 +1,64 @@
|
|||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('arrow', 'assets/sprites/asteroids_ship.png');
|
||||
game.load.image('ball', 'assets/sprites/shinyball.png');
|
||||
|
||||
}
|
||||
|
||||
var sprites;
|
||||
var bmd;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#124184';
|
||||
|
||||
bmd = game.add.bitmapData(800, 600);
|
||||
bmd.fillStyle('#ffffff');
|
||||
var bg = game.add.sprite(0, 0, bmd);
|
||||
bg.body.moves = false;
|
||||
|
||||
game.physics.gravity.y = 250;
|
||||
|
||||
sprites = game.add.group();
|
||||
|
||||
for (var i = 0; i < 50; i++)
|
||||
{
|
||||
var s = sprites.create(game.rnd.integerInRange(100, 700), game.rnd.integerInRange(32, 200), 'ball');
|
||||
s.body.velocity.x = game.rnd.integerInRange(-400, 400);
|
||||
s.body.velocity.y = game.rnd.integerInRange(-200, 200);
|
||||
}
|
||||
|
||||
sprites.setAll('body.collideWorldBounds', true);
|
||||
sprites.setAll('body.bounce.x', 0.8);
|
||||
sprites.setAll('body.bounce.y', 0.8);
|
||||
sprites.setAll('body.minBounceVelocity', 0.8);
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
// game.physics.collide(sprites, sprites);
|
||||
|
||||
// sprite.rotation = sprite.body.angle;
|
||||
|
||||
// if (sprite)
|
||||
// {
|
||||
// bmd.fillStyle('#ffff00');
|
||||
// bmd.fillRect(sprite.body.center.x, sprite.body.center.y, 2, 2);
|
||||
// }
|
||||
|
||||
// if (sprite2)
|
||||
// {
|
||||
// bmd.fillStyle('#ff00ff');
|
||||
// bmd.fillRect(sprite2.body.center.x, sprite2.body.center.y, 2, 2);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
|
||||
}
|
29
examples/wip/pausetime.js
Normal file
29
examples/wip/pausetime.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create, render: render });
|
||||
|
||||
function create() {
|
||||
|
||||
game.onPause.add(onGamePause, this);
|
||||
game.onResume.add(onGameResume, this);
|
||||
|
||||
}
|
||||
|
||||
function onGamePause() {
|
||||
|
||||
console.log('game paused at ' + this.time.now);
|
||||
|
||||
}
|
||||
|
||||
function onGameResume() {
|
||||
|
||||
console.log('game un-paused at ' + this.time.now);
|
||||
console.log('was paused for ' + game.time.pauseDuration);
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
game.debug.renderText('now: ' + game.time.now, 32, 32);
|
||||
game.debug.renderText('paused: ' + game.paused, 32, 64);
|
||||
|
||||
}
|
137
examples/wip/sat1.js
Normal file
137
examples/wip/sat1.js
Normal file
|
@ -0,0 +1,137 @@
|
|||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.spritesheet('gameboy', 'assets/sprites/gameboy_seize_color_40x60.png', 40, 60);
|
||||
|
||||
}
|
||||
|
||||
var sprite;
|
||||
var sprite2;
|
||||
var sprite3;
|
||||
|
||||
var bmd;
|
||||
|
||||
var b1;
|
||||
var b2;
|
||||
var b3;
|
||||
|
||||
var r;
|
||||
|
||||
function create() {
|
||||
|
||||
// game.stage.backgroundColor = '#124184';
|
||||
game.stage.backgroundColor = '#000';
|
||||
|
||||
test1();
|
||||
|
||||
bmd = game.add.bitmapData(800, 600);
|
||||
var bg = game.add.sprite(0, 0, bmd);
|
||||
bg.body.moves = false;
|
||||
|
||||
bmd.clear();
|
||||
bmd.context.lineWidth = 1;
|
||||
|
||||
}
|
||||
|
||||
function test1() {
|
||||
|
||||
sprite = game.add.sprite(500, 400, 'gameboy', 0);
|
||||
sprite.name = 'red';
|
||||
sprite.body.bounce.x = 0.8;
|
||||
// sprite.body = null;
|
||||
|
||||
sprite2 = game.add.sprite(0, 400, 'gameboy', 2);
|
||||
sprite2.name = 'green';
|
||||
// sprite2.body = null;
|
||||
|
||||
sprite3 = game.add.sprite(700, 400, 'gameboy', 3);
|
||||
sprite3.name = 'yellow';
|
||||
// sprite3.body = null;
|
||||
|
||||
b1 = new SAT.Box(new SAT.Vector(sprite.x, sprite.y), sprite.width, sprite.height);
|
||||
b2 = new SAT.Box(new SAT.Vector(sprite2.x, sprite2.y), sprite2.width, sprite2.height);
|
||||
b3 = new SAT.Box(new SAT.Vector(sprite3.x, sprite3.y), sprite3.width, sprite3.height);
|
||||
|
||||
r = new SAT.Response();
|
||||
|
||||
console.log(b2);
|
||||
|
||||
game.input.onDown.add(launch1, this);
|
||||
|
||||
}
|
||||
|
||||
function launch1() {
|
||||
|
||||
// console.log(b1);
|
||||
|
||||
sprite.body.velocity.x = -200;
|
||||
// sprite2.body.velocity.x = -225;
|
||||
|
||||
}
|
||||
|
||||
|
||||
function update() {
|
||||
|
||||
b1.pos.x = sprite.x;
|
||||
b1.pos.y = sprite.y;
|
||||
|
||||
b2.pos.x = sprite2.x;
|
||||
b2.pos.y = sprite2.y;
|
||||
|
||||
b3.pos.x = sprite3.x;
|
||||
b3.pos.y = sprite3.y;
|
||||
|
||||
if (SAT.testPolygonPolygon(b1.toPolygon(), b2.toPolygon(), r))
|
||||
{
|
||||
console.log(r, b1.pos, b2.pos);
|
||||
|
||||
// b1.pos.sub(r.overlapV);
|
||||
// sprite.body.velocity.x *= -sprite.body.bounce.x;
|
||||
|
||||
sprite.body.velocity.x = 0;
|
||||
|
||||
sprite.x += Math.abs(r.overlapV.x) + 1;
|
||||
console.log('sprite moved to', sprite.x);
|
||||
// sprite.x = b1.pos.x;
|
||||
// sprite.y = b1.pos.y;
|
||||
}
|
||||
|
||||
bmd.clear();
|
||||
|
||||
if (sprite)
|
||||
{
|
||||
bmd.fillStyle('rgba(255,0,0,0.8');
|
||||
bmd.fillRect(b1.pos.x, b1.pos.y, b1.w, b1.h);
|
||||
}
|
||||
|
||||
if (sprite2)
|
||||
{
|
||||
bmd.fillStyle('rgba(0,255,0,0.8');
|
||||
bmd.fillRect(b2.pos.x, b2.pos.y, b2.w, b2.h);
|
||||
}
|
||||
|
||||
if (sprite3)
|
||||
{
|
||||
bmd.fillStyle('rgba(0,0,255,0.8');
|
||||
bmd.fillRect(b3.pos.x, b3.pos.y, b3.w, b3.h);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
if (sprite)
|
||||
{
|
||||
// game.debug.renderBodyInfo(sprite, 16, 24);
|
||||
game.debug.renderText(sprite.name + ' x: ' + sprite.x, 16, 500);
|
||||
}
|
||||
|
||||
if (sprite2)
|
||||
{
|
||||
// game.debug.renderBodyInfo(sprite2, 16, 190);
|
||||
game.debug.renderText(sprite2.name + ' x: ' + sprite2.x, 400, 500);
|
||||
}
|
||||
|
||||
}
|
|
@ -217,7 +217,7 @@ Phaser.Stage.prototype = {
|
|||
return;
|
||||
}
|
||||
|
||||
if (event.type == 'pagehide' || event.type == 'blur' || document['hidden'] === true || document['webkitHidden'] === true)
|
||||
if (this.game.paused === false && (event.type == 'pagehide' || event.type == 'blur' || document['hidden'] === true || document['webkitHidden'] === true))
|
||||
{
|
||||
this.game.paused = true;
|
||||
}
|
||||
|
|
|
@ -100,7 +100,7 @@ Phaser.Text = function (game, x, y, text, style) {
|
|||
/**
|
||||
* @property {Phaser.Point} cameraOffset - If this object is fixed to the camera then use this Point to specify how far away from the Camera x/y it's rendered.
|
||||
*/
|
||||
this.cameraOffset = new Phaser.Point();
|
||||
this.cameraOffset = new Phaser.Point(x, y);
|
||||
|
||||
/**
|
||||
* @property {object} _cache - A mini cache for storing all of the calculated values.
|
||||
|
|
|
@ -30,6 +30,16 @@ Phaser.Input = function (game) {
|
|||
* @default
|
||||
*/
|
||||
this.hitContext = null;
|
||||
|
||||
/**
|
||||
* @property {function} moveCallback - An optional callback that will be fired every time the activePointer receives a move event from the DOM. Set to null to disable.
|
||||
*/
|
||||
this.moveCallback = null;
|
||||
|
||||
/**
|
||||
* @property {object} moveCallbackContext - The context in which the moveCallback will be sent. Defaults to Phaser.Input but can be set to any valid JS object.
|
||||
*/
|
||||
this.moveCallbackContext = this;
|
||||
|
||||
};
|
||||
|
||||
|
@ -404,6 +414,23 @@ Phaser.Input.prototype = {
|
|||
this.mspointer.stop();
|
||||
this.gamepad.stop();
|
||||
|
||||
this.moveCallback = null;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets a callback that is fired every time the activePointer receives a DOM move event such as a mousemove or touchmove.
|
||||
* It will be called every time the activePointer moves, which in a multi-touch game can be a lot of times, so this is best
|
||||
* to only use if you've limited input to a single pointer (i.e. mouse or touch)
|
||||
* @method Phaser.Input#setMoveCallback
|
||||
* @param {function} callback - The callback that will be called each time the activePointer receives a DOM move event.
|
||||
* @param {object} callbackContext - The context in which the callback will be called.
|
||||
*/
|
||||
setMoveCallback: function (callback, callbackContext) {
|
||||
|
||||
this.moveCallback = callback;
|
||||
this.moveCallbackContext = callbackContext;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -257,7 +257,7 @@ Phaser.Pointer.prototype = {
|
|||
},
|
||||
|
||||
/**
|
||||
* Called internall by the Input Manager.
|
||||
* Called by the Input Manager.
|
||||
* @method Phaser.Pointer#update
|
||||
*/
|
||||
update: function () {
|
||||
|
@ -336,12 +336,17 @@ Phaser.Pointer.prototype = {
|
|||
this.game.input.circle.y = this.game.input.y;
|
||||
}
|
||||
|
||||
// If the game is paused we don't process any target objects
|
||||
// If the game is paused we don't process any target objects or callbacks
|
||||
if (this.game.paused)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
if (this.game.input.moveCallback)
|
||||
{
|
||||
this.game.input.moveCallback.call(this.game.input.moveCallbackContext, this, this.x, this.y);
|
||||
}
|
||||
|
||||
// Easy out if we're dragging something and it still exists
|
||||
if (this.targetObject !== null && this.targetObject.isDragged === true)
|
||||
{
|
||||
|
|
|
@ -327,7 +327,7 @@ Phaser.Math = {
|
|||
},
|
||||
|
||||
/**
|
||||
* Find the angle of a segment from (x1, y1) -> (x2, y2 ).
|
||||
* Find the angle of a segment from (x1, y1) -> (x2, y2).
|
||||
* @method Phaser.Math#angleBetween
|
||||
* @param {number} x1
|
||||
* @param {number} y1
|
||||
|
@ -971,7 +971,7 @@ Phaser.Math = {
|
|||
* @param {number} y1
|
||||
* @param {number} x2
|
||||
* @param {number} y2
|
||||
* @return {number} The distance between this Point object and the destination Point object.
|
||||
* @return {number} The distance between the two sets of coordinates.
|
||||
*/
|
||||
distance: function (x1, y1, x2, y2) {
|
||||
|
||||
|
@ -982,6 +982,25 @@ Phaser.Math = {
|
|||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the distance between the two given set of coordinates at the power given.
|
||||
*
|
||||
* @method Phaser.Math#distancePow
|
||||
* @param {number} x1
|
||||
* @param {number} y1
|
||||
* @param {number} x2
|
||||
* @param {number} y2
|
||||
* @param {number} [pow=2]
|
||||
* @return {number} The distance between the two sets of coordinates.
|
||||
*/
|
||||
distancePow: function (x1, y1, x2, y2, pow) {
|
||||
|
||||
if (typeof pow === 'undefined') { pow = 2; }
|
||||
|
||||
return Math.sqrt(Math.pow(x2 - x1, pow) + Math.pow(y2 - y1, pow));
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the rounded distance between the two given set of coordinates.
|
||||
*
|
||||
|
|
|
@ -310,14 +310,15 @@ Phaser.Physics.Arcade.prototype = {
|
|||
|
||||
/**
|
||||
* Checks for collision between two game objects. You can perform Sprite vs. Sprite, Sprite vs. Group, Group vs. Group, Sprite vs. Tilemap Layer or Group vs. Tilemap Layer collisions.
|
||||
* If you'd like to collide an array of objects see Phaser.Physics.Arcade#collideArray.
|
||||
* The objects are also automatically separated. If you don't require separation then use ArcadePhysics.overlap instead.
|
||||
* An optional processCallback can be provided. If given this function will be called when two sprites are found to be colliding. It is called before any separation takes place,
|
||||
* giving you the chance to perform additional checks. If the function returns true then the collision and separation is carried out. If it returns false it is skipped.
|
||||
* The collideCallback is an optional function that is only called if two sprites collide. If a processCallback has been set then it needs to return true for collideCallback to be called.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#collide
|
||||
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap
|
||||
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object2 - The second object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap
|
||||
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap.
|
||||
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object2 - The second object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap.
|
||||
* @param {function} [collideCallback=null] - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
|
||||
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
|
||||
* @param {object} [callbackContext] - The context in which to run the callbacks.
|
||||
|
@ -332,6 +333,58 @@ Phaser.Physics.Arcade.prototype = {
|
|||
this._result = false;
|
||||
this._total = 0;
|
||||
|
||||
this.collideHandler(object1, object2, collideCallback, processCallback, callbackContext);
|
||||
|
||||
return (this._total > 0);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Checks for collision between a game object and an array of game objects. You can perform Sprite vs. Sprite, Sprite vs. Group, Group vs. Group, Sprite vs. Tilemap Layer or Group vs. Tilemap Layer collisions.
|
||||
* The objects are also automatically separated. If you don't require separation then use ArcadePhysics.overlap instead.
|
||||
* An optional processCallback can be provided. If given this function will be called when two sprites are found to be colliding. It is called before any separation takes place,
|
||||
* giving you the chance to perform additional checks. If the function returns true then the collision and separation is carried out. If it returns false it is skipped.
|
||||
* The collideCallback is an optional function that is only called if two sprites collide. If a processCallback has been set then it needs to return true for collideCallback to be called.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#collideArray
|
||||
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap.
|
||||
* @param {<Phaser.Sprite>array|<Phaser.Group>array|<Phaser.Particles.Emitter>array|<Phaser.Tilemap>array} objectArray - An array of objects to check. Can contain instances of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap.
|
||||
* @param {function} [collideCallback=null] - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
|
||||
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
|
||||
* @param {object} [callbackContext] - The context in which to run the callbacks.
|
||||
* @returns {boolean} True if a collision occured otherwise false.
|
||||
*/
|
||||
collideArray: function (object1, objectArray, collideCallback, processCallback, callbackContext) {
|
||||
|
||||
collideCallback = collideCallback || null;
|
||||
processCallback = processCallback || null;
|
||||
callbackContext = callbackContext || collideCallback;
|
||||
|
||||
this._result = false;
|
||||
this._total = 0;
|
||||
|
||||
for (var i = 0, len = objectArray.length; i < len; i++)
|
||||
{
|
||||
this.collideHandler(object1, objectArray[i], collideCallback, processCallback, callbackContext);
|
||||
}
|
||||
|
||||
return (this._total > 0);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal collision handler.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#collideHandler
|
||||
* @private
|
||||
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap.
|
||||
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object2 - The second object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap. Can also be an array of objects to check.
|
||||
* @param {function} [collideCallback=null] - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
|
||||
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
|
||||
* @param {object} [callbackContext] - The context in which to run the callbacks.
|
||||
*/
|
||||
collideHandler: function (object1, object2, collideCallback, processCallback, callbackContext) {
|
||||
|
||||
// Only collide valid objects
|
||||
if (object1 && object2 && object1.exists && object2.exists)
|
||||
{
|
||||
|
@ -397,8 +450,6 @@ Phaser.Physics.Arcade.prototype = {
|
|||
}
|
||||
}
|
||||
|
||||
return (this._total > 0);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -595,6 +646,36 @@ Phaser.Physics.Arcade.prototype = {
|
|||
return false;
|
||||
}
|
||||
|
||||
var r = false;
|
||||
|
||||
if (body1.deltaX() === 0 && body2.deltaX() === 0)
|
||||
{
|
||||
// They overlap but neither of them are moving
|
||||
body1.embedded = true;
|
||||
body2.embedded = true;
|
||||
}
|
||||
else if (body1.deltaX() != 0 && body2.deltaX() === 0)
|
||||
{
|
||||
r = body1.separate(body2);
|
||||
}
|
||||
else if (body2.deltaX() != 0 && body1.deltaX() === 0)
|
||||
{
|
||||
r = body2.separate(body1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Dual motion thingy
|
||||
r = body1.separate(body2);
|
||||
}
|
||||
|
||||
return r;
|
||||
|
||||
|
||||
|
||||
// return body2.separate(body1);
|
||||
|
||||
/*
|
||||
|
||||
// We got this far, so the bodies overlap and our process was good, which means we can separate ...
|
||||
this._overlap = 0;
|
||||
|
||||
|
@ -900,6 +981,7 @@ Phaser.Physics.Arcade.prototype = {
|
|||
}
|
||||
|
||||
return true;
|
||||
*/
|
||||
|
||||
},
|
||||
|
||||
|
|
|
@ -137,7 +137,7 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
|||
/**
|
||||
* @property {number} minBounceVelocity - The minimum bounce velocity (could just be the bounce value?).
|
||||
*/
|
||||
this.minBounceVelocity = 0.5;
|
||||
// this.minBounceVelocity = 0.5;
|
||||
|
||||
this._debug = 0;
|
||||
|
||||
|
@ -151,6 +151,12 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
|||
*/
|
||||
this.bounce = new Phaser.Point();
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} minVelocity - When a body rebounds off another the minVelocity is checked, if the new velocity is lower than the minVelocity the body is stopped.
|
||||
* @default
|
||||
*/
|
||||
this.minVelocity = new Phaser.Point(20, 20);
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} maxVelocity - The maximum velocity in pixels per second sq. that the Body can reach.
|
||||
* @default
|
||||
|
@ -200,6 +206,7 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
|||
* @property {object} touching - An object containing touching results.
|
||||
*/
|
||||
this.touching = { none: true, up: false, down: false, left: false, right: false };
|
||||
this.touchingPoint = new Phaser.Point(this.x, this.y);
|
||||
|
||||
/**
|
||||
* This object is populated with previous touching values from the bodies previous collision.
|
||||
|
@ -295,6 +302,7 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
|||
* @property {object} blocked - An object containing on which faces this Body is blocked from moving, if any.
|
||||
*/
|
||||
this.blocked = { up: false, down: false, left: false, right: false };
|
||||
this.blockedPoint = new Phaser.Point();
|
||||
|
||||
/**
|
||||
* @property {number} _dx - Internal cache var.
|
||||
|
@ -360,14 +368,6 @@ Phaser.Physics.Arcade.Body.prototype = {
|
|||
this.wasTouching.left = this.touching.left;
|
||||
this.wasTouching.right = this.touching.right;
|
||||
|
||||
this.touching.none = true;
|
||||
this.touching.up = false;
|
||||
this.touching.down = false;
|
||||
this.touching.left = false;
|
||||
this.touching.right = false;
|
||||
|
||||
this.embedded = false;
|
||||
|
||||
this.screenX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
|
||||
this.screenY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
|
||||
|
||||
|
@ -375,18 +375,32 @@ Phaser.Physics.Arcade.Body.prototype = {
|
|||
this.preY = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
|
||||
this.preRotation = this.sprite.angle;
|
||||
|
||||
this.x = this.preX;
|
||||
this.y = this.preY;
|
||||
this.rotation = this.preRotation;
|
||||
|
||||
this.speed = Math.sqrt(this.velocity.x * this.velocity.x + this.velocity.y * this.velocity.y);
|
||||
this.angle = Math.atan2(this.velocity.y, this.velocity.x);
|
||||
|
||||
// This all needs to move - because a body may start the preUpdate already touching something
|
||||
// See if we can reduce this down?
|
||||
this.blocked.up = false;
|
||||
this.blocked.down = false;
|
||||
this.blocked.left = false;
|
||||
this.blocked.right = false;
|
||||
|
||||
this.embedded = false;
|
||||
|
||||
this.x = this.preX;
|
||||
this.y = this.preY;
|
||||
this.rotation = this.preRotation;
|
||||
|
||||
// if (this.deltaX() == 0 && (this.touchingPoint.x !== this.x || this.touchingPoint.y !== this.y))
|
||||
// {
|
||||
// console.log('touch reset', this.x, this.y, this.touchingPoint);
|
||||
// this.touching.none = true;
|
||||
// this.touching.up = false;
|
||||
// this.touching.down = false;
|
||||
// this.touching.left = false;
|
||||
// this.touching.right = false;
|
||||
// }
|
||||
|
||||
this.speed = Math.sqrt(this.velocity.x * this.velocity.x + this.velocity.y * this.velocity.y);
|
||||
this.angle = Math.atan2(this.velocity.y, this.velocity.x);
|
||||
|
||||
this._debug++;
|
||||
|
||||
if (this.moves)
|
||||
|
@ -403,6 +417,44 @@ Phaser.Physics.Arcade.Body.prototype = {
|
|||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal method used to check the Body against the World Bounds.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#checkWorldBounds
|
||||
* @protected
|
||||
*/
|
||||
checkWorldBounds: function () {
|
||||
|
||||
if (this.x <= this.game.world.bounds.x)
|
||||
{
|
||||
this.overlapX = this.game.world.bounds.x - this.x;
|
||||
this.blocked.left = true;
|
||||
// console.log(this._debug, 'cwl', this.overlapX, this.x, this.game.world.bounds.x);
|
||||
}
|
||||
else if (this.right >= this.game.world.bounds.right)
|
||||
{
|
||||
this.overlapX = this.right - this.game.world.bounds.right;
|
||||
this.blocked.right = true;
|
||||
// console.log(this._debug, 'cwr', this.overlapX, this.x, this.game.world.bounds.x);
|
||||
}
|
||||
|
||||
if (this.y <= this.game.world.bounds.y)
|
||||
{
|
||||
this.overlapY = this.game.world.bounds.y - this.y;
|
||||
this.blocked.up = true;
|
||||
// console.log(this._debug, 'cwu', this.overlapY, this.y, this.height, this.bottom, this.game.world.bounds.bottom);
|
||||
}
|
||||
else if (this.bottom >= this.game.world.bounds.bottom)
|
||||
{
|
||||
this.overlapY = this.bottom - this.game.world.bounds.bottom;
|
||||
this.blocked.down = true;
|
||||
// console.log(this._debug, 'cwd', this.overlapY, this.y, this.height, this.bottom, this.game.world.bounds.bottom);
|
||||
}
|
||||
|
||||
this.blockedPoint.setTo(this.x, this.y);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal method.
|
||||
*
|
||||
|
@ -438,7 +490,8 @@ Phaser.Physics.Arcade.Body.prototype = {
|
|||
|
||||
this._dx = this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
|
||||
|
||||
if (this._dx > this.minBounceVelocity)
|
||||
// if (this._dx > this.minBounceVelocity)
|
||||
if (Math.abs(this.velocity.x) > this.minVelocity.x)
|
||||
{
|
||||
this.x += this._dx;
|
||||
this.velocity.x += this.motionVelocity.x;
|
||||
|
@ -493,7 +546,8 @@ Phaser.Physics.Arcade.Body.prototype = {
|
|||
|
||||
this._dy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
|
||||
|
||||
if (this._dy > this.minBounceVelocity)
|
||||
// if (this._dy > this.minBounceVelocity)
|
||||
if (Math.abs(this.velocity.y) > this.minVelocity.y)
|
||||
{
|
||||
this.y += this._dy;
|
||||
this.velocity.y += this.motionVelocity.y;
|
||||
|
@ -516,7 +570,8 @@ Phaser.Physics.Arcade.Body.prototype = {
|
|||
|
||||
this._dy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
|
||||
|
||||
if (this._dy < -this.minBounceVelocity)
|
||||
// if (this._dy < -this.minBounceVelocity)
|
||||
if (Math.abs(this.velocity.y) > this.minVelocity.y)
|
||||
{
|
||||
this.y += this._dy;
|
||||
this.velocity.y += this.motionVelocity.y;
|
||||
|
@ -557,6 +612,145 @@ Phaser.Physics.Arcade.Body.prototype = {
|
|||
|
||||
},
|
||||
|
||||
// This body is the one that is checking against 'body'
|
||||
separateX: function (body) {
|
||||
|
||||
// Doesn't matter if this is moving or not
|
||||
console.log('--- separateX -----------------------------------------------------------------------------');
|
||||
console.log(this.sprite.name, 'testing against', body.sprite.name);
|
||||
console.log(this.sprite.name, 'moving left:', (this.deltaX() < 0), 'moving right:', (this.deltaX() > 0));
|
||||
|
||||
this.overlapX = 0;
|
||||
|
||||
if (this.x < body.x)
|
||||
{
|
||||
this.overlapX = this.right - body.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.overlapX = body.right - this.x;
|
||||
}
|
||||
|
||||
// There are 5 possible collisions taking place
|
||||
// if (this.deltaX() === 0 && body.deltaX())
|
||||
|
||||
// if (this.deltaX() < 0 )
|
||||
|
||||
// if (this.deltaX() < 0 && this.allowCollision.left && body.allowCollision.right)
|
||||
|
||||
// This body is moving, the body it collided with is not
|
||||
|
||||
},
|
||||
|
||||
separate: function (body) {
|
||||
|
||||
console.log('-----------------------------------------------------------------------------');
|
||||
console.log(this.sprite.name, 'testing against', body.sprite.name);
|
||||
console.log(this.sprite.name, 'moving left:', (this.deltaX() < 0), 'moving right:', (this.deltaX() > 0));
|
||||
|
||||
this.overlapX = 0;
|
||||
this.overlapY = 0;
|
||||
|
||||
if (this.deltaX() < 0 && this.allowCollision.left && body.allowCollision.right)
|
||||
{
|
||||
// LEFT (will create a negative overlapX value)
|
||||
this.overlapX = this.x - body.right;
|
||||
console.log(this.sprite.name, 'check left', body.right, this.overlapX);
|
||||
}
|
||||
else if (this.deltaX() > 0 && this.allowCollision.right && body.allowCollision.left)
|
||||
{
|
||||
// RIGHT (will create a positive overlapX value)
|
||||
this.overlapX = this.right - body.x;
|
||||
console.log(this.sprite.name, 'check right', this.overlapX);
|
||||
}
|
||||
|
||||
if (this.deltaY() < 0 && this.allowCollision.up && body.allowCollision.down)
|
||||
{
|
||||
// UP (will create a negative overlapY value)
|
||||
this.overlapY = this.y - body.bottom;
|
||||
}
|
||||
else if (this.deltaY() > 0 && this.allowCollision.down && body.allowCollision.up)
|
||||
{
|
||||
// DOWN
|
||||
this.overlapY = this.bottom - body.y;
|
||||
}
|
||||
|
||||
if (this.overlapX === 0 && this.overlapY === 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.overlapHandler(this.overlapX, this.overlapY, body);
|
||||
body.overlapHandler(this.overlapX, this.overlapY, this);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
overlapHandler: function (x, y, body) {
|
||||
|
||||
if (x !== 0)
|
||||
{
|
||||
// Both moving, so let's cut the overlap in half
|
||||
if (this.deltaX() != 0 && body.deltaX() != 0)
|
||||
{
|
||||
console.log(this.sprite.name, 'both overlap, halving');
|
||||
x * 0.5;
|
||||
}
|
||||
|
||||
if (this.deltaX() !== 0)
|
||||
{
|
||||
// Not moving, caught impact from another object
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
if (this.deltaX() < 0 && !this.blocked.right)
|
||||
{
|
||||
// This body is moving left
|
||||
this.x += x;
|
||||
console.log(this.sprite.name, 'touch h-left', this.x, x);
|
||||
}
|
||||
else if (this.deltaX() > 0 && !this.body.blocked.left)
|
||||
{
|
||||
this.x -= x;
|
||||
console.log(this.sprite.name, 'touch h-right', this.x);
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
|
||||
if (this.bounce.x === 0)
|
||||
{
|
||||
this.velocity.x = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.velocity.x = -this.velocity.x * this.bounce.x;
|
||||
|
||||
if (Math.abs(this.velocity.x) < this.minVelocity.x)
|
||||
{
|
||||
this.velocity.x = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.sprite.x += this.deltaX();
|
||||
this.sprite.y += this.deltaY();
|
||||
|
||||
console.log(this.sprite.name, 'separate finished. Sprite.x', this.sprite.x, 'body x', this.x);
|
||||
|
||||
// reset delta ready for next collision
|
||||
this.preX = this.x;
|
||||
this.preY = this.y;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal method. This is called directly before the sprites are sent to the renderer.
|
||||
*
|
||||
|
@ -567,27 +761,28 @@ Phaser.Physics.Arcade.Body.prototype = {
|
|||
|
||||
if (this.moves)
|
||||
{
|
||||
if (this.deltaX() < 0)
|
||||
{
|
||||
this.facing = Phaser.LEFT;
|
||||
this.sprite.x += this.deltaX();
|
||||
}
|
||||
else if (this.deltaX() > 0)
|
||||
{
|
||||
this.facing = Phaser.RIGHT;
|
||||
this.sprite.x += this.deltaX();
|
||||
}
|
||||
// if (this.deltaX() < 0)
|
||||
// {
|
||||
// this.facing = Phaser.LEFT;
|
||||
// }
|
||||
// else if (this.deltaX() > 0)
|
||||
// {
|
||||
// this.facing = Phaser.RIGHT;
|
||||
// this.sprite.x += this.deltaX();
|
||||
// }
|
||||
|
||||
if (this.deltaY() < 0)
|
||||
{
|
||||
this.facing = Phaser.UP;
|
||||
this.sprite.y += this.deltaY();
|
||||
}
|
||||
else if (this.deltaY() > 0)
|
||||
{
|
||||
this.facing = Phaser.DOWN;
|
||||
this.sprite.y += this.deltaY();
|
||||
}
|
||||
// if (this.deltaY() < 0)
|
||||
// {
|
||||
// this.facing = Phaser.UP;
|
||||
// this.sprite.y += this.deltaY();
|
||||
// }
|
||||
// else if (this.deltaY() > 0)
|
||||
// {
|
||||
// this.facing = Phaser.DOWN;
|
||||
// }
|
||||
|
||||
this.sprite.x += this.deltaX();
|
||||
this.sprite.y += this.deltaY();
|
||||
|
||||
this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight);
|
||||
|
||||
|
@ -599,41 +794,6 @@ Phaser.Physics.Arcade.Body.prototype = {
|
|||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal method used to check the Body against the World Bounds.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#checkWorldBounds
|
||||
* @protected
|
||||
*/
|
||||
checkWorldBounds: function () {
|
||||
|
||||
if (this.x < this.game.world.bounds.x)
|
||||
{
|
||||
this.overlapX = this.game.world.bounds.x - this.x;
|
||||
this.blocked.left = true;
|
||||
// console.log(this._debug, 'cwl', this.overlapX, this.x, this.game.world.bounds.x);
|
||||
}
|
||||
else if (this.right > this.game.world.bounds.right)
|
||||
{
|
||||
this.overlapX = this.right - this.game.world.bounds.right;
|
||||
this.blocked.right = true;
|
||||
// console.log(this._debug, 'cwr', this.overlapX, this.x, this.game.world.bounds.x);
|
||||
}
|
||||
|
||||
if (this.y < this.game.world.bounds.y)
|
||||
{
|
||||
this.overlapY = this.game.world.bounds.y - this.y;
|
||||
this.blocked.up = true;
|
||||
// console.log(this._debug, 'cwu', this.overlapY, this.y, this.height, this.bottom, this.game.world.bounds.bottom);
|
||||
}
|
||||
else if (this.bottom > this.game.world.bounds.bottom)
|
||||
{
|
||||
this.overlapY = this.bottom - this.game.world.bounds.bottom;
|
||||
this.blocked.down = true;
|
||||
// console.log(this._debug, 'cwd', this.overlapY, this.y, this.height, this.bottom, this.game.world.bounds.bottom);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* You can modify the size of the physics Body to be any dimension you need.
|
||||
|
|
|
@ -20,32 +20,32 @@ Phaser.Time = function (game) {
|
|||
this.game = game;
|
||||
|
||||
/**
|
||||
* @property {number} physicsElapsed - The elapsed time calculated for the physics motion updates.
|
||||
*/
|
||||
this.physicsElapsed = 0;
|
||||
|
||||
/**
|
||||
* @property {number} time - Game time counter.
|
||||
* @property {number} time - Game time counter. If you need a value for in-game calculation please use Phaser.Time.now instead.
|
||||
* @protected
|
||||
*/
|
||||
this.time = 0;
|
||||
|
||||
/**
|
||||
* @property {number} pausedTime - Records how long the game has been paused for. Is reset each time the game pauses.
|
||||
*/
|
||||
this.pausedTime = 0;
|
||||
|
||||
/**
|
||||
* @property {number} now - The time right now.
|
||||
* @protected
|
||||
*/
|
||||
this.now = 0;
|
||||
|
||||
/**
|
||||
* @property {number} elapsed - Elapsed time since the last frame (in ms).
|
||||
* @protected
|
||||
*/
|
||||
this.elapsed = 0;
|
||||
|
||||
/**
|
||||
* @property {number} pausedTime - Records how long the game has been paused for. Is reset each time the game pauses.
|
||||
* @protected
|
||||
*/
|
||||
this.pausedTime = 0;
|
||||
|
||||
/**
|
||||
* @property {number} fps - Frames per second.
|
||||
* @protected
|
||||
*/
|
||||
this.fps = 0;
|
||||
|
||||
|
@ -70,6 +70,11 @@ Phaser.Time = function (game) {
|
|||
*/
|
||||
this.msMax = 0;
|
||||
|
||||
/**
|
||||
* @property {number} physicsElapsed - The elapsed time calculated for the physics motion updates.
|
||||
*/
|
||||
this.physicsElapsed = 0;
|
||||
|
||||
/**
|
||||
* @property {number} frames - The number of frames record in the last second.
|
||||
*/
|
||||
|
|
|
@ -75,6 +75,18 @@ Phaser.Utils = {
|
|||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns true if the object is an Array, otherwise false.
|
||||
* @method Phaser.Utils.isArray
|
||||
* @param {object} obj - The object to inspect.
|
||||
* @return {boolean} - true if the object is an array, otherwise false.
|
||||
*/
|
||||
isArray: function (obj) {
|
||||
|
||||
return toString.call(obj) === "[object Array]";
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* This is a slightly modified version of jQuery.isPlainObject. A plain object is an object whose internal class property is [object Object].
|
||||
* @method Phaser.Utils.isPlainObject
|
||||
|
|
Loading…
Reference in a new issue