SAT implemented and working nicely at long last :)

This commit is contained in:
photonstorm 2014-01-23 21:45:35 +00:00
parent 110ab4e253
commit ea9e22f472
7 changed files with 662 additions and 35 deletions

View file

@ -65,6 +65,8 @@ Significant API changes:
* Body.maxVelocity defaults have been removed from 10,000 to 2000.
* Body.friction is new and has a default value of 0.1 - you may need to set this to zero depending on the type of game you're making.
* Body.customSeparateCallback allows you to set your own callback when two Bodies need to separate rather than using the built-in method.
* Body.collideCallback allows you to set a callback that is fired whenever the Body is hit on any of its active faces.
* Body.allowCollision has been renamed to Body.checkCollision.
New features:

View file

@ -77,6 +77,7 @@
<meta charset="UTF-8" />
<title>phaser</title>
<base href="../"></base>
<script src="wip/SAT.js" type="text/javascript"></script>
<?php
require('../../build/config.php');

View file

@ -0,0 +1,125 @@
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 land;
function create() {
game.stage.backgroundColor = '#124184';
// game.physics.gravity.y = 100;
sprite = game.add.sprite(0, 300, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
// sprite.body.checkCollision.right = false;
sprite.body.bounce.setTo(0.9, 0.9);
// sprite.body.bounce.setTo(1, 1);
// sprite.body.friction = 0;
// sprite.scale.setTo(2, 2);
// sprite2 = game.add.sprite(500, 300, 'gameboy', 2);
sprite2 = game.add.sprite(500, 300, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(0.9, 0.9);
// sprite2.body.bounce.setTo(1, 1);
// sprite2.body.friction = 0;
land = new SAT.Polygon(new SAT.Vector(), [
new SAT.Vector(),
new SAT.Vector(100,0),
new SAT.Vector(50,75),
]);
land.pos.x = 300;
land.pos.y = 500;
// sprite2.x = sprite.body.right + 10;
// sprite2.y = sprite.body.bottom + 10;
// sprite.x = 300;
// sprite.y = 100;
// sprite2.x = 300;
// sprite2.y = 300;
// sprite2.body.velocity.y = -100;
sprite2.body.velocity.x = -300;
// game.add.tween(sprite.scale).to({x: 3, y: 3}, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
// to: function (properties, duration, ease, autoStart, delay, repeat, yoyo) {
game.input.onDown.add(launch, this);
}
function launch() {
// sprite.body.velocity.x = 150;
// sprite.body.velocity.x = -400;
// sprite.body.velocity.y = -400;
sprite2.body.velocity.x = -100;
sprite2.body.velocity.y = -100;
}
/*
Tweening body scale test!
sprite = game.add.sprite(300, 300, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
// sprite.body.checkCollision.right = false;
sprite.body.bounce.setTo(1, 1);
sprite.body.friction = 0;
// sprite.scale.setTo(2, 2);
sprite2 = game.add.sprite(500, 300, 'gameboy', 2);
sprite2.name = 'green';
sprite2.body.collideWorldBounds = true;
sprite2.body.bounce.setTo(1, 1);
sprite2.body.friction = 0;
game.add.tween(sprite.scale).to({x: 3, y: 3}, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
// to: function (properties, duration, ease, autoStart, delay, repeat, yoyo) {
*/
function update() {
game.physics.collide(sprite, sprite2);
}
function render() {
game.debug.renderPolygon(land);
if (sprite)
{
game.debug.renderBodyInfo(sprite, 16, 24);
}
if (sprite)
{
game.debug.renderPolygon(sprite.body.polygons);
}
if (sprite2)
{
game.debug.renderPolygon(sprite2.body.polygons);
}
}

View file

@ -36,7 +36,7 @@ function create() {
for (var i = 0; i < 50; i++)
{
var s = balls.create(game.rnd.integerInRange(100, 700), game.rnd.integerInRange(100, 200), 'balls', game.rnd.integerInRange(0, 5));
var s = balls.create(game.rnd.integerInRange(100, 700), game.rnd.integerInRange(100, 200), 'balls', game.rnd.integerInRange(0, 6));
s.body.velocity.x = game.rnd.integerInRange(-400, 400);
s.body.velocity.y = game.rnd.integerInRange(-100, -200);
s.name = 'ball' + i;
@ -45,11 +45,15 @@ function create() {
balls.setAll('body.collideWorldBounds', true);
balls.setAll('body.bounce.x', 0.8);
balls.setAll('body.bounce.y', 0.9);
balls.setAll('body.minBounceVelocity', 0.9);
balls.setAll('body.friction', 0.5);
sprite = game.add.sprite(300, 100, 'gameboy', 0);
sprite.name = 'red';
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.5, 0.5);
sprite.body.minBounceVelocity = 0.9;
sprite.body.bounce.setTo(0.5, 0.9);
sprite.body.friction = 0.5;
sprite2 = game.add.sprite(300, 250, 'gameboy', 2);
sprite2.name = 'yellow';

View file

@ -603,7 +603,8 @@ Phaser.Physics.Arcade.prototype = {
separate: function (body1, body2, processCallback, callbackContext, overlapOnly) {
// Can't separate two immovable bodies and the same body cannot collide with itself
if (body1 === body2 || (body1.immovable && body2.immovable) || Phaser.Rectangle.intersects(body1, body2) === false)
// if (body1 === body2 || (body1.immovable && body2.immovable) || Phaser.Rectangle.intersects(body1, body2) === false)
if (body1 === body2 || Phaser.Rectangle.intersects(body1, body2) === false)
{
return false;
}
@ -674,7 +675,7 @@ Phaser.Physics.Arcade.prototype = {
}
}
if (body.deltaX() < 0 && body.allowCollision.left && tile.tile.faceRight)
if (body.deltaX() < 0 && body.checkCollision.left && tile.tile.faceRight)
{
// LEFT
localOverlapX = body.x - tile.right;
@ -686,7 +687,7 @@ Phaser.Physics.Arcade.prototype = {
body.touching.none = false;
}
}
else if (body.deltaX() > 0 && body.allowCollision.right && tile.tile.faceLeft)
else if (body.deltaX() > 0 && body.checkCollision.right && tile.tile.faceLeft)
{
// RIGHT
localOverlapX = body.right - tile.x;
@ -700,7 +701,7 @@ Phaser.Physics.Arcade.prototype = {
}
}
if (body.deltaY() < 0 && body.allowCollision.up && tile.tile.faceBottom)
if (body.deltaY() < 0 && body.checkCollision.up && tile.tile.faceBottom)
{
// UP
localOverlapY = body.y - tile.bottom;
@ -713,7 +714,7 @@ Phaser.Physics.Arcade.prototype = {
body.touching.none = false;
}
}
else if (body.deltaY() > 0 && body.allowCollision.down && tile.tile.faceTop)
else if (body.deltaY() > 0 && body.checkCollision.down && tile.tile.faceTop)
{
// DOWN
localOverlapY = body.bottom - tile.y;
@ -778,7 +779,7 @@ Phaser.Physics.Arcade.prototype = {
body.overlapY = 0;
// Remember - this happens AFTER the body has been moved by the motion update, so it needs moving back again
if (body.deltaX() < 0 && body.allowCollision.left && tile.tile.faceRight)
if (body.deltaX() < 0 && body.checkCollision.left && tile.tile.faceRight)
{
// LEFT
body.overlapX = body.x - tile.right;
@ -791,7 +792,7 @@ Phaser.Physics.Arcade.prototype = {
body.touching.none = false;
}
}
else if (body.deltaX() > 0 && body.allowCollision.right && tile.tile.faceLeft)
else if (body.deltaX() > 0 && body.checkCollision.right && tile.tile.faceLeft)
{
// RIGHT
body.overlapX = body.right - tile.x;
@ -805,7 +806,7 @@ Phaser.Physics.Arcade.prototype = {
}
}
if (body.deltaY() < 0 && body.allowCollision.up && tile.tile.faceBottom)
if (body.deltaY() < 0 && body.checkCollision.up && tile.tile.faceBottom)
{
// UP
body.overlapY = body.y - tile.bottom;
@ -818,7 +819,7 @@ Phaser.Physics.Arcade.prototype = {
body.touching.none = false;
}
}
else if (body.deltaY() > 0 && body.allowCollision.down && tile.tile.faceTop)
else if (body.deltaY() > 0 && body.checkCollision.down && tile.tile.faceTop)
{
// DOWN
body.overlapY = body.bottom - tile.y;

View file

@ -192,11 +192,11 @@ Phaser.Physics.Arcade.Body = function (sprite) {
this.mass = 1;
/**
* Set the allowCollision properties to control which directions collision is processed for this Body.
* For example allowCollision.up = false means it won't collide when the collision happened while moving up.
* @property {object} allowCollision - An object containing allowed collision.
* Set the checkCollision properties to control which directions collision is processed for this Body.
* For example checkCollision.up = false means it won't collide when the collision happened while moving up.
* @property {object} checkCollision - An object containing allowed collision.
*/
this.allowCollision = { none: false, any: true, up: true, down: true, left: true, right: true };
this.checkCollision = { none: false, any: true, up: true, down: true, left: true, right: true };
/**
* This object is populated with boolean values when the Body collides with another.
@ -343,6 +343,12 @@ Phaser.Physics.Arcade.Body = function (sprite) {
*/
this._average = 0;
this.polygons = new SAT.Box(new SAT.Vector(this.x, this.y), this.width, this.height).toPolygon();
this.response = new SAT.Response();
// this.vx;
// this.vy;
this._debug = 0;
};
@ -363,6 +369,10 @@ Phaser.Physics.Arcade.Body.prototype = {
this.height = this.sourceHeight * scaleY;
this.halfWidth = Math.floor(this.width / 2);
this.halfHeight = Math.floor(this.height / 2);
// Scale by the difference between this and what it was previously
this.polygons.scale(scaleX / this._sx, scaleY / this._sy);
this._sx = scaleX;
this._sy = scaleY;
this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight);
@ -390,11 +400,11 @@ Phaser.Physics.Arcade.Body.prototype = {
this.blocked.left = false;
this.blocked.right = false;
this.touching.none = true;
this.touching.up = false;
this.touching.down = false;
this.touching.left = false;
this.touching.right = false;
// this.touching.none = true;
// this.touching.up = false;
// this.touching.down = false;
// this.touching.left = false;
// this.touching.right = false;
this.x = this.preX;
this.y = this.preY;
@ -415,6 +425,9 @@ Phaser.Physics.Arcade.Body.prototype = {
this.applyMotion();
}
this.polygons.pos.x = this.x;
this.polygons.pos.y = this.y;
},
/**
@ -455,6 +468,89 @@ Phaser.Physics.Arcade.Body.prototype = {
},
/**
* Internal method used to check the Body against the World Bounds.
*
* @method Phaser.Physics.Arcade#checkWorldBounds
* @protected
*/
NEWcheckWorldBounds: function () {
this.blockedPoint.setTo(0, 0);
if (this.x < this.game.world.bounds.x)
{
this.blockedPoint.x = this.game.world.bounds.x - this.x;
this.blocked.left = true;
this.touching.left = true;
}
else if (this.right > this.game.world.bounds.right)
{
this.blockedPoint.x = this.right - this.game.world.bounds.right;
this.blocked.right = true;
this.touching.right = true;
}
if (this.y < this.game.world.bounds.y)
{
this.blockedPoint.y = this.game.world.bounds.y - this.y;
this.blocked.up = true;
this.touching.up = true;
}
else if (this.bottom > this.game.world.bounds.bottom)
{
this.blockedPoint.y = this.bottom - this.game.world.bounds.bottom;
this.blocked.down = true;
this.touching.down = true;
}
// overlapX/Y values at this point will be penetration into the bounds and DELTA WILL BE ZERO
if (this.blocked.left && this.blockedPoint.x > 0)
{
// Separate
this.x += this.blockedPoint.x;
this.velocity.x *= -this.bounce.x;
// this.reboundCheck(true, false);
}
else if (this.blocked.right && this.blockedPoint.x > 0)
{
// Separate
this.x -= this.blockedPoint.x;
this.velocity.x *= -this.bounce.x;
// this.reboundCheck(true, false);
}
else
{
this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
this.velocity.x += this.motionVelocity.x;
}
// overlapX/Y values at this point will be penetration into the bounds and DELTA WILL BE ZERO
if (this.blocked.up && this.blockedPoint.y > 0)
{
// Separate
this.y += this.blockedPoint.y;
this.velocity.y *= -this.bounce.y;
// this.reboundCheck(false, true);
}
else if (this.blocked.down && this.blockedPoint.y > 0)
{
// Separate
this.y -= this.blockedPoint.y;
this.velocity.y *= -this.bounce.y;
// this.reboundCheck(false, true);
}
else
{
this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
this.velocity.y += this.motionVelocity.y;
}
this.polygons.pos.x = this.x;
this.polygons.pos.y = this.y;
},
/**
* Internal method.
*
@ -590,10 +686,13 @@ Phaser.Physics.Arcade.Body.prototype = {
this.velocity.y = -this.maxVelocity.y;
}
this.polygons.pos.x = this.x;
this.polygons.pos.y = this.y;
},
/**
* Checks for an overlap between this Body and the given Body, taking into account the allowCollision flags on both bodies.
* Checks for an overlap between this Body and the given Body, taking into account the checkCollision flags on both bodies.
* If an overlap occurs the Body.touching flags are set and the results are stored in overlapX and overlapY.
*
* @method Phaser.Physics.Arcade#overlap
@ -602,36 +701,138 @@ Phaser.Physics.Arcade.Body.prototype = {
*/
overlap: function (body) {
// var r = new Phaser.Rectangle();
// This gives us our area of intersection (width / height)
// Phaser.Rectangle.intersection(this, body, r);
/*
this.overlapX = 0;
this.overlapY = 0;
if (this.x < body.x && this.allowCollision.right && body.allowCollision.left)
if (this.x < body.x && this.checkCollision.right && body.checkCollision.left)
{
// Negative = body touched this one on the right face
console.log('Negative = body touched this one on the right face');
this.overlapX = body.x - this.right;
this.touching.right = true;
}
else if (this.x > body.x && this.allowCollision.left && body.allowCollision.right)
else if (this.x > body.x && this.checkCollision.left && body.checkCollision.right)
{
// Positive means body touched this one on the left face
console.log('Positive means body touched this one on the left face');
this.overlapX = body.right - this.x;
this.touching.left = true;
}
if (this.y < body.y && this.allowCollision.down && body.allowCollision.up)
if (this.y < body.y && this.checkCollision.down && body.checkCollision.up)
{
// Negative = body touched this one on the bottom face
this.overlapY = body.y - this.bottom;
this.touching.down = true;
}
else if (this.y > body.y && this.allowCollision.up && body.allowCollision.down)
else if (this.y > body.y && this.checkCollision.up && body.checkCollision.down)
{
// Positive means body touched this one on the top face
this.overlapY = body.bottom - this.y;
this.touching.up = true;
}
*/
/*
var ax = this.deltaX();
var bx = body.deltaX();
var check = Phaser.NONE;
// if (ax <= 0 && bx < 0)
// {
// check = Phaser.RIGHT;
// }
if (ax <= 0 && bx < 0)
{
// This is stationary (or moving left) and Body is moving left
if (this.x <= body.x)
{
// Body entering right side of This
if (this.checkCollision.right)
{
this.overlapX = -r.width;
this.touching.right = true;
}
if (body.checkCollision.left)
{
body.touching.left = true;
}
}
}
else if (ax <= 0 && bx > 0)
{
// This is stationary (or moving left) and Body is moving right
if (this.x >= body.x)
{
// Body entering left side of This
if (this.checkCollision.left)
{
this.overlapX = r.width;
this.touching.left = true;
}
if (body.checkCollision.right)
{
body.touching.right = true;
}
}
}
else if (ax > 0 && bx < 0)
{
// This is moving right and Body is moving left
if (this.x <= body.x)
{
// Body entering right side of This
if (this.checkCollision.right)
{
this.overlapX = -r.width;
this.touching.right = true;
}
if (body.checkCollision.left)
{
body.touching.left = true;
}
}
}
else if (ax > 0 && bx > 0)
{
// This is moving right and Body is moving right
if (this.x >= body.x)
{
// Body entering left side of This
if (this.checkCollision.left)
{
this.overlapX = r.width;
this.touching.left = true;
}
if (body.checkCollision.right)
{
body.touching.right = true;
}
}
}
*/
this.response.clear();
return SAT.testPolygonPolygon(this.polygons, body.polygons, this.response);
// console.log(this.overlapX, r.width);
// console.log(r);
// Which is the largest?
/*
if (this.overlapX !== 0 && this.overlapY !== 0)
{
// Crudely find out which is the largest penetration side
@ -653,10 +854,18 @@ Phaser.Physics.Arcade.Body.prototype = {
// overlapX/Y now contains either zero or a positive value containing the overlapping area
return (this.overlapX !== 0 || this.overlapY !== 0);
*/
},
// Check if we're below minVelocity and gravity isn't trying to drag us in the opposite direction
/**
* Check if we're below minVelocity and gravity isn't trying to drag us in the opposite direction.
*
* @method Phaser.Physics.Arcade#reboundCheck
* @protected
* @param {boolean} x - Check the X axis?
* @param {boolean} y - Check the Y axis?
*/
reboundCheck: function (x, y) {
if (x)
@ -681,6 +890,13 @@ Phaser.Physics.Arcade.Body.prototype = {
},
/**
* Gets the total gravity to be applied on the X axis.
*
* @method Phaser.Physics.Arcade#getTotalGravityX
* @protected
* @return {number} The total gravity to be applied on the X axis.
*/
getTotalGravityX: function () {
if (this.allowGravity)
@ -694,6 +910,13 @@ Phaser.Physics.Arcade.Body.prototype = {
},
/**
* Gets the total gravity to be applied on the Y axis.
*
* @method Phaser.Physics.Arcade#getTotalGravityY
* @protected
* @return {number} The total gravity to be applied on the Y axis.
*/
getTotalGravityY: function () {
if (this.allowGravity)
@ -707,6 +930,114 @@ Phaser.Physics.Arcade.Body.prototype = {
},
hitLeft: function (body, response) {
// This body isn't moving horizontally, so it was hit by something moving right
if (this.immovable || this.blocked.right)
{
body.add(response.overlapV);
body.velocity.x = this.velocity.x - body.velocity.x * body.bounce.x;
body.velocity.y = this.velocity.y - body.velocity.y * body.bounce.y;
body.reboundCheck(true, true);
}
else
{
if (body.immovable || body.blocked.left)
{
// We take the full separation as what hit is isn't moveable
this.sub(response.overlapV);
this.velocity.x = body.velocity.x - this.velocity.x * this.bounce.x;
this.velocity.y = body.velocity.y - this.velocity.y * this.bounce.y;
this.reboundCheck(true, true);
}
else
{
response.overlapV.scale(0.5);
this.sub(response.overlapV);
body.add(response.overlapV);
// this.reboundCheck(true, false);
// body.reboundCheck(true, false);
var tempVX = body.velocity.x;
var tempVY = body.velocity.y;
body.velocity.x = this.velocity.x * body.bounce.x;
body.velocity.y = this.velocity.y * body.bounce.x;
this.velocity.x = tempVX * this.bounce.x;
this.velocity.y = tempVY * this.bounce.y;
this.reboundCheck(true, true);
body.reboundCheck(true, false);
this.acceleration.setTo(0, 0);
body.acceleration.setTo(0, 0);
}
}
// Bounds check
if (this.checkWorldBounds && this.right >= this.game.world.bounds.right)
{
this.blocked.right = true;
this.x -= this.right - this.game.world.bounds.right;
}
if (this.collideCallback)
{
this.collideCallback.call(this.collideCallbackContext, Phaser.LEFT, this, body);
}
},
hitRight: function (body, response) {
// This body isn't moving horizontally, so it was hit by something moving right
if (this.immovable || this.blocked.left)
{
body.add(response.overlapV);
body.velocity.x = this.velocity.x - body.velocity.x * body.bounce.x;
body.velocity.y = this.velocity.y - body.velocity.y * body.bounce.y;
body.reboundCheck(true, true);
}
else
{
if (body.immovable || body.blocked.right)
{
// We take the full separation as what hit is isn't moveable
this.sub(response.overlapV);
this.velocity.x = body.velocity.x - this.velocity.x * this.bounce.x;
this.velocity.y = body.velocity.y - this.velocity.y * this.bounce.y;
this.reboundCheck(true, true);
}
else
{
response.overlapV.scale(0.5);
this.sub(response.overlapV);
body.add(response.overlapV);
// this.reboundCheck(true, false);
// body.reboundCheck(true, false);
var tempVX = body.velocity.x;
var tempVY = body.velocity.y;
body.velocity.x = this.velocity.x * body.bounce.x;
body.velocity.y = this.velocity.y * body.bounce.x;
this.velocity.x = tempVX * this.bounce.x;
this.velocity.y = tempVY * this.bounce.y;
this.reboundCheck(true, true);
body.reboundCheck(true, false);
this.acceleration.setTo(0, 0);
body.acceleration.setTo(0, 0);
}
}
// Bounds check
if (this.checkWorldBounds && this.x <= this.game.world.bounds.x)
{
this.blocked.left = true;
this.x += this.game.world.bounds.x - this.x;
}
if (this.collideCallback)
{
this.collideCallback.call(this.collideCallbackContext, Phaser.RIGHT, this, body);
}
},
/**
* Process a collision with the left face of this Body. If possible the Body will be moved right.
* Uses overlayX which will be positive.
@ -719,7 +1050,7 @@ Phaser.Physics.Arcade.Body.prototype = {
* @param {number} nv2 - The new velocity for the colliding Body.
* @param {number} avg - The new average velocity between the two Bodies.
*/
hitLeft: function (x, body, nv1, nv2, avg) {
XhitLeft: function (x, body, nv1, nv2, avg) {
// This body isn't moving horizontally, so it was hit by something moving right
if (this.immovable || this.blocked.right)
@ -777,7 +1108,7 @@ Phaser.Physics.Arcade.Body.prototype = {
* @param {number} nv2 - The new velocity for the colliding Body.
* @param {number} avg - The new average velocity between the two Bodies.
*/
hitRight: function (x, body, nv1, nv2, avg) {
XhitRight: function (x, body, nv1, nv2, avg) {
// This body isn't moving horizontally, so it was hit by something moving right
if (this.immovable || this.blocked.left)
@ -835,7 +1166,7 @@ Phaser.Physics.Arcade.Body.prototype = {
* @param {number} nv2 - The new velocity for the colliding Body.
* @param {number} avg - The new average velocity between the two Bodies.
*/
hitUp: function (y, body, nv1, nv2, avg) {
XhitUp: function (y, body, nv1, nv2, avg) {
// This body isn't moving horizontally, so it was hit by something moving right
if (this.immovable || this.blocked.down)
@ -893,7 +1224,7 @@ Phaser.Physics.Arcade.Body.prototype = {
* @param {number} nv2 - The new velocity for the colliding Body.
* @param {number} avg - The new average velocity between the two Bodies.
*/
hitDown: function (y, body, nv1, nv2, avg) {
XhitDown: function (y, body, nv1, nv2, avg) {
// This body isn't moving horizontally, so it was hit by something moving right
if (this.immovable || this.blocked.up)
@ -938,6 +1269,21 @@ Phaser.Physics.Arcade.Body.prototype = {
},
sub: function (v) {
this.x -= v.x;
this.y -= v.y;
},
add: function (v) {
this.x += v.x;
this.y += v.y;
},
/**
* This separates this Body from the given Body unless a customSeparateCallback is set.
* It assumes they have already been overlap checked and the resulting overlap is stored in overlapX and overlapY.
@ -954,6 +1300,147 @@ Phaser.Physics.Arcade.Body.prototype = {
return this.customSeparateCallback.call(this.customSeparateContext, this, this.overlapX, this.overlapY);
}
// if (this.immovable && body.immovable)
// {
// this.response.overlapV.scale(0.5);
// this.sub(this.response.overlapV);
// body.add(this.response.overlapV);
// }
// Move equally out of each other
// console.log(this.response);
// angle of collision
// var dx = this.x - body.x;
// var dy = this.y - body.y;
// var collision_angle = Math.atan2(dy, dx);
// var sin = Math.sin(collision_angle);
// var cos = Math.sin(collision_angle);
// var sin2 = Math.sin(collision_angle + Math.PI / 2);
// var cos2 = Math.sin(collision_angle + Math.PI / 2);
var distances = [
(body.right - this.x), // distance of box 'b' to face on 'left' side of 'a'.
(this.right - body.x), // distance of box 'b' to face on 'right' side of 'a'.
(body.bottom - this.y), // distance of box 'b' to face on 'bottom' side of 'a'.
(this.bottom - body.y) // distance of box 'b' to face on 'top' side of 'a'.
];
// console.log('angle of collision', collision_angle, this.game.math.radToDeg(collision_angle), this.response);
// console.log(distances);
if (this.response.overlapN.x)
{
// Which is smaller? Left or Right?
if (distances[0] < distances[1])
{
console.log(this.sprite.name, 'collided on the LEFT with', body.sprite.name);
this.hitLeft(body, this.response);
}
else if (distances[1] < distances[0])
{
console.log(this.sprite.name, 'collided on the RIGHT with', body.sprite.name);
this.hitRight(body, this.response);
}
}
else if (this.response.overlapN.y)
{
// Which is smaller? Top or Bottom?
if (distances[2] < distances[3])
{
console.log(this.sprite.name, 'collided on the TOP with', body.sprite.name);
this.touching.up = true;
body.touching.down = true;
}
else if (distances[3] < distances[2])
{
console.log(this.sprite.name, 'collided on the BOTTOM with', body.sprite.name);
this.touching.down = true;
body.touching.up = true;
}
}
return true;
// this.response.overlapV.scale(0.5);
// this.x -= this.response.overlapV.x;
// this.y -= this.response.overlapV.y;
// body.x += this.response.overlapV.x;
// body.y += this.response.overlapV.y;
// this.polygons.pos.x = this.x;
// this.polygons.pos.y = this.y;
// body.polygons.pos.x = body.x;
// body.polygons.pos.y = body.y;
// var tempVX = body.velocity.x;
// var tempVY = body.velocity.y;
// body.velocity.x = this.velocity.x * body.bounce.x;
// body.velocity.y = this.velocity.y * body.bounce.x;
// this.velocity.x = tempVX * this.bounce.x;
// this.velocity.y = tempVY * this.bounce.y;
// this.reboundCheck(true, true);
// this.acceleration.setTo(0, 0);
// body.acceleration.setTo(0, 0);
/*
Find the positions right before the collision. You are already approximating this by: "finding the shortest penetration vector and adding it to the AABB's position."
Find the velocities right after the collision using Newtonian physics:
For the case where mass is hard-coded as 1, simply swap the velocities (this does not apply to static objects which must have infinite mass):
A.v = B.u
B.v = A.u
If objects A and B have different masses:
A.v = (A.u * (A.m - B.m) + (2 * B.m * B.u)) / (A.m + B.m)
B.v = (B.u * (B.m - A.m) + (2 * A.m * A.u)) / (A.m + B.m)
where:
v: velocity after collision
u: velocity before collision
m: mass (use the largest number possible for the mass of a fixed, static object)
Set acceleration to 0: The acceleration from the collision was accounted for above by the velocity calculations in step number 2.
*/
/*
var vx_1 = this.speed * Math.cos(this.angle - collision_angle);
var vy_1 = this.speed * Math.sin(this.angle - collision_angle);
var vx_2 = body.speed * Math.cos(body.angle - collision_angle);
var vy_2 = body.speed * Math.sin(body.angle - collision_angle);
var final_vx_1 = ((this.mass - body.mass) * vx_1 + (body.mass + body.mass) * vx_2)/(this.mass + body.mass);
var final_vx_2 = ((this.mass + this.mass) * vx_1 + (body.mass - this.mass) * vx_2)/(this.mass + body.mass);
var final_vy_1 = vy_1;
var final_vy_2 = vy_2
this.velocity.x = (cos * final_vx_1 + cos2 * final_vy_1);
this.velocity.y = (sin * final_vx_1 + sin2 * final_vy_1);
body.velocity.x = (cos * final_vx_2 + cos2 * final_vy_2);
body.velocity.y = (sin * final_vx_2 + sin2 * final_vy_2);
// this.velocity.x = (cos * final_vx_1 + cos2 * final_vy_1) * this.bounce.x;
// this.velocity.y = (sin * final_vx_1 + sin2 * final_vy_1) * this.bounce.y;
// body.velocity.x = (cos * final_vx_2 + cos2 * final_vy_2) * body.bounce.x;
// body.velocity.y = (sin * final_vx_2 + sin2 * final_vy_2) * body.bounce.y;
// this.velocity.x = (Math.cos(collision_angle) * final_vx_1 + Math.cos(collision_angle + Math.PI/2) * final_vy_1) * this.bounce.x;
// this.velocity.y = (Math.sin(collision_angle) * final_vx_1 + Math.sin(collision_angle + Math.PI/2) * final_vy_1) * this.bounce.y;
// body.velocity.x = (Math.cos(collision_angle) * final_vx_2 + Math.cos(collision_angle + Math.PI/2) * final_vy_2) * body.bounce.x;
// body.velocity.y = (Math.sin(collision_angle) * final_vx_2 + Math.sin(collision_angle + Math.PI/2) * final_vy_2) * body.bounce.y;
// this.sub(response.overlapV);
// body.add(response.overlapV);
*/
/*
if (this.overlapX !== 0)
{
this._newVelocity1 = Math.sqrt((body.velocity.x * body.velocity.x * body.mass) / this.mass) * ((body.velocity.x > 0) ? 1 : -1);
@ -989,6 +1476,7 @@ Phaser.Physics.Arcade.Body.prototype = {
this.hitUp(this.overlapY, body, this._newVelocity1, this._newVelocity2, this._average);
}
}
*/
return true;
@ -1022,6 +1510,8 @@ Phaser.Physics.Arcade.Body.prototype = {
this.facing = Phaser.DOWN;
}
// this.applyMotion();
if ((this.deltaX() < 0 && !this.blocked.left) || (this.deltaX() > 0 && !this.blocked.right))
{
this.sprite.x += this.deltaX();

View file

@ -923,7 +923,7 @@ Phaser.Utils.Debug.prototype = {
},
/**
* Renders the corners and point information of the given Sprite.
*
* @method Phaser.Utils.Debug#renderPolygon
* @param {array} polygon
* @param {string} [color='rgb(255,255,255)'] - The color the polygon is stroked in.
@ -937,14 +937,18 @@ Phaser.Utils.Debug.prototype = {
color = color || 'rgb(255,255,255)';
var points = polygon.points;
var x = polygon.pos.x;
var y = polygon.pos.y;
this.start(0, 0, color);
this.context.beginPath();
this.context.moveTo(polygon[0].x, polygon[0].y);
this.context.moveTo(x + points[0].x, y + points[0].y);
for (var i = 1; i < polygon.length; i++)
for (var i = 1; i < points.length; i++)
{
this.context.lineTo(polygon[i].x, polygon[i].y);
this.context.lineTo(x + points[i].x, y + points[i].y);
}
this.context.closePath();