mirror of
https://github.com/wassname/phaser.git
synced 2026-09-12 12:40:47 +08:00
All physics projections added.
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
var Shapes;
|
||||
(function (Shapes) {
|
||||
|
||||
var Point = Shapes.Point = (function () {
|
||||
function Point(x, y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
Point.prototype.getDist = function () {
|
||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||
};
|
||||
Point.origin = new Point(0, 0);
|
||||
return Point;
|
||||
})();
|
||||
|
||||
})(Shapes || (Shapes = {}));
|
||||
|
||||
var p = new Shapes.Point(3, 4);
|
||||
var dist = p.getDist();
|
||||
@@ -0,0 +1,128 @@
|
||||
/// <reference path="../../_definitions.ts" />
|
||||
|
||||
/**
|
||||
* Phaser - Physics - Projection
|
||||
*/
|
||||
|
||||
module Phaser.Physics.Projection {
|
||||
|
||||
export class AABB22Deg {
|
||||
|
||||
public static CollideS(x: number, y: number, obj: Phaser.Physics.AABB, t: Phaser.Physics.TileMapCell) {
|
||||
|
||||
var signx = t.signx;
|
||||
var signy = t.signy;
|
||||
|
||||
//first we need to check to make sure we're colliding with the slope at all
|
||||
var py = obj.pos.y - (signy * obj.yw);
|
||||
var penY = t.pos.y - py;//this is the vector from the innermost point on the box to the highest point on
|
||||
//the tile; if it is positive, this means the box is above the tile and
|
||||
//no collision is occuring
|
||||
|
||||
if (0 < (penY * signy))
|
||||
{
|
||||
|
||||
var ox = (obj.pos.x - (signx * obj.xw)) - (t.pos.x + (signx * t.xw));//this gives is the coordinates of the innermost
|
||||
var oy = (obj.pos.y - (signy * obj.yw)) - (t.pos.y - (signy * t.yw));//point on the AABB, relative to a point on the slope
|
||||
|
||||
var sx = t.sx;//get slope unit normal
|
||||
var sy = t.sy;
|
||||
|
||||
//if the dotprod of (ox,oy) and (sx,sy) is negative, the corner is in the slope
|
||||
//and we need toproject it out by the magnitude of the projection of (ox,oy) onto (sx,sy)
|
||||
var dp = (ox * sx) + (oy * sy);
|
||||
|
||||
if (dp < 0)
|
||||
{
|
||||
//collision; project delta onto slope and use this to displace the object
|
||||
sx *= -dp;//(sx,sy) is now the projection vector
|
||||
sy *= -dp;
|
||||
|
||||
var lenN = Math.sqrt(sx * sx + sy * sy);
|
||||
var lenP = Math.sqrt(x * x + y * y);
|
||||
|
||||
var aY = Math.abs(penY);
|
||||
if (lenP < lenN)
|
||||
{
|
||||
if (aY < lenP)
|
||||
{
|
||||
obj.reportCollisionVsWorld(0, penY, 0, penY / aY, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_OTHER;
|
||||
}
|
||||
else
|
||||
{
|
||||
obj.reportCollisionVsWorld(x, y, x / lenP, y / lenP, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_AXIS;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (aY < lenN)
|
||||
{
|
||||
obj.reportCollisionVsWorld(0, penY, 0, penY / aY, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_OTHER;
|
||||
}
|
||||
else
|
||||
{
|
||||
obj.reportCollisionVsWorld(sx, sy, t.sx, t.sy, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_OTHER;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//if we've reached this point, no collision has occured
|
||||
return Phaser.Physics.AABB.COL_NONE;
|
||||
|
||||
}
|
||||
|
||||
public static CollideB(x: number, y: number, obj: Phaser.Physics.AABB, t: Phaser.Physics.TileMapCell) {
|
||||
|
||||
var signx = t.signx;
|
||||
var signy = t.signy;
|
||||
|
||||
var ox = (obj.pos.x - (signx * obj.xw)) - (t.pos.x - (signx * t.xw));//this gives is the coordinates of the innermost
|
||||
var oy = (obj.pos.y - (signy * obj.yw)) - (t.pos.y + (signy * t.yw));//point on the AABB, relative to a point on the slope
|
||||
|
||||
var sx = t.sx;//get slope unit normal
|
||||
var sy = t.sy;
|
||||
|
||||
//if the dotprod of (ox,oy) and (sx,sy) is negative, the corner is in the slope
|
||||
//and we need toproject it out by the magnitude of the projection of (ox,oy) onto (sx,sy)
|
||||
var dp = (ox * sx) + (oy * sy);
|
||||
|
||||
if (dp < 0)
|
||||
{
|
||||
//collision; project delta onto slope and use this to displace the object
|
||||
sx *= -dp;//(sx,sy) is now the projection vector
|
||||
sy *= -dp;
|
||||
|
||||
var lenN = Math.sqrt(sx * sx + sy * sy);
|
||||
var lenP = Math.sqrt(x * x + y * y);
|
||||
|
||||
if (lenP < lenN)
|
||||
{
|
||||
obj.reportCollisionVsWorld(x, y, x / lenP, y / lenP, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_AXIS;
|
||||
}
|
||||
else
|
||||
{
|
||||
obj.reportCollisionVsWorld(sx, sy, t.sx, t.sy, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_OTHER;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return Phaser.Physics.AABB.COL_NONE;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
var Shapes;
|
||||
(function (Shapes) {
|
||||
|
||||
var Point = Shapes.Point = (function () {
|
||||
function Point(x, y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
Point.prototype.getDist = function () {
|
||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||
};
|
||||
Point.origin = new Point(0, 0);
|
||||
return Point;
|
||||
})();
|
||||
|
||||
})(Shapes || (Shapes = {}));
|
||||
|
||||
var p = new Shapes.Point(3, 4);
|
||||
var dist = p.getDist();
|
||||
@@ -0,0 +1,54 @@
|
||||
/// <reference path="../../_definitions.ts" />
|
||||
|
||||
/**
|
||||
* Phaser - Physics - Projection
|
||||
*/
|
||||
|
||||
module Phaser.Physics.Projection {
|
||||
|
||||
export class AABB45Deg {
|
||||
|
||||
public static Collide(x: number, y: number, obj: Phaser.Physics.AABB, t: Phaser.Physics.TileMapCell) {
|
||||
|
||||
var signx = t.signx;
|
||||
var signy = t.signy;
|
||||
|
||||
var ox = (obj.pos.x - (signx * obj.xw)) - t.pos.x;//this gives is the coordinates of the innermost
|
||||
var oy = (obj.pos.y - (signy * obj.yw)) - t.pos.y;//point on the AABB, relative to the tile center
|
||||
|
||||
var sx = t.sx;
|
||||
var sy = t.sy;
|
||||
|
||||
//if the dotprod of (ox,oy) and (sx,sy) is negative, the corner is in the slope
|
||||
//and we need toproject it out by the magnitude of the projection of (ox,oy) onto (sx,sy)
|
||||
var dp = (ox * sx) + (oy * sy);
|
||||
|
||||
if (dp < 0)
|
||||
{
|
||||
//collision; project delta onto slope and use this to displace the object
|
||||
sx *= -dp;//(sx,sy) is now the projection vector
|
||||
sy *= -dp;
|
||||
|
||||
var lenN = Math.sqrt(sx * sx + sy * sy);
|
||||
var lenP = Math.sqrt(x * x + y * y);
|
||||
|
||||
if (lenP < lenN)
|
||||
{
|
||||
//project along axis
|
||||
obj.reportCollisionVsWorld(x, y, x / lenP, y / lenP, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_AXIS;
|
||||
}
|
||||
else
|
||||
{
|
||||
//project along slope
|
||||
obj.reportCollisionVsWorld(sx, sy, t.sx, t.sy);
|
||||
|
||||
return Phaser.Physics.AABB.COL_OTHER;
|
||||
}
|
||||
}
|
||||
|
||||
return Phaser.Physics.AABB.COL_NONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
var Shapes;
|
||||
(function (Shapes) {
|
||||
|
||||
var Point = Shapes.Point = (function () {
|
||||
function Point(x, y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
Point.prototype.getDist = function () {
|
||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||
};
|
||||
Point.origin = new Point(0, 0);
|
||||
return Point;
|
||||
})();
|
||||
|
||||
})(Shapes || (Shapes = {}));
|
||||
|
||||
var p = new Shapes.Point(3, 4);
|
||||
var dist = p.getDist();
|
||||
@@ -0,0 +1,124 @@
|
||||
/// <reference path="../../_definitions.ts" />
|
||||
|
||||
/**
|
||||
* Phaser - Physics - Projection
|
||||
*/
|
||||
|
||||
module Phaser.Physics.Projection {
|
||||
|
||||
export class AABB67Deg {
|
||||
|
||||
public static CollideS(x: number, y: number, obj: Phaser.Physics.AABB, t: Phaser.Physics.TileMapCell) {
|
||||
|
||||
var signx = t.signx;
|
||||
var signy = t.signy;
|
||||
|
||||
var px = obj.pos.x - (signx * obj.xw);
|
||||
var penX = t.pos.x - px;
|
||||
|
||||
if (0 < (penX * signx))
|
||||
{
|
||||
|
||||
var ox = (obj.pos.x - (signx * obj.xw)) - (t.pos.x - (signx * t.xw));//this gives is the coordinates of the innermost
|
||||
var oy = (obj.pos.y - (signy * obj.yw)) - (t.pos.y + (signy * t.yw));//point on the AABB, relative to a point on the slope
|
||||
|
||||
var sx = t.sx;//get slope unit normal
|
||||
var sy = t.sy;
|
||||
|
||||
//if the dotprod of (ox,oy) and (sx,sy) is negative, the corner is in the slope
|
||||
//and we need to project it out by the magnitude of the projection of (ox,oy) onto (sx,sy)
|
||||
var dp = (ox * sx) + (oy * sy);
|
||||
if (dp < 0)
|
||||
{
|
||||
//collision; project delta onto slope and use this to displace the object
|
||||
sx *= -dp;//(sx,sy) is now the projection vector
|
||||
sy *= -dp;
|
||||
|
||||
var lenN = Math.sqrt(sx * sx + sy * sy);
|
||||
var lenP = Math.sqrt(x * x + y * y);
|
||||
|
||||
var aX = Math.abs(penX);
|
||||
if (lenP < lenN)
|
||||
{
|
||||
if (aX < lenP)
|
||||
{
|
||||
obj.reportCollisionVsWorld(penX, 0, penX / aX, 0, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_OTHER;
|
||||
}
|
||||
else
|
||||
{
|
||||
obj.reportCollisionVsWorld(x, y, x / lenP, y / lenP, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_AXIS;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (aX < lenN)
|
||||
{
|
||||
obj.reportCollisionVsWorld(penX, 0, penX / aX, 0, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_OTHER;
|
||||
}
|
||||
else
|
||||
{
|
||||
obj.reportCollisionVsWorld(sx, sy, t.sx, t.sy, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_OTHER;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//if we've reached this point, no collision has occured
|
||||
return Phaser.Physics.AABB.COL_NONE;
|
||||
|
||||
}
|
||||
|
||||
public static CollideB(x: number, y: number, obj: Phaser.Physics.AABB, t: Phaser.Physics.TileMapCell) {
|
||||
|
||||
var signx = t.signx;
|
||||
var signy = t.signy;
|
||||
|
||||
var ox = (obj.pos.x - (signx * obj.xw)) - (t.pos.x + (signx * t.xw));//this gives is the coordinates of the innermost
|
||||
var oy = (obj.pos.y - (signy * obj.yw)) - (t.pos.y - (signy * t.yw));//point on the AABB, relative to a point on the slope
|
||||
|
||||
var sx = t.sx;//get slope unit normal
|
||||
var sy = t.sy;
|
||||
|
||||
//if the dotprod of (ox,oy) and (sx,sy) is negative, the corner is in the slope
|
||||
//and we need toproject it out by the magnitude of the projection of (ox,oy) onto (sx,sy)
|
||||
var dp = (ox * sx) + (oy * sy);
|
||||
|
||||
if (dp < 0)
|
||||
{
|
||||
//collision; project delta onto slope and use this to displace the object
|
||||
sx *= -dp;//(sx,sy) is now the projection vector
|
||||
sy *= -dp;
|
||||
|
||||
var lenN = Math.sqrt(sx * sx + sy * sy);
|
||||
var lenP = Math.sqrt(x * x + y * y);
|
||||
|
||||
|
||||
if (lenP < lenN)
|
||||
{
|
||||
obj.reportCollisionVsWorld(x, y, x / lenP, y / lenP, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_AXIS;
|
||||
}
|
||||
else
|
||||
{
|
||||
obj.reportCollisionVsWorld(sx, sy, t.sx, t.sy, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_OTHER;
|
||||
}
|
||||
}
|
||||
|
||||
return Phaser.Physics.AABB.COL_NONE;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -30,7 +30,7 @@ var Phaser;
|
||||
var lenP = Math.sqrt(x * x + y * y);
|
||||
if (lenP < pen) {
|
||||
//it's shorter to move along axis directions
|
||||
obj.ReportCollisionVsWorld(x, y, x / lenP, y / lenP, t);
|
||||
obj.reportCollisionVsWorld(x, y, x / lenP, y / lenP, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_AXIS;
|
||||
} else {
|
||||
@@ -38,7 +38,7 @@ var Phaser;
|
||||
ox /= len;
|
||||
oy /= len;
|
||||
|
||||
obj.ReportCollisionVsWorld(ox * pen, oy * pen, ox, oy, t);
|
||||
obj.reportCollisionVsWorld(ox * pen, oy * pen, ox, oy, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_OTHER;
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ module Phaser.Physics.Projection {
|
||||
if (lenP < pen)
|
||||
{
|
||||
//it's shorter to move along axis directions
|
||||
obj.ReportCollisionVsWorld(x, y, x / lenP, y / lenP, t);
|
||||
obj.reportCollisionVsWorld(x, y, x / lenP, y / lenP, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_AXIS;
|
||||
}
|
||||
@@ -44,7 +44,7 @@ module Phaser.Physics.Projection {
|
||||
ox /= len;//len should never be 0, since if it IS 0, rad should be > than len
|
||||
oy /= len;//and we should never reach here
|
||||
|
||||
obj.ReportCollisionVsWorld(ox * pen, oy * pen, ox, oy, t);
|
||||
obj.reportCollisionVsWorld(ox * pen, oy * pen, ox, oy, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_OTHER;
|
||||
}
|
||||
|
||||
@@ -27,14 +27,14 @@ var Phaser;
|
||||
if (((signx * ox) < 0) || ((signy * oy) < 0)) {
|
||||
//the test corner is "outside" the 1/4 of the circle we're interested in
|
||||
var lenP = Math.sqrt(x * x + y * y);
|
||||
obj.ReportCollisionVsWorld(x, y, x / lenP, y / lenP, t);
|
||||
obj.reportCollisionVsWorld(x, y, x / lenP, y / lenP, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_AXIS;
|
||||
} else if (0 < pen) {
|
||||
//project along corner->circle vector
|
||||
ox /= len;
|
||||
oy /= len;
|
||||
obj.ReportCollisionVsWorld(ox * pen, oy * pen, ox, oy, t);
|
||||
obj.reportCollisionVsWorld(ox * pen, oy * pen, ox, oy, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_OTHER;
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ module Phaser.Physics.Projection {
|
||||
{
|
||||
//the test corner is "outside" the 1/4 of the circle we're interested in
|
||||
var lenP = Math.sqrt(x * x + y * y);
|
||||
obj.ReportCollisionVsWorld(x, y, x / lenP, y / lenP, t);
|
||||
obj.reportCollisionVsWorld(x, y, x / lenP, y / lenP, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_AXIS;//we need to report
|
||||
}
|
||||
@@ -38,7 +38,7 @@ module Phaser.Physics.Projection {
|
||||
//project along corner->circle vector
|
||||
ox /= len;
|
||||
oy /= len;
|
||||
obj.ReportCollisionVsWorld(ox * pen, oy * pen, ox, oy, t);
|
||||
obj.reportCollisionVsWorld(ox * pen, oy * pen, ox, oy, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_OTHER;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ var Phaser;
|
||||
AABBFull.Collide = function (x, y, obj, t) {
|
||||
var l = Math.sqrt(x * x + y * y);
|
||||
|
||||
obj.ReportCollisionVsWorld(x, y, x / l, y / l, t);
|
||||
obj.reportCollisionVsWorld(x, y, x / l, y / l, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_AXIS;
|
||||
};
|
||||
|
||||
@@ -12,7 +12,7 @@ module Phaser.Physics.Projection {
|
||||
|
||||
var l = Math.sqrt(x * x + y * y);
|
||||
|
||||
obj.ReportCollisionVsWorld(x, y, x / l, y / l, t);
|
||||
obj.reportCollisionVsWorld(x, y, x / l, y / l, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_AXIS;
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
var Shapes;
|
||||
(function (Shapes) {
|
||||
|
||||
var Point = Shapes.Point = (function () {
|
||||
function Point(x, y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
Point.prototype.getDist = function () {
|
||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||
};
|
||||
Point.origin = new Point(0, 0);
|
||||
return Point;
|
||||
})();
|
||||
|
||||
})(Shapes || (Shapes = {}));
|
||||
|
||||
var p = new Shapes.Point(3, 4);
|
||||
var dist = p.getDist();
|
||||
@@ -0,0 +1,60 @@
|
||||
/// <reference path="../../_definitions.ts" />
|
||||
|
||||
/**
|
||||
* Phaser - Physics - Projection
|
||||
*/
|
||||
|
||||
module Phaser.Physics.Projection {
|
||||
|
||||
export class AABBHalf {
|
||||
|
||||
public static Collide(x: number, y: number, obj: Phaser.Physics.AABB, t: Phaser.Physics.TileMapCell) {
|
||||
|
||||
//calculate the projection vector for the half-edge, and then
|
||||
//(if collision is occuring) pick the minimum
|
||||
|
||||
var sx = t.signx;
|
||||
var sy = t.signy;
|
||||
|
||||
var ox = (obj.pos.x - (sx * obj.xw)) - t.pos.x;//this gives is the coordinates of the innermost
|
||||
var oy = (obj.pos.y - (sy * obj.yw)) - t.pos.y;//point on the AABB, relative to the tile center
|
||||
|
||||
//we perform operations analogous to the 45deg tile, except we're using
|
||||
//an axis-aligned slope instead of an angled one..
|
||||
|
||||
//if the dotprod of (ox,oy) and (sx,sy) is negative, the corner is in the slope
|
||||
//and we need toproject it out by the magnitude of the projection of (ox,oy) onto (sx,sy)
|
||||
var dp = (ox * sx) + (oy * sy);
|
||||
if (dp < 0)
|
||||
{
|
||||
//collision; project delta onto slope and use this to displace the object
|
||||
sx *= -dp;//(sx,sy) is now the projection vector
|
||||
sy *= -dp;
|
||||
|
||||
var lenN = Math.sqrt(sx * sx + sy * sy);
|
||||
var lenP = Math.sqrt(x * x + y * y);
|
||||
|
||||
if (lenP < lenN)
|
||||
{
|
||||
//project along axis; note that we're assuming that this tile is horizontal OR vertical
|
||||
//relative to the AABB's current tile, and not diagonal OR the current tile.
|
||||
obj.reportCollisionVsWorld(x, y, x / lenP, y / lenP, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_AXIS;
|
||||
}
|
||||
else
|
||||
{
|
||||
//note that we could use -= instead of -dp
|
||||
obj.reportCollisionVsWorld(sx, sy, t.signx, t.signy, t);
|
||||
|
||||
return Phaser.Physics.AABB.COL_OTHER;
|
||||
}
|
||||
}
|
||||
|
||||
return Phaser.Physics.AABB.COL_NONE;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user