Skip to content

Commit

Permalink
Massively improve collision detection by colliding the shuttlecock wi…
Browse files Browse the repository at this point in the history
…th a polygon containing all space the battledore has travelled through during the previous frame.
  • Loading branch information
djcsdy committed Feb 12, 2012
1 parent e0dcb71 commit def40d0
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/net/noiseinstitute/battledore_and_shuttlecock/Battledore.as
Expand Up @@ -7,16 +7,23 @@ package net.noiseinstitute.battledore_and_shuttlecock {

public class Battledore extends Entity {
public var offsetX:Number = 0;

public var prevX:Number = 0;
public var prevY:Number = 0;

public var battledoreWidth:Number = 8;
public var battledoreHeight:Number = 64;

public var velocity:Point = new Point;

private var battledoreMask:BattledoreMask;

public function Battledore() {
var image:Image = Image.createRect(8, 64);
var image:Image = Image.createRect(battledoreWidth, battledoreHeight);
image.centerOrigin();
graphic = image;

setHitbox(image.width, image.height, image.originX, image.originY);
mask = battledoreMask = new BattledoreMask(this);
}

override public function update():void {
Expand All @@ -29,6 +36,8 @@ package net.noiseinstitute.battledore_and_shuttlecock {
velocity.x = x - prevX;
velocity.y = y - prevY;

battledoreMask.updateMask();

super.update();
}
}
Expand Down
@@ -0,0 +1,92 @@
package net.noiseinstitute.battledore_and_shuttlecock {
import flash.display.Graphics;
import flash.display.Sprite;

import net.flashpunk.FP;
import net.flashpunk.Mask;

public class BattledoreMask extends Mask {
private var battledore:Battledore;

private var collisionContainer:Sprite = new Sprite();
private var mask:Sprite = new Sprite();
private var hitObject:Sprite = new Sprite();

public function BattledoreMask(battledore:Battledore) {
this.battledore = battledore;

collisionContainer.addChild(mask);
collisionContainer.addChild(hitObject);

_check[Mask] = collideMask;
}

public function updateMask():void {
update();

var halfWidth:Number = battledore.battledoreWidth * 0.5;
var halfHeight:Number = battledore.battledoreHeight * 0.5;

var graphics:Graphics = mask.graphics;
graphics.clear();
graphics.lineStyle();

graphics.beginFill(0xffffff, 1);
graphics.moveTo(battledore.prevX - halfWidth, battledore.prevY - halfHeight);
graphics.lineTo(battledore.x - halfWidth, battledore.y - halfHeight);
graphics.lineTo(battledore.x + halfWidth, battledore.y - halfHeight);
graphics.lineTo(battledore.prevX + halfWidth, battledore.prevY - halfHeight);
graphics.lineTo(battledore.prevX - halfWidth, battledore.prevY - halfHeight);
graphics.endFill();

graphics.beginFill(0xff0000, 1);
graphics.moveTo(battledore.prevX + halfWidth, battledore.prevY - halfHeight);
graphics.lineTo(battledore.x + halfWidth, battledore.y - halfHeight);
graphics.lineTo(battledore.x + halfWidth, battledore.y + halfHeight);
graphics.lineTo(battledore.prevX + halfWidth, battledore.prevY + halfHeight);
graphics.lineTo(battledore.prevX + halfWidth, battledore.prevY - halfHeight);
graphics.endFill();

graphics.beginFill(0x00ff00, 1);
graphics.moveTo(battledore.prevX + halfWidth, battledore.prevY + halfHeight);
graphics.lineTo(battledore.x + halfWidth, battledore.y + halfHeight);
graphics.lineTo(battledore.x - halfWidth, battledore.y + halfHeight);
graphics.lineTo(battledore.prevX - halfWidth, battledore.prevY + halfHeight);
graphics.lineTo(battledore.prevX + halfWidth, battledore.prevY + halfHeight);
graphics.endFill();

graphics.beginFill(0x0000ff, 1);
graphics.moveTo(battledore.prevX - halfWidth, battledore.prevY + halfHeight);
graphics.lineTo(battledore.x - halfWidth, battledore.y + halfHeight);
graphics.lineTo(battledore.x - halfWidth, battledore.y - halfHeight);
graphics.lineTo(battledore.prevX - halfWidth, battledore.prevY - halfHeight);
graphics.lineTo(battledore.prevX - halfWidth, battledore.prevY + halfHeight);
graphics.endFill();
}

override protected function update():void {
parent.originX = parent.x;
parent.originY = parent.y;
parent.width = Main.WIDTH;
parent.height = Main.HEIGHT;
}

private function collideMask(other:Mask):Boolean {
var graphics:Graphics = hitObject.graphics;
graphics.clear();
graphics.beginFill(0xffff00, 1);
graphics.drawRect(other.parent.x - other.parent.originX,
other.parent.y - other.parent.originY,
other.parent.width,
other.parent.height);
graphics.endFill();

return mask.hitTestObject(hitObject);
}

override public function renderDebug(g:Graphics):void {
FP.engine.alpha = 0.5;
FP.stage.addChild(collisionContainer);
}
}
}
Expand Up @@ -92,15 +92,15 @@ package net.noiseinstitute.battledore_and_shuttlecock {
updateScore(score+1);

if (shuttlecock.x > battledore.prevX) {
if (shuttlecock.x < battledore.x + (shuttlecock.width + battledore.width) * 0.5 + 1) {
shuttlecock.x = battledore.x + (shuttlecock.width + battledore.width) * 0.5 + 1;
if (shuttlecock.x < battledore.x + (shuttlecock.width + battledore.battledoreWidth) * 0.5 + 1) {
shuttlecock.x = battledore.x + (shuttlecock.width + battledore.battledoreWidth) * 0.5 + 1;
}
if (shuttlecock.velocity.x < 0) {
shuttlecock.velocity.x = -shuttlecock.velocity.x;
}
} else {
if (shuttlecock.x > battledore.x - (shuttlecock.width + battledore.width) * 0.5 - 1) {
shuttlecock.x = battledore.x - (shuttlecock.width + battledore.width) * 0.5 -1;
if (shuttlecock.x > battledore.x - (shuttlecock.width + battledore.battledoreWidth) * 0.5 - 1) {
shuttlecock.x = battledore.x - (shuttlecock.width + battledore.battledoreWidth) * 0.5 -1;
}
if (shuttlecock.velocity.x > 0) {
shuttlecock.velocity.x = -shuttlecock.velocity.x;
Expand Down

0 comments on commit def40d0

Please sign in to comment.