Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

2d grid collision check

View collision_check.as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.utils.getTimer;
[SWF (backgroundColor = 0xffffff, width = 1090, height = 800)]
public class BallCollisionGrid extends Sprite {
private var _grids:Array;
private var _balls:Array;
private var numBalls:Number = 200;
private static const RADIUS:Number = 20;
private static const GRID_SIZE:Number = 40;
public function BallCollisionGrid():void {
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
createBalls();
drawGrids();
//Grid Check Collision
var startTime:int = getTimer();
var elapsedTime:int;
for (var i:int = 0; i< 10; i++) {
createGrids();
assignBallsToGrid();
checkBallsCollision();
}
elapsedTime = getTimer() - startTime;
trace ("Time elapsed:" + elapsedTime);
// Basic Check Collision
startTime = getTimer();
for (var j:int = 0; j< 10; j++) {
basicCheck();
}
elapsedTime = getTimer() - startTime;
trace ("Time elapsed:" + elapsedTime);
}
private function createBalls():void {
_balls = new Array();
for (var i:int = 0; i < numBalls; i++) {
var ball:Ball = new Ball(RADIUS);
ball.x = Math.random() * stage.stageWidth;
ball.y = Math.random() * stage.stageHeight;
ball.vx = Math.random() * 4 - 2;
ball.vy = Math.random() * 4 - 2;
addChild(ball);
_balls.push(ball);
}
}
private function createGrids():void {
_grids = new Array();
for (var i:int = 0; i< stage.stageWidth / GRID_SIZE; i++) {
_grids[i] = new Array();
for (var j:int = 0; j< stage.stageHeight / GRID_SIZE; j++) {
_grids[i][j] = new Array();
}
}
}
private function drawGrids():void {
//graphics.clear();
graphics.lineStyle(0, 0.5);
for (var i:int = 0; i<= stage.stageWidth; i+=GRID_SIZE) {
graphics.moveTo(i, 0);
graphics.lineTo(i, stage.stageHeight);
}
for (i = 0; i<= stage.stageHeight; i+= GRID_SIZE) {
graphics.moveTo(0, i);
graphics.lineTo(stage.stageWidth, i);
}
}
private function assignBallsToGrid():void {
for (var i:int = 0; i< numBalls; i++) {
var ball:Ball = Ball(_balls[i]);
var xpos:int = Math.floor(ball.x / GRID_SIZE);
var ypos:int = Math.floor(ball.y / GRID_SIZE);
_grids[xpos][ypos].push(ball);
}
}
private function checkBallsCollision():void {
for (var i:int = 0; i< _grids.length; i++) {
for (var j:int = 0; j< _grids[i].length; j++) {
checkOneCell(i, j);
checkTwoCell(i, j, i+1, j);
checkTwoCell(i, j, i, j+1);
checkTwoCell(i, j, i-1, j);
checkTwoCell(i, j, i+1, j+1);
}
}
}
private function checkOneCell(x1:Number, y1:Number):void {
var _cell:Array = _grids[x1][y1] as Array;
for (var i:int = 0; i< _cell.length-1; i++) {
var ballA:Ball = _cell[i] as Ball;
for (var j:int = i+1; j< _cell.length; j++) {
var ballB:Ball = _cell[j] as Ball;
checkCollision(ballA, ballB);
}
}
}
private function checkTwoCell(x1:Number, y1:Number, x2:Number, y2:Number):void {
if (x2 < 0) { return }
if (x2 >= _grids.length) { return }
if (y2 >= _grids[x2].length) { return }
var _cell0:Array = _grids[x1][y1] as Array;
var _cell1:Array = _grids[x2][y2] as Array;
for (var i:int = 0; i< _cell0.length; i++) {
var ballA:Ball = _cell0[i] as Ball;
for (var j:int = 0; j< _cell1.length; j++) {
var ballB:Ball = _cell1[j] as Ball;
checkCollision(ballA, ballB);
}
}
}
private function checkCollision(ballA:Ball, ballB:Ball):void {
var dx:Number = ballB.x - ballA.x;
var dy:Number = ballB.y - ballA.y;
var dist:Number = Math.sqrt(dx*dx + dy*dy);
if (dist < ballB.radius + ballA.radius) {
ballA.color = 0xff0000;
ballB.color = 0xff0000;
}
}
private function basicCheck():void {
for (var i:int = 0; i< numBalls-1; i++) {
var ballA:Ball = _balls[i] as Ball;
for (var j:int = i+1; j< numBalls; j++) {
var ballB:Ball = _balls[j] as Ball;
checkCollision(ballA, ballB);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.