Line.coordinatesOnLine will return all coordinates on the line using Bresenhams line algorithm.

This commit is contained in:
photonstorm 2014-03-14 00:05:03 +00:00
parent ec0b22268d
commit a83a76bc5d
5 changed files with 97 additions and 28 deletions

View file

@ -151,6 +151,7 @@ New features:
* ScaleManager.fullScreenTarget allows you to change the DOM element that the fullscreen API is called on (feature request #526)
* Merged Georges p2 BodyDebug and reformatted for jshint pass. Looks awesome :)
* ArcadePhysics.Body has a new gravityScale property, which is a modifier multiplied against the world gravity value on a Body.
* Line.coordinatesOnLine will return all coordinates on the line using Bresenhams line algorithm.
Updates:

View file

@ -0,0 +1,20 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create });
function create() {
var line = new Phaser.Line(100, 50, 10, 300);
var coords = line.coordinatesOnLine();
var bmd = game.add.bitmapData(800, 600);
bmd.context.fillStyle = '#ffffff';
var bg = game.add.sprite(0, 0, bmd);
for (var i = 0; i < coords.length; i++)
{
bmd.context.fillRect(coords[i][0], coords[i][1], 1, 1);
}
}

View file

@ -30,13 +30,12 @@ function create() {
// Basically this sets EVERY SINGLE tile to fully collide on all faces
map.setCollisionByExclusion([7, 32, 35, 36, 47]);
layer.debug = true;
// layer.debug = true;
layer.resizeWorld();
cursors = game.input.keyboard.createCursorKeys();
/*
emitter = game.add.emitter(0, 0, 200);
emitter.makeParticles('chunk');
@ -44,17 +43,16 @@ function create() {
emitter.maxRotation = 0;
emitter.gravity = 150;
emitter.bounce.setTo(0.5, 0.5);
*/
sprite = game.add.sprite(200, 70, 'phaser');
sprite.anchor.setTo(0.5, 0.5);
sprite.anchor.set(0.5);
game.physics.enable(sprite);
// sprite.body.setSize(14, 14, 2, 0);
console.log(sprite.body);
// Because both our body and our tiles are so tiny,
// and the body is moving pretty fast, we need to add
// some tile padding to the body. WHat this does
sprite.body.tilePadding.set(64, 64);
game.camera.follow(sprite);
@ -62,9 +60,9 @@ function create() {
function particleBurst() {
// emitter.x = sprite.x;
// emitter.y = sprite.y;
// emitter.start(true, 2000, null, 1);
emitter.x = sprite.x;
emitter.y = sprite.y;
emitter.start(true, 2000, null, 1);
}
@ -78,38 +76,32 @@ function update() {
if (cursors.up.isDown)
{
sprite.body.velocity.y = -100;
// particleBurst();
sprite.body.velocity.y = -200;
particleBurst();
}
else if (cursors.down.isDown)
{
sprite.body.velocity.y = 100;
// particleBurst();
sprite.body.velocity.y = 200;
particleBurst();
}
if (cursors.left.isDown)
{
sprite.body.velocity.x = -100;
sprite.body.velocity.x = -200;
sprite.scale.x = -1;
// particleBurst();
particleBurst();
}
else if (cursors.right.isDown)
{
sprite.body.velocity.x = 100;
sprite.body.velocity.x = 200;
sprite.scale.x = 1;
// particleBurst();
particleBurst();
}
}
function render() {
// game.debug.text(game.physics.arcade._intersection.width, 32, 32);
// game.debug.text(game.physics.arcade._intersection.height, 32, 64);
game.debug.geom(sprite.body, 'rgba(0,255,0,0.4)', true, 1);
game.debug.text(sprite.body.overlapX, 32, 32);
game.debug.text(sprite.body.overlapY, 32, 64);
game.debug.body(sprite);
}

View file

@ -124,6 +124,54 @@ Phaser.Line.prototype = {
return (this.pointOnLine(x, y) && (x >= xMin && x <= xMax) && (y >= yMin && y <= yMax));
},
/**
* Using Bresenham's line algorithm this will return an array of all coordinates on this line.
* The start and end points are rounded before this runs as the algorithm works on integers.
*
* @method Phaser.Line#coordinatesOnLine
* @param {array} [results] - The array to store the results in. If not provided a new one will be generated.
* @return {array} An array of coordinates.
*/
coordinatesOnLine: function (results) {
if (typeof results === 'undefined') { results = []; }
var x1 = Math.round(this.start.x);
var y1 = Math.round(this.start.y);
var x2 = Math.round(this.end.x);
var y2 = Math.round(this.end.y);
var dx = Math.abs(x2 - x1);
var dy = Math.abs(y2 - y1);
var sx = (x1 < x2) ? 1 : -1;
var sy = (y1 < y2) ? 1 : -1;
var err = dx - dy;
results.push([x1, y1]);
while (!((x1 == x2) && (y1 == y2)))
{
var e2 = err << 1;
if (e2 > -dy)
{
err -= dy;
x1 += sx;
}
if (e2 < dx)
{
err += dx;
y1 += sy;
}
results.push([x1, y1]);
}
return results;
}
};

View file

@ -33,16 +33,24 @@ Phaser.Physics.Arcade.Body = function (sprite) {
/**
* @property {Phaser.Point} offset - The offset of the Physics Body from the Sprite x/y position.
*/
// this.offset = new Phaser.Point(-(sprite.anchor.x * sprite.width), -(sprite.anchor.y * sprite.height));
this.offset = new Phaser.Point();
/**
* @property {Phaser.Point} position - The position of the physics body.
* @readonly
*/
// this.position = new Phaser.Point(sprite.x + this.offset.x, sprite.y + this.offset.y);
this.position = new Phaser.Point(sprite.x, sprite.y);
if (sprite.anchor.x !== 0)
{
this.position.x -= (sprite.anchor.x * sprite.width);
}
if (sprite.anchor.y !== 0)
{
this.position.y -= (sprite.anchor.y * sprite.height);
}
/**
* @property {Phaser.Point} prev - The previous position of the physics body.
* @readonly