Skip to content
Browse files

edge detection for bottom container is working

  • Loading branch information...
1 parent 9f76fb9 commit 80468ca8771d3e2af6e9de3857fe3f007128f3c2 @marksoper committed May 8, 2012
Showing with 34 additions and 17 deletions.
  1. +31 −16 js/sand.js
  2. +3 −1 templates/singleGrain.html
View
47 js/sand.js
@@ -11,24 +11,36 @@ var Sand = function(options) {
stopped: []
};
this.idSequence = 0;
- this.bounds = this.bounds || {
+ if (this.canvas) {
+ this.setBounds();
+ this.setEdges();
+ }
+};
+
+Sand.prototype.setBounds = function(canvas) {
+ canvas = canvas || this.canvas;
+ this.bounds = {
top: 0,
- right: this.canvas.width,
- bottom: this.canvas.height,
+ right: canvas.width,
+ bottom: canvas.height,
left: 0
};
- this.edges = this.edges || {
+};
+
+Sand.prototype.setEdges = function(canvas) {
+ canvas = canvas || this.canvas;
+ this.edges = {
top: {
type: "bound",
value: 0
},
right: {
type: "bound",
- value: this.canvas.width
+ value: canvas.width
},
bottom: {
type: "container",
- value: [[canvas.width, this.canvas.height]]
+ value: [[canvas.width, canvas.height]]
},
left: {
type: "bound",
@@ -42,6 +54,7 @@ Sand.prototype.addGrain = function(options) {
options.id = this.idSequence;
this.idSequence+=1;
var grain = new this.Grain(options);
+ grain.draw();
if (grain.moving) {
this.grains.moving.push(grain);
} else {
@@ -68,7 +81,7 @@ Sand.prototype.detectEdges = function(grain) {
left: null
};
if (this.edges.top.type === "bound") { // top container not supported
- if ( (grain.location[1] - grain.height) < this.edges.top.value ) {
+ if ( grain.location[1] < this.edges.top.value ) {
hits.top = {
type: this.edges.top.type,
value: grain.location[0]
@@ -86,11 +99,14 @@ Sand.prototype.detectEdges = function(grain) {
}
}
if (this.edges.bottom.type === "container") { // only container supported for bottom
- hits.bottom = {
- type: this.edges.bottom.type,
- value: this.detectContainer(grain, this.edges.bottom.value)
- };
- return hits;
+ // TODO: Need separate function to detect more complex container edge
+ if ( (grain.location[1] + grain.height) >= this.edges.bottom.value[0][1] ) {
+ hits.bottom = {
+ type: this.edges.bottom.type,
+ value: this.detectContainer(grain, this.edges.bottom.value)
+ };
+ return hits;
+ }
}
if (this.edges.left.type === "bound") { // left container not supported
if ( (grain.location[0] - grain.width) < this.edges.left.value ) {
@@ -109,19 +125,18 @@ Sand.prototype.advance = function() {
if (this.status === "started") {
this.grains.moving.forEach(function(grain) {
grain.advance();
- console.log("grain advance at location: " + grain.location[0] + " , " + grain.location[1]);
});
this.grains.moving.forEach(function(grain) {
var hits = self.detectEdges(grain);
for (var e in hits) {
if (hits[e]) {
if (hits[e].type === "bound") {
grain.visible = false;
- delete this.grains.moving(this.grains.moving.indexOf(grain));
+ self.grains.moving.splice(self.grains.moving.indexOf(grain),1);
} else if (hits[e].type === "container") {
grain.moving = false;
- this.grains.stopped.push(grain);
- delete this.grains.moving(this.grains.moving.indexOf(grain));
+ self.grains.stopped.push(grain);
+ self.grains.moving.splice(self.grains.moving.indexOf(grain),1);
}
}
}
View
4 templates/singleGrain.html
@@ -58,9 +58,11 @@
borderStyle: "rgba(25,25,25,1.0)",
height: GROUND_HEIGHT
});
+ sand.setBounds();
+ sand.setEdges();
sand.edges.bottom = {
type: "container",
- value: [[canvas.width, this.canvas.height - GROUND_HEIGHT]];
+ value: [[canvas.width, canvas.height - GROUND_HEIGHT]]
};
var addGrainInterval = setInterval(addGrain, 1000.0 / ADD_GRAIN_RATE);

0 comments on commit 80468ca

Please sign in to comment.
Something went wrong with that request. Please try again.