Added in Circle to the Ninja physics system.

This commit is contained in:
photonstorm
2014-03-06 07:18:59 +00:00
parent 3e93f24583
commit e97a207816
4 changed files with 2657 additions and 5 deletions
+117
View File
@@ -0,0 +1,117 @@
// var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.spritesheet('ninja-tiles', 'assets/physics/ninja-tiles.png', 128, 128, 34);
game.load.image('a', 'assets/sprites/firstaid.png');
game.load.image('ball', 'assets/sprites/shinyball.png');
}
var sprite1;
var cursors;
var tile1;
var tile2;
var t;
var running = false;
function create() {
game.stage.smoothed = true;
// Activate the Ninja physics system
game.physics.startSystem(Phaser.Physics.NINJA);
// game.physics.ninja.gravity = 0.1;
sprite1 = game.add.sprite(102, 200, 'ball');
// Enable the physics body for the Ninja physics system
// By default it will create an AABB body for the sprite
game.physics.ninja.enableCircle(sprite1, sprite1.width / 2);
// But you can change it to either a Tile or a Circle
tile1 = game.add.sprite(100, 500, 'ninja-tiles', 14);
tile1.width = 100;
tile1.height = 100;
game.physics.ninja.enableTile(tile1, 14);
cursors = game.input.keyboard.createCursorKeys();
}
function collisionHandler() {
game.stage.backgroundColor = 0xff0000;
}
function update() {
game.physics.ninja.collide(sprite1, tile1, collisionHandler, null, this);
// tile1.body.moveRight(1);
/*
if (cursors.up.isDown && !running)
{
running = true;
t = Date.now();
}
sprite1.body.setZeroVelocity();
if (running)
{
sprite1.body.moveRight(100);
if (sprite1.body.x >= 200)
{
var ms = Date.now() - t;
console.log('100px in ', ms);
running = false;
sprite1.body.setZeroVelocity();
}
}
*/
// sprite1.body.setZeroVelocity();
if (cursors.left.isDown)
{
sprite1.body.moveLeft(20);
}
else if (cursors.right.isDown)
{
sprite1.body.moveRight(20);
}
if (cursors.up.isDown)
{
sprite1.body.moveUp(20);
}
else if (cursors.down.isDown)
{
sprite1.body.moveUp(20);
}
}
function render() {
game.debug.text(sprite1.body.shape.velocity.x, 32, 32);
game.debug.text(sprite1.body.shape.velocity.y, 32, 64);
game.debug.text(game.math.radToDeg(sprite1.body.angle), 32, 96);
// tile1.render(game.context, 'ninja-tiles');
// tile2.render(game.context, 'ninja-tiles');
// game.debug.geom(sprite1.body, 'rgba(0,255,0,0.4)', true, 1);
// game.debug.geom(tile1, 'rgba(0,255,0,0.4)', true, 1);
// game.debug.geom(tile1, 'rgba(0,255,0,0.4)', true, 1);
}
+4 -2
View File
@@ -15,11 +15,13 @@
* @param {Phaser.Sprite} sprite - The Sprite object this physics body belongs to.
* @param {number} [type=1] - The type of Ninja shape to create. 1 = AABB, 2 = Circle or 3 = Tile.
* @param {number} [id=1] - If this body is using a Tile shape, you can set the Tile id here, i.e. Phaser.Physics.Ninja.Tile.SLOPE_45DEGpn, Phaser.Physics.Ninja.Tile.CONVEXpp, etc.
* @param {number} [radius=16] - If this body is using a Circle shape this controls the radius.
*/
Phaser.Physics.Ninja.Body = function (system, sprite, type, id) {
Phaser.Physics.Ninja.Body = function (system, sprite, type, id, radius) {
if (typeof type === 'undefined') { type = 1; }
if (typeof id === 'undefined') { id = 1; }
if (typeof radius === 'undefined') { radius = 16; }
/**
* @property {Phaser.Sprite} sprite - Reference to the parent Sprite.
@@ -68,7 +70,7 @@ Phaser.Physics.Ninja.Body = function (system, sprite, type, id) {
}
else if (type === 2)
{
this.circle = new Phaser.Physics.Ninja.Circle(system, sprite.x, sprite.y, sprite.width, sprite.height);
this.circle = new Phaser.Physics.Ninja.Circle(system, sprite.x, sprite.y, radius);
this.shape = this.circle;
}
else if (type === 3)
File diff suppressed because it is too large Load Diff
+10 -2
View File
@@ -124,7 +124,7 @@ Phaser.Physics.Ninja.prototype = {
{
if (object[i].body === null)
{
object[i].body = new Phaser.Physics.Ninja.Body(this, object[i], type, id);
object[i].body = new Phaser.Physics.Ninja.Body(this, object[i], type, id, radius);
object[i].anchor.set(0.5);
}
}
@@ -442,7 +442,15 @@ Phaser.Physics.Ninja.prototype = {
return body2.aabb.collideAABBVsTile(body1.tile);
}
// circles next
if (body1.circle && body2.tile)
{
return body1.circle.collideCircleVsTile(body2.tile);
}
if (body1.tile && body2.circle)
{
return body2.circle.collideCircleVsTile(body1.tile);
}
}