mirror of
https://github.com/wassname/phaser.git
synced 2026-07-17 01:30:32 +08:00
Angular Physics re-implemented, most tests now working as expected.
This commit is contained in:
@@ -166,8 +166,7 @@ Phaser.Physics.Arcade.prototype = {
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#updateMotion
|
||||
* @param {Phaser.Physics.Arcade.Body} The Body object to be updated.
|
||||
*/
|
||||
OLDupdateMotion: function (body) {
|
||||
updateMotion: function (body) {
|
||||
|
||||
// If you're wondering why the velocity is halved and applied twice, read this: http://www.niksula.hut.fi/~hkankaan/Homepages/gravity.html
|
||||
|
||||
@@ -208,6 +207,7 @@ Phaser.Physics.Arcade.prototype = {
|
||||
// body.velocity.y += this._velocityDelta;
|
||||
|
||||
},
|
||||
*/
|
||||
|
||||
/**
|
||||
* Called automatically by a Physics body, it updates all motion related values on the Body.
|
||||
@@ -221,8 +221,8 @@ Phaser.Physics.Arcade.prototype = {
|
||||
|
||||
if (body.allowGravity)
|
||||
{
|
||||
this._gravityX = (this.gravity.x + body.gravity.x) * this.game.time.physicsElapsed;
|
||||
this._gravityY = (this.gravity.y + body.gravity.y) * this.game.time.physicsElapsed;
|
||||
this._gravityX = (this.gravity.x + body.gravity.x);
|
||||
this._gravityY = (this.gravity.y + body.gravity.y);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -231,27 +231,46 @@ Phaser.Physics.Arcade.prototype = {
|
||||
}
|
||||
|
||||
// Rotation
|
||||
// this._velocityDelta = (this.computeVelocity(body, body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular, 0) - body.angularVelocity) * 0.5;
|
||||
// body.angularVelocity += this._velocityDelta;
|
||||
// body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);
|
||||
// body.angularVelocity += this._velocityDelta;
|
||||
if (body.allowRotation)
|
||||
{
|
||||
this._velocityDelta = body.angularAcceleration * this.game.time.physicsElapsed;
|
||||
|
||||
if (body.angularDrag !== 0 && body.angularAcceleration === 0)
|
||||
{
|
||||
this._drag = body.angularDrag * this.game.time.physicsElapsed;
|
||||
|
||||
if (body.angularVelocity > 0)
|
||||
{
|
||||
body.angularVelocity -= this._drag;
|
||||
}
|
||||
else if (body.angularVelocity < 0)
|
||||
{
|
||||
body.angularVelocity += this._drag;
|
||||
}
|
||||
}
|
||||
|
||||
body.rotation += this.game.time.physicsElapsed * (body.angularVelocity + this._velocityDelta / 2);
|
||||
body.angularVelocity += this._velocityDelta;
|
||||
|
||||
if (body.angularVelocity > body.maxAngular)
|
||||
{
|
||||
body.angularVelocity = body.maxAngular;
|
||||
}
|
||||
else if (body.angularVelocity < -body.maxAngular)
|
||||
{
|
||||
body.angularVelocity = -body.maxAngular;
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
// body.motionVelocity.x = (body.acceleration.x + this._gravityX) * this.game.time.physicsElapsed;
|
||||
// body.motionVelocity.y = (body.acceleration.y + this._gravityY) * this.game.time.physicsElapsed;
|
||||
body.motionVelocity.x = (body.acceleration.x + this._gravityX) * this.game.time.physicsElapsed;
|
||||
body.motionVelocity.y = (body.acceleration.y + this._gravityY) * this.game.time.physicsElapsed;
|
||||
|
||||
},
|
||||
|
||||
@@ -266,64 +285,30 @@ Phaser.Physics.Arcade.prototype = {
|
||||
* @param {number} [max=10000] - An absolute value cap for the velocity.
|
||||
* @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, gravity) {
|
||||
|
||||
max = max || 10000;
|
||||
|
||||
velocity += (acceleration + gravity) * this.game.time.physicsElapsed;
|
||||
|
||||
if (acceleration === 0 && drag !== 0)
|
||||
{
|
||||
this._drag = drag;
|
||||
|
||||
if (velocity - this._drag > 0)
|
||||
{
|
||||
velocity -= this._drag;
|
||||
}
|
||||
else if (velocity + this._drag < 0)
|
||||
{
|
||||
velocity += this._drag;
|
||||
}
|
||||
else
|
||||
{
|
||||
velocity = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return velocity;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* A tween-like function that takes a starting velocity and some other factors and returns an altered velocity.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#computeVelocity
|
||||
* @param {Phaser.Physics.Arcade.Body} body - The Body object to be updated.
|
||||
* @param {number} velocity - Any component of velocity (e.g. 20).
|
||||
* @param {number} acceleration - Rate at which the velocity is changing.
|
||||
* @param {number} drag - Really kind of a deceleration, this is how much the velocity changes if Acceleration is not set.
|
||||
* @param {number} [max=10000] - An absolute value cap for the velocity.
|
||||
* @param {number} gravity - The acceleration due to gravity. Gravity will not induce drag.
|
||||
* @return {number} The altered Velocity value.
|
||||
*/
|
||||
OLDcomputeVelocity: function (body, velocity, acceleration, drag, max, gravity) {
|
||||
|
||||
max = max || 10000;
|
||||
|
||||
velocity += (acceleration + gravity) * this.game.time.physicsElapsed;
|
||||
// velocity = (acceleration + gravity) * this.game.time.physicsElapsed;
|
||||
// velocity += (acceleration + gravity) * this.game.time.physicsElapsed;
|
||||
velocity = (acceleration + gravity);
|
||||
|
||||
if (acceleration === 0 && drag !== 0)
|
||||
{
|
||||
this._drag = drag * this.game.time.physicsElapsed;
|
||||
// this._drag = drag;
|
||||
|
||||
// if (velocity - drag > 0)
|
||||
if (velocity - this._drag > 0)
|
||||
{
|
||||
velocity -= this._drag;
|
||||
// velocity += this._drag;
|
||||
velocity -= drag;
|
||||
}
|
||||
else if (velocity + this._drag < 0)
|
||||
// else if (velocity - drag < 0)
|
||||
else if (velocity - this._drag < 0)
|
||||
{
|
||||
velocity += this._drag;
|
||||
// velocity -= this._drag;
|
||||
velocity -= drag;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -343,6 +328,7 @@ Phaser.Physics.Arcade.prototype = {
|
||||
return velocity;
|
||||
|
||||
},
|
||||
*/
|
||||
|
||||
/**
|
||||
* Called automatically by the core game loop.
|
||||
|
||||
+66
-16
@@ -308,13 +308,15 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
||||
* @default
|
||||
*/
|
||||
this.sleeping = false;
|
||||
this.canSleep = false;
|
||||
this.canSleep = true;
|
||||
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;
|
||||
// this.friction = 0.99;
|
||||
// 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.
|
||||
@@ -415,10 +417,7 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
{
|
||||
if (this._sleepTimer >= this.sleepDuration)
|
||||
{
|
||||
console.log('sleeping true');
|
||||
this.sleeping = true;
|
||||
// this.preX = this.x;
|
||||
// this.preY = this.y;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -451,7 +450,43 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
applyMotion: function () {
|
||||
|
||||
// Apply drag
|
||||
// Apply drag - this should be proportionally applied, not linearly like below
|
||||
if (this.drag.x !== 0 && this.acceleration.x === 0)
|
||||
{
|
||||
this._drag = this.drag.x * this.game.time.physicsElapsed;
|
||||
|
||||
if (this.velocity.x > 0)
|
||||
{
|
||||
this.velocity.x -= this._drag;
|
||||
}
|
||||
else if (this.velocity.x < 0)
|
||||
{
|
||||
this.velocity.x += this._drag;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.drag.y !== 0 && this.acceleration.y === 0)
|
||||
{
|
||||
this._drag = this.drag.y * this.game.time.physicsElapsed;
|
||||
|
||||
if (this.velocity.y > 0)
|
||||
{
|
||||
this.velocity.y -= this._drag;
|
||||
}
|
||||
else if (this.velocity.y < 0)
|
||||
{
|
||||
this.velocity.y += this._drag;
|
||||
}
|
||||
}
|
||||
|
||||
this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
|
||||
this.velocity.x += this.motionVelocity.x;
|
||||
|
||||
this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
|
||||
this.velocity.y += this.motionVelocity.y;
|
||||
|
||||
|
||||
/*
|
||||
if (this.drag.x !== 0 && this.acceleration.x === 0)
|
||||
{
|
||||
this._drag = this.drag.x * this.game.time.physicsElapsed;
|
||||
@@ -459,19 +494,21 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
if (this.velocity.x - this._drag > 0)
|
||||
{
|
||||
this.velocity.x -= this._drag;
|
||||
this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
|
||||
this.velocity.x += this.motionVelocity.x;
|
||||
}
|
||||
else if (this.velocity.x + this.drag.x < 0)
|
||||
{
|
||||
this.velocity.x += this._drag;
|
||||
this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
|
||||
this.velocity.x += this.motionVelocity.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.velocity.x = 0;
|
||||
// this.preX = this.x;
|
||||
// this.motionVelocity.x = 0;
|
||||
}
|
||||
|
||||
this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
|
||||
this.velocity.x += this.motionVelocity.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -486,25 +523,28 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
if (this.velocity.y - this._drag > 0)
|
||||
{
|
||||
this.velocity.y -= this._drag;
|
||||
this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
|
||||
this.velocity.y += this.motionVelocity.y;
|
||||
}
|
||||
else if (this.velocity.y + this.drag.y < 0)
|
||||
{
|
||||
this.velocity.y += this._drag;
|
||||
this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
|
||||
this.velocity.y += this.motionVelocity.y;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.velocity.y = 0;
|
||||
// this.preY = this.y;
|
||||
// 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)
|
||||
{
|
||||
@@ -518,6 +558,16 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
if (this.collideWorldBounds)
|
||||
{
|
||||
this.checkWorldBounds();
|
||||
|
||||
if (this.blocked.left || this.blocked.right)
|
||||
{
|
||||
// this.preX = this.x;
|
||||
}
|
||||
|
||||
if (this.blocked.up || this.blocked.down)
|
||||
{
|
||||
// this.preX = this.x;
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
@@ -556,7 +606,7 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
if (this.collideWorldBounds)
|
||||
{
|
||||
this.checkWorldBounds();
|
||||
// this.checkWorldBounds();
|
||||
}
|
||||
|
||||
this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight);
|
||||
@@ -587,14 +637,14 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
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);
|
||||
// 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);
|
||||
// console.log(this._debug, 'hit right', vx,'to',this.velocity.x);
|
||||
}
|
||||
|
||||
if (this.y < this.game.world.bounds.y)
|
||||
@@ -602,14 +652,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);
|
||||
// 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);
|
||||
// console.log(this._debug, 'hit bottom', vy,'to',this.velocity.y);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
+4
-3
@@ -454,15 +454,16 @@ Phaser.Utils.Debug.prototype = {
|
||||
|
||||
this.start(x, y, color, 220);
|
||||
|
||||
this.splitline('Width: ' + sprite.width, 'Height: ' + sprite.height);
|
||||
this.splitline('x: ' + sprite.body.x.toFixed(2), 'y: ' + sprite.body.y.toFixed(2));
|
||||
this.splitline('x: ' + sprite.body.x.toFixed(2), 'y: ' + sprite.body.y.toFixed(2), 'width: ' + sprite.width, 'height: ' + sprite.height);
|
||||
this.splitline('speed: ' + sprite.body.speed.toFixed(2), 'angle: ' + sprite.body.angle.toFixed(2));
|
||||
this.splitline('old x: ' + sprite.body.preX.toFixed(2), 'y: ' + sprite.body.preY.toFixed(2));
|
||||
//this.splitline('old x: ' + sprite.body.preX.toFixed(2), 'y: ' + sprite.body.preY.toFixed(2));
|
||||
this.splitline('drag x: ' + sprite.body.drag.x, 'y: ' + sprite.body.drag.y);
|
||||
this.splitline('gravity x: ' + sprite.body.gravity.x, 'y: ' + sprite.body.gravity.y);
|
||||
this.splitline('world gravity x: ' + this.game.physics.gravity.x, 'y: ' + this.game.physics.gravity.y);
|
||||
this.splitline('acceleration x: ' + sprite.body.acceleration.x.toFixed(2), 'y: ' + sprite.body.acceleration.y.toFixed(2));
|
||||
this.splitline('velocity x: ' + sprite.body.velocity.x.toFixed(2), 'y: ' + sprite.body.velocity.y.toFixed(2));
|
||||
this.splitline('motion x: ' + sprite.body.motionVelocity.x.toFixed(2), 'y: ' + sprite.body.motionVelocity.y.toFixed(2));
|
||||
this.splitline('bounce x: ' + sprite.body.bounce.x.toFixed(2), 'y: ' + sprite.body.bounce.y.toFixed(2));
|
||||
this.splitline('sleeping: ' + sprite.body.sleeping, 'sleepTimer: ' + sprite.body._sleepTimer.toFixed(2));
|
||||
this.splitline('sleepMin x: ' + sprite.body.sleepMin.x, 'y: ' + sprite.body.sleepMin.y);
|
||||
this.splitline('sleepMax x: ' + sprite.body.sleepMax.x, 'y: ' + sprite.body.sleepMax.y);
|
||||
|
||||
Reference in New Issue
Block a user