mirror of
https://github.com/wassname/phaser.git
synced 2026-06-29 16:30:29 +08:00
PointProxy added to allow for easy setting of force and velocity. More p2 tests done. World update done.
This commit is contained in:
@@ -179,6 +179,7 @@
|
||||
<script src="$path/src/utils/Color.js"></script>
|
||||
|
||||
<script src="$path/src/physics/World.js"></script>
|
||||
<script src="$path/src/physics/PointProxy.js"></script>
|
||||
<script src="$path/src/physics/Body.js"></script>
|
||||
|
||||
<script src="$path/src/particles/Particles.js"></script>
|
||||
|
||||
+31
-1
@@ -9,6 +9,7 @@ function preload() {
|
||||
|
||||
var box;
|
||||
var box2;
|
||||
var cursors;
|
||||
|
||||
function p2px(v) {
|
||||
return v *= -20;
|
||||
@@ -24,7 +25,13 @@ function create() {
|
||||
|
||||
box2 = game.add.sprite(400, 0, 'box');
|
||||
box2.physicsEnabled = true;
|
||||
box2.body.mass = 2;
|
||||
|
||||
box2.body.setZeroDamping();
|
||||
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
|
||||
|
||||
// box2.body.mass = 2;
|
||||
|
||||
/*
|
||||
// Add a plane
|
||||
@@ -39,10 +46,33 @@ function create() {
|
||||
|
||||
function update() {
|
||||
|
||||
box2.body.setZeroVelocity();
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
box2.body.moveLeft(200);
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
box2.body.moveRight(200);
|
||||
}
|
||||
|
||||
if (cursors.up.isDown)
|
||||
{
|
||||
box2.body.moveUp(200);
|
||||
}
|
||||
else if (cursors.down.isDown)
|
||||
{
|
||||
box2.body.moveDown(200);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
game.debug.renderText('x: ' + box2.body.velocity.x, 32, 32);
|
||||
game.debug.renderText('y: ' + box2.body.velocity.y, 32, 64);
|
||||
|
||||
// game.debug.renderText('x: ' + p2px(boxBody.position[0]), 32, 32);
|
||||
// game.debug.renderText('y: ' + p2px(boxBody.position[1]), 32, 64);
|
||||
// game.debug.renderText('r: ' + boxBody.angle, 32, 96);
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('box', 'assets/sprites/block.png');
|
||||
|
||||
}
|
||||
|
||||
var box;
|
||||
var move = false;
|
||||
var start = 0;
|
||||
var end = 0;
|
||||
|
||||
function create() {
|
||||
|
||||
box = game.add.sprite(200, 200, 'box');
|
||||
box.physicsEnabled = true;
|
||||
|
||||
box.body.setZeroDamping();
|
||||
|
||||
game.input.onDown.addOnce(startTiming, this);
|
||||
|
||||
}
|
||||
|
||||
function startTiming() {
|
||||
|
||||
start = game.time.now;
|
||||
end = start + 1000;
|
||||
move = true;
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
if (move)
|
||||
{
|
||||
box.body.moveLeft(100);
|
||||
|
||||
if (game.time.now >= end)
|
||||
{
|
||||
move = false;
|
||||
var duration = game.time.now - start;
|
||||
console.log('Test over. Distance: ', box.x, 'duration', duration);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
box.body.setZeroVelocity();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
game.debug.renderText('x: ' + box.body.velocity.x, 32, 32);
|
||||
game.debug.renderText('y: ' + box.body.velocity.y, 32, 64);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('box', 'assets/sprites/block.png');
|
||||
|
||||
}
|
||||
|
||||
var box;
|
||||
var box2;
|
||||
var cursors;
|
||||
|
||||
function p2px(v) {
|
||||
return v *= -20;
|
||||
}
|
||||
|
||||
function px2p(v) {
|
||||
return v * -0.05;
|
||||
}
|
||||
|
||||
function create() {
|
||||
|
||||
box = game.add.sprite(200, 200, 'box');
|
||||
box.anchor.set(0.5);
|
||||
box.physicsEnabled = true;
|
||||
|
||||
box2 = game.add.sprite(400, 100, 'box');
|
||||
box2.anchor.set(0.5);
|
||||
box2.physicsEnabled = true;
|
||||
|
||||
box2.body.setZeroDamping();
|
||||
box2.body.fixedRotation = true;
|
||||
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
|
||||
game.physics.defaultRestitution = 0.8;
|
||||
|
||||
var top = new p2.Body({ mass: 0, position:[0, 0], angle: game.math.degToRad(-180) });
|
||||
top.addShape(new p2.Plane());
|
||||
game.physics.addBody(top);
|
||||
|
||||
var bottom = new p2.Body({ mass: 0, position:[0, px2p(600)] });
|
||||
bottom.addShape(new p2.Plane());
|
||||
game.physics.addBody(bottom);
|
||||
|
||||
var left = new p2.Body({ mass: 0, position:[0, 0], angle: game.math.degToRad(90) });
|
||||
left.addShape(new p2.Plane());
|
||||
game.physics.addBody(left);
|
||||
|
||||
var right = new p2.Body({ mass: 0, position:[px2p(800), 0], angle: game.math.degToRad(-90) });
|
||||
right.addShape(new p2.Plane());
|
||||
game.physics.addBody(right);
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
box2.body.setZeroVelocity();
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
box2.body.moveLeft(400);
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
box2.body.moveRight(400);
|
||||
}
|
||||
|
||||
if (cursors.up.isDown)
|
||||
{
|
||||
box2.body.moveUp(400);
|
||||
}
|
||||
else if (cursors.down.isDown)
|
||||
{
|
||||
box2.body.moveDown(400);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
game.debug.renderText('x: ' + box2.body.velocity.x, 32, 32);
|
||||
game.debug.renderText('y: ' + box2.body.velocity.y, 32, 64);
|
||||
|
||||
// game.debug.renderText('x: ' + p2px(boxBody.position[0]), 32, 32);
|
||||
// game.debug.renderText('y: ' + p2px(boxBody.position[1]), 32, 64);
|
||||
// game.debug.renderText('r: ' + boxBody.angle, 32, 96);
|
||||
|
||||
// drawbox();
|
||||
|
||||
}
|
||||
|
||||
function drawbox() {
|
||||
|
||||
// var ctx = game.context;
|
||||
|
||||
/*
|
||||
ctx.save();
|
||||
ctx.translate(game.width/2, game.height/2); // Translate to the center
|
||||
ctx.scale(50, -50); // Zoom in and flip y axis
|
||||
|
||||
ctx.lineWidth = 0.05;
|
||||
ctx.strokeStyle = 'rgb(255,255,255)';
|
||||
|
||||
|
||||
ctx.beginPath();
|
||||
var x = boxBody.position[0],
|
||||
y = boxBody.position[1];
|
||||
ctx.save();
|
||||
ctx.translate(x, y); // Translate to the center of the box
|
||||
ctx.rotate(boxBody.angle); // Rotate to the box body frame
|
||||
ctx.rect(-boxShape.width/2, -boxShape.height/2, boxShape.width, boxShape.height);
|
||||
ctx.stroke();
|
||||
ctx.closePath();
|
||||
// ctx.restore();
|
||||
*/
|
||||
|
||||
// ctx.save();
|
||||
// ctx.translate(game.width/2, game.height/2); // Translate to the center
|
||||
// ctx.scale(20, -20); // Zoom in and flip y axis
|
||||
|
||||
// ctx.lineWidth = 0.05;
|
||||
// ctx.strokeStyle = 'rgb(255,255,255)';
|
||||
// ctx.beginPath();
|
||||
|
||||
// var y = planeBody.position[1];
|
||||
// ctx.rotate(0); // Rotate to the box body frame
|
||||
// ctx.moveTo(-game.width, y);
|
||||
// ctx.lineTo( game.width, y);
|
||||
// ctx.stroke();
|
||||
|
||||
// ctx.closePath();
|
||||
// ctx.restore();
|
||||
}
|
||||
|
||||
+2
-3
@@ -447,7 +447,6 @@ Phaser.Game.prototype = {
|
||||
this.tweens = new Phaser.TweenManager(this);
|
||||
this.input = new Phaser.Input(this);
|
||||
this.sound = new Phaser.SoundManager(this);
|
||||
// this.physics = new Phaser.Physics.Arcade(this);
|
||||
this.physics = new Phaser.Physics.World(this);
|
||||
this.particles = new Phaser.Particles(this);
|
||||
this.plugins = new Phaser.PluginManager(this, this);
|
||||
@@ -616,11 +615,11 @@ Phaser.Game.prototype = {
|
||||
this.tweens.update();
|
||||
this.sound.update();
|
||||
this.input.update();
|
||||
this.physics.update();
|
||||
this.state.update();
|
||||
this.world.update();
|
||||
this.physics.update();
|
||||
this.particles.update();
|
||||
this.plugins.update();
|
||||
this.world.update();
|
||||
|
||||
this.world.postUpdate();
|
||||
this.plugins.postUpdate();
|
||||
|
||||
+165
-42
@@ -31,10 +31,16 @@ Phaser.Physics.Body = function (sprite) {
|
||||
*/
|
||||
this.offset = new Phaser.Point();
|
||||
|
||||
// force
|
||||
|
||||
this.shape = null;
|
||||
|
||||
// this.data = new p2.Body({ mass: 0, position:[this.px2p(sprite.x), this.px2p(sprite.y)], angularVelocity: 1 });
|
||||
this.data = new p2.Body({ position:[this.px2p(sprite.x), this.px2p(sprite.y)] });
|
||||
this.data = new p2.Body({ position:[this.px2p(sprite.x), this.px2p(sprite.y)], mass: 1 });
|
||||
|
||||
/**
|
||||
* @property {Phaser.PointProxy} velocity - The velocity of the body. Set velocity.x to a negative value to move to the left, position to the right. velocity.y negative values move up, positive move down.
|
||||
*/
|
||||
this.velocity = new Phaser.Physics.PointProxy(this.data.velocity);
|
||||
|
||||
/**
|
||||
* @property {number} _sx - Internal cache var.
|
||||
@@ -118,6 +124,82 @@ Phaser.Physics.Body.prototype = {
|
||||
// toLocalFrame
|
||||
// toWorldFrame
|
||||
|
||||
/**
|
||||
* If this Body is dynamic then this will zero its velocity on both axis.
|
||||
*
|
||||
* @method Phaser.Physics.Body#setZeroVelocity
|
||||
*/
|
||||
setZeroVelocity: function () {
|
||||
|
||||
this.data.velocity[0] = 0;
|
||||
this.data.velocity[1] = 0;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets the Body damping and angularDamping to zero.
|
||||
*
|
||||
* @method Phaser.Physics.Body#setZeroDamping
|
||||
*/
|
||||
setZeroDamping: function () {
|
||||
|
||||
this.data.damping = 0;
|
||||
this.data.angularDamping = 0;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* If this Body is dynamic then this will move it to the left by setting its x velocity to the given speed.
|
||||
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
|
||||
*
|
||||
* @method Phaser.Physics.Body#moveLeft
|
||||
* @param {number} speed - The speed at which it should move to the left, in pixels per second.
|
||||
*/
|
||||
moveLeft: function (speed) {
|
||||
|
||||
this.data.velocity[0] = this.px2p(-speed);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* If this Body is dynamic then this will move it to the right by setting its x velocity to the given speed.
|
||||
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
|
||||
*
|
||||
* @method Phaser.Physics.Body#moveRight
|
||||
* @param {number} speed - The speed at which it should move to the right, in pixels per second.
|
||||
*/
|
||||
moveRight: function (speed) {
|
||||
|
||||
this.data.velocity[0] = this.px2p(speed);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* If this Body is dynamic then this will move it up by setting its y velocity to the given speed.
|
||||
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
|
||||
*
|
||||
* @method Phaser.Physics.Body#moveUp
|
||||
* @param {number} speed - The speed at which it should move up, in pixels per second.
|
||||
*/
|
||||
moveUp: function (speed) {
|
||||
|
||||
this.data.velocity[1] = this.px2p(-speed);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* If this Body is dynamic then this will move it down by setting its y velocity to the given speed.
|
||||
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
|
||||
*
|
||||
* @method Phaser.Physics.Body#moveDown
|
||||
* @param {number} speed - The speed at which it should move down, in pixels per second.
|
||||
*/
|
||||
moveDown: function (speed) {
|
||||
|
||||
this.data.velocity[1] = this.px2p(speed);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal method that updates the Body scale in relation to the parent Sprite.
|
||||
*
|
||||
@@ -401,6 +483,87 @@ Phaser.Physics.Body.prototype = {
|
||||
|
||||
Phaser.Physics.Body.prototype.constructor = Phaser.Physics.Body;
|
||||
|
||||
/**
|
||||
* @name Phaser.Physics.Body#static
|
||||
* @property {boolean} static - Returns true if the Body is static. Setting Body.static to 'false' will make it dynamic.
|
||||
*/
|
||||
Object.defineProperty(Phaser.Physics.Body.prototype, "static", {
|
||||
|
||||
get: function () {
|
||||
|
||||
return (this.data.motionState === Phaser.STATIC);
|
||||
|
||||
},
|
||||
|
||||
set: function (value) {
|
||||
|
||||
if (value && this.data.motionState !== Phaser.STATIC)
|
||||
{
|
||||
this.data.motionState = Phaser.STATIC;
|
||||
}
|
||||
else if (!value && this.data.motionState === Phaser.STATIC)
|
||||
{
|
||||
this.data.motionState = Phaser.DYNAMIC;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* @name Phaser.Physics.Body#dynamic
|
||||
* @property {boolean} dynamic - Returns true if the Body is dynamic. Setting Body.dynamic to 'false' will make it static.
|
||||
*/
|
||||
Object.defineProperty(Phaser.Physics.Body.prototype, "dynamic", {
|
||||
|
||||
get: function () {
|
||||
|
||||
return (this.data.motionState === Phaser.DYNAMIC);
|
||||
|
||||
},
|
||||
|
||||
set: function (value) {
|
||||
|
||||
if (value && this.data.motionState !== Phaser.DYNAMIC)
|
||||
{
|
||||
this.data.motionState = Phaser.DYNAMIC;
|
||||
}
|
||||
else if (!value && this.data.motionState === Phaser.DYNAMIC)
|
||||
{
|
||||
this.data.motionState = Phaser.STATIC;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* @name Phaser.Physics.Body#kinematic
|
||||
* @property {boolean} kinematic - Returns true if the Body is kinematic. Setting Body.kinematic to 'false' will make it static.
|
||||
*/
|
||||
Object.defineProperty(Phaser.Physics.Body.prototype, "kinematic", {
|
||||
|
||||
get: function () {
|
||||
|
||||
return (this.data.motionState === Phaser.KINEMATIC);
|
||||
|
||||
},
|
||||
|
||||
set: function (value) {
|
||||
|
||||
if (value && this.data.motionState !== Phaser.KINEMATIC)
|
||||
{
|
||||
this.data.motionState = Phaser.KINEMATIC;
|
||||
}
|
||||
else if (!value && this.data.motionState === Phaser.KINEMATIC)
|
||||
{
|
||||
this.data.motionState = Phaser.STATIC;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* @name Phaser.Physics.Body#allowSleep
|
||||
* @property {boolean} allowSleep -
|
||||
@@ -554,8 +717,6 @@ Object.defineProperty(Phaser.Physics.Body.prototype, "fixedRotation", {
|
||||
|
||||
});
|
||||
|
||||
// force
|
||||
|
||||
/**
|
||||
* @name Phaser.Physics.Body#inertia
|
||||
* @property {number} inertia - The inertia of the body around the Z axis..
|
||||
@@ -667,8 +828,6 @@ Object.defineProperty(Phaser.Physics.Body.prototype, "sleepSpeedLimit", {
|
||||
|
||||
});
|
||||
|
||||
// velocity
|
||||
|
||||
/**
|
||||
* @name Phaser.Physics.Body#x
|
||||
* @property {number} x - The x coordinate of this Body.
|
||||
@@ -708,39 +867,3 @@ Object.defineProperty(Phaser.Physics.Body.prototype, "y", {
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Object.defineProperties(Phaser.Physics.Body.prototype, {
|
||||
|
||||
"bob": {
|
||||
|
||||
get: function () {
|
||||
|
||||
return { x: 1, y: 2 };
|
||||
|
||||
},
|
||||
|
||||
set: function (value) {
|
||||
|
||||
console.log(arguments);
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
"ben": {
|
||||
|
||||
get: function () {
|
||||
|
||||
return { x: 4, y: 5 };
|
||||
|
||||
},
|
||||
|
||||
set: function (value) {
|
||||
|
||||
console.log(arguments);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2014 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
/**
|
||||
* A PointProxy is an internal class that allows for direct getter/setter style property access to Arrays and TypedArrays.
|
||||
*
|
||||
* @class Phaser.Physics.PointProxy
|
||||
* @classdesc PointProxy
|
||||
* @constructor
|
||||
* @param {any} destination - The object to bind to.
|
||||
*/
|
||||
Phaser.Physics.PointProxy = function (destination) {
|
||||
|
||||
this.destination = destination;
|
||||
|
||||
};
|
||||
|
||||
Phaser.Physics.PointProxy.prototype.constructor = Phaser.Physics.PointProxy;
|
||||
|
||||
/**
|
||||
* @name Phaser.Physics.PointProxy#x
|
||||
* @property {number} x - The x property of this PointProxy.
|
||||
*/
|
||||
Object.defineProperty(Phaser.Physics.PointProxy.prototype, "x", {
|
||||
|
||||
get: function () {
|
||||
|
||||
return this.destination[0];
|
||||
|
||||
},
|
||||
|
||||
set: function (value) {
|
||||
|
||||
this.destination[0] = value;
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* @name Phaser.Physics.PointProxy#y
|
||||
* @property {number} y - The y property of this PointProxy.
|
||||
*/
|
||||
Object.defineProperty(Phaser.Physics.PointProxy.prototype, "y", {
|
||||
|
||||
get: function () {
|
||||
|
||||
return this.destination[1];
|
||||
|
||||
},
|
||||
|
||||
set: function (value) {
|
||||
|
||||
this.destination[1] = value;
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
@@ -22,7 +22,7 @@ Phaser.Physics.World = function (game) {
|
||||
*/
|
||||
this.game = game;
|
||||
|
||||
p2.World.call(this);
|
||||
p2.World.call(this, { gravity: [0, 0] });
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user