mirror of
https://github.com/wassname/phaser.git
synced 2026-09-09 11:28:47 +08:00
Further physics modifications
This commit is contained in:
@@ -221,8 +221,8 @@ Phaser.Physics.Arcade.prototype = {
|
||||
|
||||
if (body.allowGravity)
|
||||
{
|
||||
this._gravityX = (this.gravity.x + body.gravity.x);
|
||||
this._gravityY = (this.gravity.y + body.gravity.y);
|
||||
this._gravityX = (this.gravity.x + body.gravity.x) * this.game.time.physicsElapsed;
|
||||
this._gravityY = (this.gravity.y + body.gravity.y) * this.game.time.physicsElapsed;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -236,61 +236,22 @@ Phaser.Physics.Arcade.prototype = {
|
||||
// body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);
|
||||
// body.angularVelocity += this._velocityDelta;
|
||||
|
||||
// body.motionVelocity.x = (this.computeVelocity(body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x, this._gravityX) - body.velocity.x) * 0.5;
|
||||
// body.motionVelocity.y = (this.computeVelocity(body, body.velocity.y, body.acceleration.y, body.drag.y, body.maxVelocity.y, this._gravityY) - body.velocity.y) * 0.5;
|
||||
|
||||
// velocity = velocity + gravity*delta_time/2
|
||||
// position = position + velocity*delta_time
|
||||
// velocity = velocity + gravity*delta_time/2
|
||||
|
||||
body.velocity.x += this._gravityX;
|
||||
body.velocity.y += this._gravityY;
|
||||
|
||||
body.motionVelocity.x = body.acceleration.x * this.game.time.physicsElapsed;
|
||||
body.motionVelocity.y = body.acceleration.y * this.game.time.physicsElapsed;
|
||||
|
||||
// temp = acc*dt
|
||||
// pos = pos + dt*(vel + temp/2)
|
||||
// vel = vel + temp
|
||||
|
||||
this._drag = 0;
|
||||
|
||||
if (body.drag.x !== 0 && body.velocity.x !== 0 && body.acceleration.x === 0)
|
||||
{
|
||||
if (body.velocity.x - body.drag.x > 0)
|
||||
{
|
||||
this._drag = -body.drag.x;
|
||||
}
|
||||
else if (body.velocity.x + body.drag.x < 0)
|
||||
{
|
||||
this._drag = body.drag.x;
|
||||
}
|
||||
}
|
||||
|
||||
// body.motionVelocity.x = body.acceleration.x + this._gravityX - body.drag.x * this.game.time.physicsElapsed;
|
||||
// body.motionVelocity.x = body.acceleration.x + this._gravityX * this.game.time.physicsElapsed;
|
||||
body.motionVelocity.x = (body.acceleration.x + this._gravityX + this._drag) * this.game.time.physicsElapsed;
|
||||
|
||||
this._drag = 0;
|
||||
|
||||
if (body.drag.y !== 0 && body.velocity.y !== 0 && body.acceleration.y === 0)
|
||||
{
|
||||
if (body.velocity.y - body.drag.y > 0)
|
||||
{
|
||||
this._drag = -body.drag.y;
|
||||
}
|
||||
else if (body.velocity.y + body.drag.y < 0)
|
||||
{
|
||||
this._drag = body.drag.y;
|
||||
}
|
||||
}
|
||||
|
||||
// body.motionVelocity.y = body.acceleration.y + this._gravityY - body.drag.y * this.game.time.physicsElapsed;
|
||||
// body.motionVelocity.y = body.acceleration.y + this._gravityY * this.game.time.physicsElapsed;
|
||||
body.motionVelocity.y = (body.acceleration.y + this._gravityY + this._drag) * this.game.time.physicsElapsed;
|
||||
|
||||
// if (body.sleeping === false)
|
||||
// {
|
||||
// body.x = body.x + this.game.time.physicsElapsed * (body.velocity.x + body.motionVelocity.x / 2);
|
||||
// body.velocity.x = body.velocity.x + body.motionVelocity.x;
|
||||
|
||||
// body.y = body.y + this.game.time.physicsElapsed * (body.velocity.y + body.motionVelocity.y / 2);
|
||||
// body.velocity.y = body.velocity.y + body.motionVelocity.y;
|
||||
// }
|
||||
// body.motionVelocity.x = (body.acceleration.x + this._gravityX) * this.game.time.physicsElapsed;
|
||||
// body.motionVelocity.y = (body.acceleration.y + this._gravityY) * this.game.time.physicsElapsed;
|
||||
|
||||
},
|
||||
|
||||
@@ -306,11 +267,11 @@ Phaser.Physics.Arcade.prototype = {
|
||||
* @param {number} gravity - The acceleration due to gravity. Gravity will not induce drag.
|
||||
* @return {number} The altered Velocity value.
|
||||
*/
|
||||
computeVelocity: function (body, velocity, acceleration, drag, max) {
|
||||
computeVelocity: function (body, velocity, acceleration, drag, max, gravity) {
|
||||
|
||||
max = max || 10000;
|
||||
|
||||
velocity += acceleration;
|
||||
velocity += (acceleration + gravity) * this.game.time.physicsElapsed;
|
||||
|
||||
if (acceleration === 0 && drag !== 0)
|
||||
{
|
||||
@@ -330,15 +291,6 @@ Phaser.Physics.Arcade.prototype = {
|
||||
}
|
||||
}
|
||||
|
||||
if (velocity > max)
|
||||
{
|
||||
velocity = max;
|
||||
}
|
||||
else if (velocity < -max)
|
||||
{
|
||||
velocity = -max;
|
||||
}
|
||||
|
||||
return velocity;
|
||||
|
||||
},
|
||||
|
||||
+92
-27
@@ -141,6 +141,10 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
||||
*/
|
||||
this.acceleration = new Phaser.Point();
|
||||
|
||||
this.speed = 0;
|
||||
this.angle = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} drag - The drag applied to the motion of the Body.
|
||||
*/
|
||||
@@ -303,12 +307,14 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
||||
* @property {boolean} canSleep - A Body that canSleep will have its velocity set to zero if it falls below sleepThreshold for longer than sleepDuration.
|
||||
* @default
|
||||
*/
|
||||
this.sleeping = true;
|
||||
this.canSleep = true;
|
||||
this.sleeping = false;
|
||||
this.canSleep = false;
|
||||
this.sleepMin = new Phaser.Point(-20, -20);
|
||||
this.sleepMax = new Phaser.Point(20, 20);
|
||||
this.sleepDuration = 2000; // ms
|
||||
this._sleepTimer = 0; // ms
|
||||
this._drag = 0;
|
||||
this._debug = 0;
|
||||
|
||||
/**
|
||||
* If a body is overlapping with another body, but neither of them are moving (maybe they spawned on-top of each other?) this is set to true.
|
||||
@@ -389,6 +395,9 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
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.blocked.up = false;
|
||||
this.blocked.down = false;
|
||||
this.blocked.left = false;
|
||||
@@ -398,20 +407,6 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
{
|
||||
this.game.physics.updateMotion(this);
|
||||
|
||||
if (this.collideWorldBounds)
|
||||
{
|
||||
this.checkWorldBounds();
|
||||
}
|
||||
|
||||
if (this.velocity.x > this.maxVelocity.x)
|
||||
{
|
||||
this.velocity.x = this.maxVelocity.x;
|
||||
}
|
||||
else if (this.velocity.x < -this.maxVelocity.x)
|
||||
{
|
||||
this.velocity.x = -this.maxVelocity.x;
|
||||
}
|
||||
|
||||
if (this.canSleep)
|
||||
{
|
||||
if (!this.sleeping)
|
||||
@@ -422,8 +417,6 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
{
|
||||
console.log('sleeping true');
|
||||
this.sleeping = true;
|
||||
// this.velocity.setTo(0, 0);
|
||||
// this.motionVelocity.setTo(0, 0);
|
||||
// this.preX = this.x;
|
||||
// this.preY = this.y;
|
||||
}
|
||||
@@ -458,11 +451,74 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
applyMotion: function () {
|
||||
|
||||
this.x = this.x + this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
|
||||
this.velocity.x = this.velocity.x + this.motionVelocity.x;
|
||||
// Apply drag
|
||||
if (this.drag.x !== 0 && this.acceleration.x === 0)
|
||||
{
|
||||
this._drag = this.drag.x * this.game.time.physicsElapsed;
|
||||
|
||||
this.y = this.y + this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
|
||||
this.velocity.y = this.velocity.y + this.motionVelocity.y;
|
||||
if (this.velocity.x - this._drag > 0)
|
||||
{
|
||||
this.velocity.x -= this._drag;
|
||||
}
|
||||
else if (this.velocity.x + this.drag.x < 0)
|
||||
{
|
||||
this.velocity.x += this._drag;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.velocity.x = 0;
|
||||
// this.motionVelocity.x = 0;
|
||||
}
|
||||
|
||||
this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
|
||||
this.velocity.x += this.motionVelocity.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
|
||||
this.velocity.x += this.motionVelocity.x;
|
||||
}
|
||||
|
||||
if (this.drag.y !== 0 && this.acceleration.y === 0)
|
||||
{
|
||||
this._drag = this.drag.y * this.game.time.physicsElapsed;
|
||||
|
||||
if (this.velocity.y - this._drag > 0)
|
||||
{
|
||||
this.velocity.y -= this._drag;
|
||||
}
|
||||
else if (this.velocity.y + this.drag.y < 0)
|
||||
{
|
||||
this.velocity.y += this._drag;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.velocity.y = 0;
|
||||
// this.motionVelocity.y = 0;
|
||||
}
|
||||
|
||||
this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
|
||||
this.velocity.y += this.motionVelocity.y;
|
||||
}
|
||||
else
|
||||
{
|
||||
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)
|
||||
{
|
||||
// this.velocity.x = this.maxVelocity.x;
|
||||
}
|
||||
else if (this.velocity.x < -this.maxVelocity.x)
|
||||
{
|
||||
// this.velocity.x = -this.maxVelocity.x;
|
||||
}
|
||||
|
||||
if (this.collideWorldBounds)
|
||||
{
|
||||
this.checkWorldBounds();
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
@@ -476,11 +532,6 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
if (this.moves)
|
||||
{
|
||||
if (this.collideWorldBounds)
|
||||
{
|
||||
// this.checkWorldBounds();
|
||||
}
|
||||
|
||||
if (this.deltaX() < 0 && this.blocked.left === false)
|
||||
{
|
||||
this.facing = Phaser.LEFT;
|
||||
@@ -503,6 +554,11 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
this.sprite.y += this.deltaY();
|
||||
}
|
||||
|
||||
if (this.collideWorldBounds)
|
||||
{
|
||||
this.checkWorldBounds();
|
||||
}
|
||||
|
||||
this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight);
|
||||
|
||||
if (this.allowRotation)
|
||||
@@ -511,6 +567,8 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
}
|
||||
}
|
||||
|
||||
this._debug++;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -521,17 +579,22 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
*/
|
||||
checkWorldBounds: function () {
|
||||
|
||||
var vx = this.velocity.x;
|
||||
var vy = this.velocity.y;
|
||||
|
||||
if (this.x < this.game.world.bounds.x)
|
||||
{
|
||||
this.blocked.left = true;
|
||||
this.x = this.game.world.bounds.x;
|
||||
this.velocity.x *= -this.bounce.x;
|
||||
console.log(this._debug, 'hit left', vx,'to',this.velocity.x);
|
||||
}
|
||||
else if (this.right > this.game.world.bounds.right)
|
||||
{
|
||||
this.blocked.right = true;
|
||||
this.x = this.game.world.bounds.right - this.width;
|
||||
this.velocity.x *= -this.bounce.x;
|
||||
console.log(this._debug, 'hit right', vx,'to',this.velocity.x);
|
||||
}
|
||||
|
||||
if (this.y < this.game.world.bounds.y)
|
||||
@@ -539,12 +602,14 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
this.blocked.up = true;
|
||||
this.y = this.game.world.bounds.y;
|
||||
this.velocity.y *= -this.bounce.y;
|
||||
console.log(this._debug, 'hit top', vy,'to',this.velocity.y);
|
||||
}
|
||||
else if (this.bottom > this.game.world.bounds.bottom)
|
||||
{
|
||||
this.blocked.down = true;
|
||||
this.y = this.game.world.bounds.bottom - this.height;
|
||||
this.velocity.y *= -this.bounce.y;
|
||||
console.log(this._debug, 'hit bottom', vy,'to',this.velocity.y);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user