mirror of
https://github.com/wassname/phaser.git
synced 2026-06-27 16:10:15 +08:00
Body bounce + blocking values nearly sorted.
This commit is contained in:
+25
-12
@@ -32,20 +32,17 @@ function create() {
|
||||
// gravity into floor jiggle
|
||||
function test8() {
|
||||
|
||||
game.physics.gravity.y = 150;
|
||||
game.physics.gravity.y = 250;
|
||||
|
||||
sprite = game.add.sprite(400, 600-100, 'ball');
|
||||
sprite.body.moves = false;
|
||||
sprite = game.add.sprite(400, 100, 'ball');
|
||||
sprite.body.collideWorldBounds = true;
|
||||
sprite.body.bounce.setTo(0.8, 0.8);
|
||||
sprite.body.minBounceVelocity = 0.8;
|
||||
|
||||
sprite2 = game.add.sprite(500, 100, 'ball');
|
||||
sprite2.body.collideWorldBounds = true;
|
||||
sprite2.body.bounce.setTo(0.5, 0.5);
|
||||
// sprite2.body.friction = 0.1;
|
||||
// sprite2.body.canSleep = true;
|
||||
|
||||
track = sprite2;
|
||||
sprite2.body.minBounceVelocity = 0.5;
|
||||
|
||||
game.input.onDown.add(launch8, this);
|
||||
|
||||
@@ -53,7 +50,7 @@ function test8() {
|
||||
|
||||
function launch8() {
|
||||
|
||||
sprite.body.velocity.y = -200;
|
||||
// sprite.body.velocity.y = 200;
|
||||
// sprite2.body.velocity.y = 200;
|
||||
|
||||
}
|
||||
@@ -232,14 +229,30 @@ function update() {
|
||||
|
||||
// sprite.rotation = sprite.body.angle;
|
||||
|
||||
bmd.fillStyle('#ffff00');
|
||||
bmd.fillRect(track.body.center.x, track.body.center.y, 2, 2);
|
||||
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() {
|
||||
|
||||
game.debug.renderBodyInfo(sprite, 16, 24);
|
||||
game.debug.renderBodyInfo(sprite2, 16, 190);
|
||||
if (sprite)
|
||||
{
|
||||
game.debug.renderBodyInfo(sprite, 16, 24);
|
||||
}
|
||||
|
||||
if (sprite2)
|
||||
{
|
||||
game.debug.renderBodyInfo(sprite2, 16, 190);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+131
-44
@@ -151,6 +151,13 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
||||
*/
|
||||
this.angle = 0;
|
||||
|
||||
/**
|
||||
* @property {number} minBounceVelocity - The minimum bounce velocity (could just be the bounce value?).
|
||||
*/
|
||||
this.minBounceVelocity = 0.5;
|
||||
|
||||
this._debug = 0;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} gravity - The gravity applied to the motion of the Body.
|
||||
*/
|
||||
@@ -385,6 +392,8 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
this.blocked.left = false;
|
||||
this.blocked.right = false;
|
||||
|
||||
this._debug++;
|
||||
|
||||
if (this.moves)
|
||||
{
|
||||
if (this.collideWorldBounds)
|
||||
@@ -452,34 +461,125 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
this.velocity.y = Math.sin(this.angle) * this.speed;
|
||||
}
|
||||
|
||||
// overlapX/Y values at this point will be penetration into the bounds
|
||||
if (this.blocked.down)
|
||||
// overlapX/Y values at this point will be penetration into the bounds and DELTA WILL BE ZERO
|
||||
if (this.blocked.left)
|
||||
{
|
||||
this.y -= this.overlapY;
|
||||
// Separate
|
||||
this.x += this.overlapX;
|
||||
this.preX = this.x; // because we don't want any delta from a separation
|
||||
|
||||
// Reflect?
|
||||
if (this.velocity.x > this.minBounceVelocity && this.bounce.x !== 0)
|
||||
{
|
||||
this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
|
||||
this.velocity.x += this.motionVelocity.x;
|
||||
this.velocity.x *= -this.bounce.x;
|
||||
// console.log(this._debug, 'blocked down + r', oy, 'v', this.velocity.y, 'mv', this.motionVelocity.y, 'd', this.deltaY());
|
||||
}
|
||||
else
|
||||
{
|
||||
this.velocity.x = 0;
|
||||
this.motionVelocity.x = 0;
|
||||
}
|
||||
}
|
||||
else if (this.blocked.up)
|
||||
else if (this.blocked.right)
|
||||
{
|
||||
// Separate
|
||||
this.x -= this.overlapX;
|
||||
this.preX = this.x; // because we don't want any delta from a separation
|
||||
|
||||
// Reflect?
|
||||
if (this.velocity.y < -this.minBounceVelocity && this.bounce.y !== 0)
|
||||
{
|
||||
this.x -= this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
|
||||
this.velocity.x += this.motionVelocity.x;
|
||||
this.velocity.x *= -this.bounce.x;
|
||||
// console.log(this._debug, 'blocked up + r', oy, 'v', this.velocity.y, 'mv', this.motionVelocity.y, 'd', this.deltaY());
|
||||
}
|
||||
else
|
||||
{
|
||||
this.velocity.x = 0;
|
||||
this.motionVelocity.x = 0;
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
// Separate
|
||||
this.y += this.overlapY;
|
||||
this.preY = this.y; // because we don't want any delta from a separation
|
||||
|
||||
// Reflect?
|
||||
if (this.velocity.y < -this.minBounceVelocity && this.bounce.y !== 0)
|
||||
{
|
||||
this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
|
||||
this.velocity.y += this.motionVelocity.y;
|
||||
this.velocity.y *= -this.bounce.y;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.velocity.y = 0;
|
||||
this.motionVelocity.y = 0;
|
||||
}
|
||||
|
||||
console.log(this._debug, 'blocked up', this.overlapY, 'v', this.velocity.y, 'min', this.minBounceVelocity, 'mv', this.motionVelocity.y, 'd', this.deltaY());
|
||||
}
|
||||
|
||||
this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
|
||||
this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
|
||||
|
||||
this.velocity.x += this.motionVelocity.x;
|
||||
this.velocity.y += this.motionVelocity.y;
|
||||
|
||||
if (this.blocked.down && this.deltaY() > 0)
|
||||
else if (this.blocked.down)
|
||||
{
|
||||
this.velocity.y *= -this.bounce.y;
|
||||
// Separate
|
||||
this.y -= this.overlapY;
|
||||
// this.preY = this.y; // because we don't want any delta from a separation
|
||||
// this.sprite.y -= this.deltaY();
|
||||
|
||||
// console.log(this._debug, 'blocked down', this.overlapY, 'v', this.velocity.y, 'min', this.minBounceVelocity, 'mv', this.motionVelocity.y, 'd', this.deltaY(), 'newy', this.y);
|
||||
|
||||
// Reflect? It's the velocity AFTER the bounce we need to test! And only if there is a bounce value
|
||||
if (this.bounce.y !== 0 && this.velocity.y != 0)
|
||||
{
|
||||
this.velocity.y *= -this.bounce.y;
|
||||
|
||||
var oy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
|
||||
|
||||
if (oy < -this.minBounceVelocity)
|
||||
{
|
||||
|
||||
|
||||
// if (this.velocity.y < -this.minBounceVelocity)
|
||||
// {
|
||||
// var oy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
|
||||
this.y += oy;
|
||||
this.velocity.y += this.motionVelocity.y;
|
||||
// console.log(this._debug, 'rb', this.velocity.y, 'delta', this.deltaY(), 'newy', this.y);
|
||||
// console.log(this._debug, 'rb', oy, 'delta', this.deltaY(), 'newy', this.y);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Kill it dead :)
|
||||
this.preY = this.y; // because we don't want any delta from a separation
|
||||
this.velocity.y = 0;
|
||||
this.motionVelocity.y = 0;
|
||||
// console.log(this._debug, 'void1', this.velocity.y, 'delta', this.deltaY());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Kill it dead :)
|
||||
this.preY = this.y; // because we don't want any delta from a separation
|
||||
this.velocity.y = 0;
|
||||
this.motionVelocity.y = 0;
|
||||
// console.log(this._debug, 'void2', this.velocity.y, 'delta', this.deltaY());
|
||||
}
|
||||
}
|
||||
else if (this.blocked.up && this.deltaY() < 0)
|
||||
else
|
||||
{
|
||||
this.velocity.y *= -this.bounce.y;
|
||||
}
|
||||
|
||||
if (this.collideWorldBounds)
|
||||
{
|
||||
// this.checkWorldBounds();
|
||||
this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
|
||||
this.velocity.y += this.motionVelocity.y;
|
||||
}
|
||||
|
||||
if (this.velocity.x > this.maxVelocity.x)
|
||||
@@ -515,39 +615,25 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
if (this.deltaX() < 0)
|
||||
{
|
||||
this.facing = Phaser.LEFT;
|
||||
|
||||
if (this.blocked.left === false)
|
||||
{
|
||||
this.sprite.x += this.deltaX();
|
||||
}
|
||||
this.sprite.x += this.deltaX();
|
||||
}
|
||||
else if (this.deltaX() > 0)
|
||||
{
|
||||
this.facing = Phaser.RIGHT;
|
||||
|
||||
if (this.blocked.right === false)
|
||||
{
|
||||
this.sprite.x += this.deltaX();
|
||||
}
|
||||
this.sprite.x += this.deltaX();
|
||||
}
|
||||
|
||||
if (this.deltaY() < 0)
|
||||
{
|
||||
this.facing = Phaser.UP;
|
||||
|
||||
if (this.blocked.up === false)
|
||||
{
|
||||
this.sprite.y += this.deltaY();
|
||||
}
|
||||
this.sprite.y += this.deltaY();
|
||||
// console.log(this._debug, 'post up', this.deltaY(), this.sprite.y);
|
||||
}
|
||||
else if (this.deltaY() > 0)
|
||||
{
|
||||
this.facing = Phaser.DOWN;
|
||||
|
||||
if (this.blocked.down === false)
|
||||
{
|
||||
this.sprite.y += this.deltaY();
|
||||
}
|
||||
this.sprite.y += this.deltaY();
|
||||
// console.log(this._debug, 'post down', this.deltaY(), this.sprite.y);
|
||||
}
|
||||
|
||||
this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight);
|
||||
@@ -597,13 +683,12 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
{
|
||||
this.overlapY = this.bottom - this.game.world.bounds.bottom;
|
||||
this.blocked.down = true;
|
||||
// console.log(this._debug, 'cw', this.overlapY, this.y, this.height, this.bottom, this.game.world.bounds.bottom);
|
||||
// this.y = this.game.world.bounds.bottom - this.height;
|
||||
// this.preY = this.y;
|
||||
// this.velocity.y *= -this.bounce.y;
|
||||
}
|
||||
|
||||
console.log('checkWorldBounds', this.overlapX, this.overlapY);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -718,7 +803,8 @@ Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "bottom", {
|
||||
* @return {number}
|
||||
*/
|
||||
get: function () {
|
||||
return Math.floor(this.y + this.height);
|
||||
// return Math.floor(this.y + this.height);
|
||||
return this.y + this.height;
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -754,7 +840,8 @@ Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "right", {
|
||||
* @return {number}
|
||||
*/
|
||||
get: function () {
|
||||
return Math.floor(this.x + this.width);
|
||||
// return Math.floor(this.x + this.width);
|
||||
return this.x + this.width;
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@@ -59,10 +59,10 @@ Phaser.Tileset = function (name, firstgid, width, height, margin, spacing, prope
|
||||
/**
|
||||
* @property {object} tilePproperties - Tile specific properties (typically defined in the Tiled editor).
|
||||
*/
|
||||
this.tileProperties = {};
|
||||
// this.tileProperties = {};
|
||||
|
||||
/**
|
||||
* @property {object} image - The image used for rendering.
|
||||
* @property {object} image - The image used for rendering. This is a reference to the image stored in Phaser.Cache.
|
||||
*/
|
||||
this.image = null;
|
||||
|
||||
@@ -91,12 +91,12 @@ Phaser.Tileset.prototype = {
|
||||
* @method Phaser.Tileset#getTile
|
||||
* @param {number} index - The index of the tile within the set.
|
||||
* @return {object} The tile object.
|
||||
*/
|
||||
getTile: function (index) {
|
||||
|
||||
return this.tiles[index];
|
||||
|
||||
},
|
||||
*/
|
||||
|
||||
/**
|
||||
* Gets a Tile from this set.
|
||||
@@ -104,12 +104,12 @@ Phaser.Tileset.prototype = {
|
||||
* @method Phaser.Tileset#getTileX
|
||||
* @param {number} index - The index of the tile within the set.
|
||||
* @return {object} The tile object.
|
||||
*/
|
||||
getTileX: function (index) {
|
||||
|
||||
return this.tiles[index][0];
|
||||
|
||||
},
|
||||
*/
|
||||
|
||||
/**
|
||||
* Gets a Tile from this set.
|
||||
@@ -117,12 +117,12 @@ Phaser.Tileset.prototype = {
|
||||
* @method Phaser.Tileset#getTileY
|
||||
* @param {number} index - The index of the tile within the set.
|
||||
* @return {object} The tile object.
|
||||
*/
|
||||
getTileY: function (index) {
|
||||
|
||||
return this.tiles[index][1];
|
||||
|
||||
},
|
||||
*/
|
||||
|
||||
/**
|
||||
* Sets tile spacing and margins.
|
||||
@@ -144,12 +144,12 @@ Phaser.Tileset.prototype = {
|
||||
* @method Phaser.Tileset#checkTileIndex
|
||||
* @param {number} index - The index of the tile within the set.
|
||||
* @return {boolean} True if a tile exists at the given index otherwise false.
|
||||
*/
|
||||
checkTileIndex: function (index) {
|
||||
|
||||
return (this.tiles[index]);
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user