lewisje / renderengine

Automatically exported from code.google.com/p/renderengine
MIT License
0 stars 0 forks source link

ColliderComponent.updateModel() doesn't add objects to a collision Node until they are moved #13

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
It turns out that the updateModel() function on ColliderComponent only adds 
objects to a node 
when they get moved from one grid node to another.  It goes like this:

     var obj = this.getHostObject();
     if (!obj.ModelData)
     {
        obj.ModelData = { lastNode: null };
     }

     if ( obj.ModelData.lastNode && 
obj.ModelData.lastNode.getRect().containsPoint(obj.getPosition()) )
     {
        // The object is within the same node
        return;
     }

     // Find the node that contains the object
     var aNode = this.getCollisionModel().findNodePoint(obj.getPosition());
     if (aNode != null)
     {
        if (obj.ModelData.lastNode && (obj.ModelData.lastNode.getIndex() != aNode.getIndex()))
        {
           obj.ModelData.lastNode.removeObject(obj);
           aNode.addObject(obj);
        }
        obj.ModelData.lastNode = aNode;
     }

1. updateModel() gets called on the current object's ColliderComponent.
2. If object has no lastNode, it gets set to null.  Then, in the second to last 
line of updateModel(), 
the object's lastNode gets set correctly.
3. If the object has been moved from one grid node to another, it will get 
added to the list of 
nodes.
4. Note well! If an object never gets moved from one node to another, it will 
never get added to 
its current node's list of objects.

In my game, I have bullets. They weren't getting told about some collisions 
because their Potential 
Collision Lists were missing the level furniture because the level furniture 
was not added to the 
object lists of any grid nodes.

Here is my rewrite of the updateModel() function that fixes the problem by 
adding an object to a 
node when the lastNode of the object first gets set.

     var obj = this.getHostObject();
                       var aNode = this.getCollisionModel().findNodePoint(obj.getPosition());

     if (!obj.ModelData)
     {
        obj.ModelData = { lastNode: null };
                                obj.ModelData.lastNode = aNode;
                                aNode.addObject(obj);
     }

     if ( obj.ModelData.lastNode && 
obj.ModelData.lastNode.getRect().containsPoint(obj.getPosition()) )
     {
        // The object is within the same node
        return;
     }

     // Find the node that contains the object
     if (aNode != null)
     {
        if (obj.ModelData.lastNode && (obj.ModelData.lastNode.getIndex() != aNode.getIndex()))
           obj.ModelData.lastNode.removeObject(obj);

        obj.ModelData.lastNode = aNode;
     }

Original issue reported on code.google.com by maryrose...@gmail.com on 31 May 2010 at 7:00

GoogleCodeExporter commented 8 years ago

Original comment by bfatt...@gmail.com on 10 Jun 2010 at 9:04

GoogleCodeExporter commented 8 years ago

Original comment by bfatt...@gmail.com on 10 Jun 2010 at 9:05

GoogleCodeExporter commented 8 years ago
Modified the updateModel() method of component.collider.js to set the node the 
object is in if the node has never been set (obj.ModelData.lastNode === null).

Original comment by bfatt...@gmail.com on 15 Jul 2010 at 3:31