Sleeping can now force a position

This commit is contained in:
Richard Davey 2019-03-20 14:59:53 +00:00
parent ec1b765833
commit 0f59b326f6

View file

@ -1003,28 +1003,37 @@ var Body = new Class({
this.prev.x = this.x; this.prev.x = this.x;
this.prev.y = this.y; this.prev.y = this.y;
this.preRotation = this.rotation; this.preRotation = this.rotation;
if (window.dump)
{
console.log('preUpdate v', this.gameObject.name, 'vel', this.velocity.y);
if (this.gameObject.name === 'bit01')
{
window.dump = false;
}
}
}, },
sleep: function () sleep: function (forceY)
{ {
if (!this.sleeping) if (!this.sleeping)
{ {
this.sleeping = true; this.sleeping = true;
console.log('put to sleep'); console.log(this.gameObject.name, 'put to sleep on frame', this.world._frame);
this.velocity.set(0); this.velocity.set(0);
this.prevVelocity.set(0); this.prevVelocity.set(0);
this.speed = 0;
this.forcePosition = true;
var worldBlocked = this.worldBlocked;
if (forceY && !worldBlocked.none)
{
var worldBounds = this.world.bounds;
if (worldBlocked.down)
{
this.bottom = worldBounds.bottom;
}
else if (worldBlocked.up)
{
this.y = worldBounds.y;
}
}
} }
}, },
@ -1047,7 +1056,7 @@ var Body = new Class({
} }
}, },
updateBlockers: function () checkBlockers: function ()
{ {
// Iterate through the list of previous frame blockers and see if they are still there // Iterate through the list of previous frame blockers and see if they are still there
@ -1086,7 +1095,7 @@ var Body = new Class({
*/ */
update: function (delta) update: function (delta)
{ {
this.updateBlockers(); this.checkBlockers();
var velocity = this.velocity; var velocity = this.velocity;
var position = this.position; var position = this.position;
@ -1137,7 +1146,26 @@ var Body = new Class({
// And finally we'll integrate the new position back to the Sprite in postUpdate // And finally we'll integrate the new position back to the Sprite in postUpdate
}, },
// Return true if body should be repositioned, otherwise return false to avoid positioning movingY: function ()
{
if (this.sleeping || this.physicsType === CONST.STATIC_BODY)
{
return false;
}
var velocityY = this.velocity.y;
if ((velocityY <= 0 && this.isWorldBlockedUp()) || (velocityY >= 0 && this.isWorldBlockedDown()))
{
return false;
}
var actualVelocityY = Math.abs(velocityY) - Math.abs(this._gy);
return (actualVelocityY > this.minVelocity.y);
},
// Return true if body can be repositioned after this call, otherwise return false to stop positioning in the update
checkWorldRebound: function () checkWorldRebound: function ()
{ {
var blocked = this.blocked; var blocked = this.blocked;
@ -1155,6 +1183,11 @@ var Body = new Class({
return true; return true;
} }
if (this.sleeping)
{
return false;
}
// Reverse the velocity for the world bounce? // Reverse the velocity for the world bounce?
if ( if (
(by !== 0) && (by !== 0) &&
@ -1172,16 +1205,17 @@ var Body = new Class({
// Gravity is pulling them down // Gravity is pulling them down
if (newVelocityY > 0 && (newVelocityY < gravityY || FuzzyLessThan(newVelocityY, gravityY, this.minVelocity.y))) if (newVelocityY > 0 && (newVelocityY < gravityY || FuzzyLessThan(newVelocityY, gravityY, this.minVelocity.y)))
{ {
this.sleep(); console.log('frame', this.world._frame, this.gameObject.name, 'rebound up too small, sending to sleep', newVelocityY, gravityY);
this.sleep(true);
console.log(this.gameObject.name, 'rebound up too small, zeroing', newVelocityY, gravityY);
console.log('zero y', this.y, 'gy', this.gameObject.y, worldBlocked.down); console.log('zero y', this.y, 'gy', this.gameObject.y, worldBlocked.down);
return false; return false;
} }
else else
{ {
velocity.y *= -by; velocity.y *= -by;
console.log(this.gameObject.name, 'rebounded up', newVelocityY, gravityY); console.log(this.gameObject.name, 'rebounded up', newVelocityY, gravityY, 'frame', this.world._frame);
if (this.onWorldBounds) if (this.onWorldBounds)
{ {
@ -1196,15 +1230,16 @@ var Body = new Class({
// Gravity is pulling them up // Gravity is pulling them up
if (newVelocityY < 0 && (newVelocityY > gravityY || FuzzyGreaterThan(newVelocityY, gravityY, this.minVelocity.y))) if (newVelocityY < 0 && (newVelocityY > gravityY || FuzzyGreaterThan(newVelocityY, gravityY, this.minVelocity.y)))
{ {
this.sleep(); console.log(this.gameObject.name, 'rebound down too small, sending to sleep', newVelocityY, gravityY);
this.sleep(true);
console.log(this.gameObject.name, 'rebound down too small, zeroing', newVelocityY, gravityY);
return false; return false;
} }
else else
{ {
velocity.y *= -by; velocity.y *= -by;
console.log(this.gameObject.name, 'rebounded down', newVelocityY, gravityY); console.log(this.gameObject.name, 'rebounded down', newVelocityY, gravityY, 'frame', this.world._frame);
if (this.onWorldBounds) if (this.onWorldBounds)
{ {
@ -1220,9 +1255,10 @@ var Body = new Class({
if (FuzzyEqual(newVelocityY, 0, this.minVelocity.y)) if (FuzzyEqual(newVelocityY, 0, this.minVelocity.y))
{ {
this.sleep(); console.log(this.gameObject.name, 'rebound zero g too small, sending to sleep', newVelocityY, gravityY, 'y pos', this.bottom);
this.sleep(true);
console.log(this.gameObject.name, 'rebound zero g too small, zeroing', newVelocityY, gravityY, 'y pos', this.bottom);
return false; return false;
} }
else else
@ -1256,6 +1292,16 @@ var Body = new Class({
var gameObject = this.gameObject; var gameObject = this.gameObject;
// var newVelocityY = Math.abs(this.velocity.y);
// if (newVelocityY < this.minVelocity.y)
// {
// if (this.isBlockedDown())
// {
// }
// }
if (this.moves) if (this.moves)
{ {
var mx = this.deltaMax.x; var mx = this.deltaMax.x;
@ -1315,17 +1361,17 @@ var Body = new Class({
{ {
gameObject.x += dx; gameObject.x += dx;
gameObject.y += dy; gameObject.y += dy;
if (this.allowRotation)
{
gameObject.angle += this.deltaZ();
}
} }
} }
this._dx = dx; this._dx = dx;
this._dy = dy; this._dy = dy;
if (this.allowRotation && !this.sleeping)
{
gameObject.angle += this.deltaZ();
}
this.checkSleep(dx, dy); this.checkSleep(dx, dy);
// Store collision flags // Store collision flags
@ -1346,13 +1392,13 @@ var Body = new Class({
wasBlocked.left = worldBlocked.left; wasBlocked.left = worldBlocked.left;
wasBlocked.right = worldBlocked.right; wasBlocked.right = worldBlocked.right;
if (window.dump)
{
console.log('postUpdate v', this.gameObject.name, 'vel', this.velocity.y);
}
this.prevVelocity.x = this.velocity.x; this.prevVelocity.x = this.velocity.x;
this.prevVelocity.y = this.velocity.y; this.prevVelocity.y = this.velocity.y;
if (!this.sleeping)
{
// console.log('frame', this.world._frame, this.gameObject.name, 'vy', this.velocity.y, 'sleep', this._sleep, 'y', this.y, 'gy', this.gameObject.y, 'blocked', this.blocked.down);
}
}, },
// return true if gravity is pulling up and body blocked up, // return true if gravity is pulling up and body blocked up,
@ -1382,6 +1428,8 @@ var Body = new Class({
{ {
this._sleep++; this._sleep++;
// console.log(this.gameObject.name, 'sleep y', this.y);
if (this._sleep >= this.sleepIterations) if (this._sleep >= this.sleepIterations)
{ {
this.sleep(); this.sleep();
@ -2133,7 +2181,8 @@ var Body = new Class({
this.setBlocker(by); this.setBlocker(by);
if (forceY) // if (forceY && !this.worldBlocked.up)
if (!this.worldBlocked.up && this.velocity.y <= 0)
{ {
this.y = by.bottom; this.y = by.bottom;
this.forcePosition = true; this.forcePosition = true;
@ -2158,7 +2207,8 @@ var Body = new Class({
this.setBlocker(by); this.setBlocker(by);
if (forceY) // if (forceY && !this.worldBlocked.down)
if (!this.worldBlocked.down && this.velocity.y >= 0)
{ {
this.bottom = by.y; this.bottom = by.y;
this.forcePosition = true; this.forcePosition = true;
@ -2221,6 +2271,7 @@ var Body = new Class({
if (forceY !== undefined && !this.wasBlocked.down) if (forceY !== undefined && !this.wasBlocked.down)
{ {
console.log(this.gameObject.name, 'world blocked down + position');
this.bottom = forceY; this.bottom = forceY;
this.forcePosition = true; this.forcePosition = true;
} }