Skip to content

Commit

Permalink
Fix collision detection
Browse files Browse the repository at this point in the history
  • Loading branch information
zerojuan committed Nov 4, 2012
1 parent e6f8ae7 commit 2900526
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 167 deletions.
68 changes: 41 additions & 27 deletions js/entities/hero.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ define('Hero',[
this.animation.gotoAndPlay('run');

this.graphics = new createjs.Container();
this.graphics.addChild(this.animation);//,debugBox);
this.graphics.addChild(this.animation,debugBox);

this.graphics.x = this.x;
this.graphics.y = this.y;
Expand All @@ -41,13 +41,14 @@ define('Hero',[

Hero.prototype = {
update : function(){
//console.log(this.onGround);
var dy = 0;
if(this.collision){
//dy = -this.collision.height;
if(this.collision){
if(this.animation.currentAnimation != 'run')
this.animation.gotoAndPlay('run');
this.onGround = true;
this.collision = null;
}else{
//this.x += this.speed;
this.onGround = false;
}else{
this.velocity.y += .4;
}

Expand All @@ -64,44 +65,57 @@ define('Hero',[
},
jump : function(){
console.log('JUMP!!');
this.animation.gotoAndPlay('jump');
this.onGround = false;
this.velocity.y = -10;
},
collide : function(objB, data){
//console.log('DATA: ' + data.width + ', ' + data.height);
this.collision = data;


if(data.width < data.height){
this.separateX(objB);
this.separateX(objB, data);
}else{
this.separateY(objB);
this.separateY(objB, data);
this.collision = data;
}

},
separateX : function(objB){
var overlap = this.collision.width;
var objBX = objB.x;
separateX : function(objB, data){
var overlap = data.width;
var objBX = objB.getFuturePosition().x;
//get how much the overlap
var objADX = this.x - this.getFuturePosition().x;
var objBDX = objB.x - objB.getFuturePosition().x;
console.log('Change in X: ' + objADX + ' VS ' + objBDX);
//console.log('Change in X: ' + objADX + ' VS ' + objBDX);

if(!objB.movable){
//have the player absorb all the impact

if(objBX > this.x){
//this.collision.face = 'right';
this.x -= overlap;
this.velocity.x = objB.velocity.x;
}else{
//this.collision.face = 'left';
this.x += overlap;
}

}
if(objBX > this.x){
this.x -= overlap;
}else{
this.x += overlap;
}


},
separateY : function(objB){
var overlap = this.collision.height;
separateY : function(objB, data){
var overlap = data.height;
//get how much the overlap
if(overlap > 0){
this.y -= overlap;
//this.velocity.y = objB.velocity.y;
var objADX = this.y - this.getFuturePosition().y;
var objBDX = objB.y - objB.getFuturePosition().y;
//console.log('Change in X: ' + objADX + ' VS ' + objBDX);

if(Math.abs(overlap) > 1 ){
this.y = (objB.y + objB.boundingBox.y) - this.boundingBox.height - this.boundingBox.y;
this.velocity.y = 0;
//this.collision.face = 'bottom';
return true;
}else{
return false;
}
},
render : function(){
Expand Down
44 changes: 37 additions & 7 deletions js/entities/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ define('Platform', [
this.tilesheet = null;
this.speed = 0;
this.immovable = true;
this.velocity = {x : .5, y: 0};
this.velocity = {x : -.5, y: 0};

for(var prop in opts){
this[prop] = opts[prop];
Expand All @@ -36,8 +36,7 @@ define('Platform', [

for(var i=0; i < this.cols; i++){
for(var j=0; j < this.rows; j++){
if(j == 0){
console.log('I: '+i +' J: ' + j);
if(j == 0){
this.drawTile(tilemap, this.tilesheet, 1, i, j);
}else{
this.drawTile(tilemap, this.tilesheet, 0, i, j);
Expand All @@ -50,15 +49,15 @@ define('Platform', [
var debugBox = new createjs.Shape(boundingBoxGfx);

this.graphics = new createjs.Container();
this.graphics.addChild(tilemap);//, debugBox);
this.graphics.addChild(tilemap, debugBox);
this.graphics.x = this.x;
this.graphics.y = this.y;
}

Platform.prototype = {
update : function(){
this.velocity.x += this.acceleration;
this.x -= this.velocity.x;
this.velocity.x -= this.acceleration;
this.x += this.velocity.x;
if(this.x < this.outside){
this.isVisible = false;
}
Expand All @@ -80,7 +79,38 @@ define('Platform', [
}
},
reset : function(opts){
this.x = opts.x;
for(var prop in opts){
this[prop] = opts[prop];
}

this.width = this.cols * this.tileWidth;
this.height = this.rows * this.tileHeight;
this.outside = -this.width;

this.boundingBox = new createjs.Rectangle(0, 8, this.cols * this.tileWidth, this.rows * this.tileHeight);

var tilemap = new createjs.Container();

for(var i=0; i < this.cols; i++){
for(var j=0; j < this.rows; j++){
if(j == 0){
this.drawTile(tilemap, this.tilesheet, 1, i, j);
}else{
this.drawTile(tilemap, this.tilesheet, 0, i, j);
}
}
}

var boundingBoxGfx = new createjs.Graphics();
boundingBoxGfx.beginStroke('#00ff00').drawRect(this.boundingBox.x, this.boundingBox.y, this.boundingBox.width, this.boundingBox.height);
var debugBox = new createjs.Shape(boundingBoxGfx);


this.graphics.removeAllChildren();
this.graphics.addChild(tilemap, debugBox);
this.graphics.x = this.x;
this.graphics.y = this.y;

this.isVisible = true;
}
}
Expand Down
45 changes: 39 additions & 6 deletions js/entities/platformgenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ define("PlatformGenerator", [
this.platforms = [];
this.collidables = [];
this.x = 0;
this.y = 0;
this.y = 0;
this.acceleration = 0;

for(var prop in opts){
this[prop] = opts[prop];
}

//precreate the platforms to memory
var platform = new Platform({
var startPlatform = new Platform({
tilesheet : this.bitmap,
rows : 60,
cols : 20,
y : 300,
acceleration : this.acceleration
});
});

var platform2 = new Platform({
tilesheet : this.bitmap,
Expand All @@ -32,14 +32,38 @@ define("PlatformGenerator", [
y : 400,
x : 600,
acceleration : this.acceleration
});

var platform3 = new Platform({
tilesheet : this.bitmap,
rows : 60,
cols : 10,
y : 300,
x : 900,
acceleration : this.acceleration
});

var platform4 = new Platform({
tilesheet : this.bitmap,
rows : 60,
cols : 10,
y : 300,
x : 1200,
acceleration : this.acceleration
});

this.platforms.push(platform);
this.platforms.push(startPlatform);
this.platforms.push(platform2);
this.platforms.push(platform3);
this.platforms.push(platform4);
this.collidables = this.platforms;

this.graphics = new createjs.Container();
this.graphics.addChild(platform.graphics, platform2.graphics);
for(var i in this.platforms){
this.graphics.addChild(this.platforms[i].graphics);
}

this.lastPlatformIndex = this.platforms.length - 1;

this.graphics.x = this.x;
this.graphics.y = this.y;
Expand All @@ -50,7 +74,16 @@ define("PlatformGenerator", [
for(var i in this.platforms){
this.collidables[i].update();
if(!this.collidables[i].isVisible){
this.collidables[i].reset({x: 600})
//switch
var lastPlatform = this.collidables[this.lastPlatformIndex];
var lastX = lastPlatform.x + lastPlatform.width;

this.collidables[i].reset(
{
cols: Math.abs(Math.random() * 40 - 20),
y : lastPlatform.y + (Math.random() * 100 - 50),
x: lastX + Math.random() * 100 + 100});
this.lastPlatformIndex = i;
}
}
},
Expand Down
Loading

0 comments on commit 2900526

Please sign in to comment.