Skip to content

Commit

Permalink
Introduced the notion of shapes
Browse files Browse the repository at this point in the history
Unusued for now !
  • Loading branch information
obiot committed Sep 2, 2013
1 parent 7576eea commit 7e4c1b5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
27 changes: 23 additions & 4 deletions src/entity/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,25 @@

/**
* Entity collision Box<br>
* (reference to me.ObjectEntity.shapes[0].getBounds)
* @public
* @deprecated
* @type me.Rect
* @name collisionBox
* @memberOf me.ObjectEntity
*/
collisionBox : null,

/**
* Entity collision shapes<br>
* (RFU - Reserved for Future Usage)
* @public
* @type Object[]
* @name shapes
* @memberOf me.ObjectEntity
*/
shapes : null,

/**
* The entity renderable object (if defined)
* @public
Expand Down Expand Up @@ -535,10 +547,7 @@

// ref to the collision map
this.collisionMap = me.game.collisionMap;

// create a a default collision rectangle
this.collisionBox = new me.Rect(this.pos, this.width, this.height);


/**
* Define if an entity can go through breakable tiles<br>
* default value : false<br>
Expand All @@ -557,6 +566,16 @@
* @memberOf me.ObjectEntity
*/
this.onTileBreak = null;

// set the entity default collision shape
this.shapes = [];

// default for now is a rectangle
this.shapes[0] = new me.Rect(this.pos, this.width, this.height);

// set collisionBox
this.collisionBox = this.shapes[0].getBounds();

},

/**
Expand Down
7 changes: 5 additions & 2 deletions src/math/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,11 @@
* @return {me.Rect} new rectangle
*/
getBounds : function() {
// return this or a a cloned object ?
return this.clone();
// don't clone the pos vector, as the current implementation
// in master relies on the fact that the collisionBox pos vector
// is a reference to the entity pos vector
//return this.clone();
return new me.Rect(this.pos, this.width, this.height);
},

/**
Expand Down

3 comments on commit 7e4c1b5

@obiot
Copy link
Member Author

@obiot obiot commented on 7e4c1b5 Sep 2, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parasyte is this correct ? ( i mean for the shapes[] property)
please do note the comment i added in the me.Rect.getBounds() function, as I believe this won't be the case anymore after ticket #103 ?

with this I can modify the whole object creation, add proper parsing of other Tiled shapes in master, and add corresponding shape type :)

@parasyte
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think you have it right. The current code for #103 also expects the collision shape position to be updated with the "body" (eg. EntityObject). I think what we should do instead is use the shape position as an offset from the body, and the shapes will have a reference to the body as well, for the final position calculations during collision detection. I believe this is what Chipmunk does...

As an example, a circle shape with pos = {x:0, y:0} and radius = 10 would create a 20x20 circle with the body's position at its center. And with pos = {x:10,y:10} the body's position would be at the circle's upper-left corner. Etc.

@obiot
Copy link
Member Author

@obiot obiot commented on 7e4c1b5 Sep 3, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parasyte yes, offset pos sounds like right. This is actually what i tried to achieve with the colPos vector, in an more or less optimized way, but with a code difficult to understand at the end ! :)

I guess shapes still need a bit more love though, like adding add/remove function, or emptying the array on destroy.

Please sign in to comment.