Wisdom8888 / beginning-android-games

Automatically exported from code.google.com/p/beginning-android-games
0 stars 0 forks source link

Chapter 8 Previously Posted Fix for SpatialHashGrid contained errors - Fixed again #36

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. After using the code from 'FixedSpatialHashGrid.java' that was posted by 
Rieder.A...@gmail.com, there were still errors. The problem was that the 
following methods were trying to access ArrayList items indexed as -1 (cellId 
's marked as not containing the obj GameObject):

insertStaticObject, insertDynamicObject, removeObject, and getPotentialColliders

Example:

Original:

    public void insertDynamicObject(GameObject obj) {
        int[] cellIds = getCellIds(obj);
        int i = 0;
        int cellId = -1;
        for(i = 0; i < cellIds.length; ++i) {
            cellId = cellIds[i];
            dynamicCells[cellId].add(obj);
        }
    }

Fixed:
public void insertDynamicObject(GameObject obj){
        int[] cellIds = getCellIds(obj);
        int cellId = -1;
        for (int i = 0; i < cellIds.length; ++i){
            cellId = cellIds[i];

            if (cellId == -1)
                continue;

            dynamicCells[cellId].add(obj);
        }
    }

Attached working version of the complete class

Thank you for writing such a great book!

Original issue reported on code.google.com by imse...@gmail.com on 12 Oct 2011 at 6:48

Attachments: