MCPcopy Create free account
hub / github.com/defold/defold / GetGridCellVertices

Function GetGridCellVertices

engine/physics/src/physics/box2d/box2d_physics.cpp:1022–1094  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1020 }
1021
1022 static int GetGridCellVertices(GridShapeData* shape_data, int index, b2Vec2* vertices)
1023 {
1024 // Original code checked if the shape was enabled, but I'm not sure we can do that now.
1025 // The grid has only a single body that holds sub-shapes for all grid polygons,
1026 // so we would have to perhaps keep track of this in the grid shape data to avoid
1027 // going deeper into this function.
1028 //
1029 // if (!b2Body_IsEnabled(shape_data->m_CellBodyId))
1030 // {
1031 // return 0;
1032 // }
1033
1034 const GridShapeData::Cell& cell = shape_data->m_Cells[index];
1035 if (cell.m_Index == B2GRIDSHAPE_EMPTY_CELL)
1036 {
1037 return 0;
1038 }
1039
1040 const HullSet::Hull& hull = shape_data->m_HullSet->m_Hulls[cell.m_Index];
1041 assert(hull.m_Count <= B2_MAX_POLYGON_VERTICES);
1042
1043 int row = index / shape_data->m_ColumnCount;
1044 int col = index - (shape_data->m_ColumnCount * row);
1045
1046 float halfWidth = shape_data->m_CellWidth * shape_data->m_ColumnCount * 0.5f;
1047 float halfHeight = shape_data->m_CellHeight * shape_data->m_RowCount * 0.5f;
1048
1049 b2Vec2 t = {shape_data->m_CellWidth * col - halfWidth, shape_data->m_CellHeight * row - halfHeight};
1050 t.x += shape_data->m_CellWidth * 0.5f;
1051 t.y += shape_data->m_CellHeight * 0.5f;
1052 t += shape_data->m_Position;
1053
1054 const HullFlags& flags = shape_data->m_CellFlags[index];
1055 float xScale = flags.m_FlipHorizontal ? -1.0f : 1.0f;
1056 float yScale = flags.m_FlipVertical ? -1.0f : 1.0f;
1057 float tmpX = 0.0;
1058
1059 for (uint32_t i = 0; i < hull.m_Count; ++i)
1060 {
1061 vertices[i] = shape_data->m_HullSet->m_Vertices[hull.m_Index + i];
1062 if (flags.m_Rotate90)
1063 {
1064 // Clockwise rotation (x, y) -> (y, -x)
1065 // Also tile isn't necessary a square, that's why we should swap width and height
1066 // We apply flip before rotation, which means scale is part of size and should be swapped as well
1067 tmpX = vertices[i].x;
1068 vertices[i].x = vertices[i].y * (yScale * shape_data->m_CellWidth);
1069 vertices[i].y = -1 * tmpX * (xScale * shape_data->m_CellHeight);
1070 }
1071 else
1072 {
1073 vertices[i].x *= xScale * shape_data->m_CellWidth;
1074 vertices[i].y *= yScale * shape_data->m_CellHeight;
1075 }
1076 vertices[i] += t;
1077 }
1078
1079 // Reverse order when single flipped

Callers 2

UpdateScaleFunction · 0.85
CreateGridCellShapeFunction · 0.85

Calls 1

assertFunction · 0.50

Tested by

no test coverage detected