| 1133 | } |
| 1134 | |
| 1135 | bool SetGridShapeHull(HCollisionObject2D collision_object, uint32_t shape_index, uint32_t row, uint32_t column, uint32_t hull, HullFlags flags) |
| 1136 | { |
| 1137 | Body* body = (Body*) collision_object; |
| 1138 | GridShapeData* shape_data = GetGridShapeData(body, shape_index); |
| 1139 | |
| 1140 | if (shape_data == 0) |
| 1141 | { |
| 1142 | return false; |
| 1143 | } |
| 1144 | |
| 1145 | uint32_t index = row * shape_data->m_ColumnCount + column; |
| 1146 | assert(index < shape_data->m_RowCount * shape_data->m_ColumnCount); |
| 1147 | |
| 1148 | GridShapeData::Cell* cell = &shape_data->m_Cells[index]; |
| 1149 | bool hull_changed = cell->m_Index != hull; |
| 1150 | cell->m_Index = hull; |
| 1151 | |
| 1152 | // We need to recreate polygon if the flags have changed |
| 1153 | HullFlags* cell_flags = &shape_data->m_CellFlags[index]; |
| 1154 | bool cell_flags_changed = memcmp(&flags, cell_flags, sizeof(HullFlags)) != 0; |
| 1155 | shape_data->m_CellFlags[index] = flags; |
| 1156 | |
| 1157 | uint64_t opaque_id = ToOpaqueHandle(shape_data->m_CellPolygonShapes[index]); |
| 1158 | |
| 1159 | // treat cells with an empty hull as an empty cell |
| 1160 | if (hull != B2GRIDSHAPE_EMPTY_CELL) |
| 1161 | { |
| 1162 | HullSet::Hull& h = shape_data->m_HullSet->m_Hulls[hull]; |
| 1163 | if (h.m_Count == 0) |
| 1164 | { |
| 1165 | cell->m_Index = B2GRIDSHAPE_EMPTY_CELL; |
| 1166 | } |
| 1167 | else if (opaque_id == 0) |
| 1168 | { |
| 1169 | CreateGridCellShape(collision_object, shape_index, index); |
| 1170 | } |
| 1171 | else if (cell_flags_changed || hull_changed) |
| 1172 | { |
| 1173 | b2DestroyShape(shape_data->m_CellPolygonShapes[index], false); |
| 1174 | memset(&shape_data->m_CellPolygonShapes[index], 0, sizeof(shape_data->m_CellPolygonShapes[index])); |
| 1175 | CreateGridCellShape(collision_object, shape_index, index); |
| 1176 | } |
| 1177 | } |
| 1178 | else |
| 1179 | { |
| 1180 | // Clear the polygon |
| 1181 | if (opaque_id != 0) |
| 1182 | { |
| 1183 | b2DestroyShape(shape_data->m_CellPolygonShapes[index], false); |
| 1184 | memset(&shape_data->m_CellPolygonShapes[index], 0, sizeof(shape_data->m_CellPolygonShapes[index])); |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | return true; |
| 1189 | } |
| 1190 | |
| 1191 | bool SetGridShapeEnable(HCollisionObject2D collision_object, uint32_t shape_index, uint32_t enable) |
| 1192 | { |