Haha… yeah Bre! \o/
Listen… this thing should only partially work, but my test is flawed somewhere and I modified it so many times that can’t find where it’s going bad (if you have a testbed I’d like to play with it). Could you try to use it with your setup and see how it behaves?
You should replace your moveBy()
calls with moveByBresenham()
ones and lower the values of precision (< 10 probably). Also, if it doesn’t work, not even with precision
at 1, try increasing your gravity value. As you can see it calls moveBy()
, so moveCollideX/Y()
should also be called in turn.
/**
* Moves the Entity by amount specified, using the Bresenham's line algorithm and retaining integer values for its x and y.
* @param x Horizontal offset.
* @param y Vertical offset.
* @param solidType An optional collision type (or array of types) to stop flush against upon collision.
* @param precision Distance between consecutive tests. Higher values are faster but increase the chance of missing collisions.
*/
public function moveByBresenham(x:Number, y:Number, solidType:Object = null, precision:int = 1):void
{
_moveX += x;
_moveY += y;
x = Math.round(_moveX);
y = Math.round(_moveY);
_moveX -= x;
_moveY -= y;
if (solidType) {
var fromX:int = Math.round(this.x);
var fromY:int = Math.round(this.y);
var toX:int = fromX + x;
var toY:int = fromY + y;
var steep:Boolean = Math.abs(toY - fromY) > Math.abs(toX - fromX);
if (steep) { // swap x <-> y
var tmp:int;
tmp = fromX;
fromX = fromY;
fromY = tmp;
tmp = toX;
toX = toY;
toY = tmp;
}
var deltaX:int = Math.abs(toX - fromX);
var deltaY:int = Math.abs(toY - fromY);
var error:int = deltaX / 2;
var count:int = -1;
var xStep:int = fromX < toX ? 1 : -1;
var yStep:int = fromY < toY ? 1 : -1;
while (fromX != toX) {
if (count == precision || count < 0) {
if (steep) {
moveBy(fromY - this.x, fromX - this.y, solidType);
} else {
moveBy(fromX - this.x, fromY - this.y, solidType);
}
count = 0;
}
error -= deltaY;
if (error < 0) {
fromY += yStep;
error += deltaX;
}
fromX += xStep;
count++;
}
// last point
if (steep) {
moveBy(fromY - this.x, fromX - this.y, solidType);
} else {
moveBy(fromX - this.x, fromY - this.y, solidType);
}
} else { // no solidType specified -> just move it
this.x += x;
this.y += y;
}
}