18 changes: 16 additions & 2 deletions engine/physics/src/box2d/Box2D/Collision/Shapes/b2GridShape.cpp
Expand Up @@ -149,11 +149,25 @@ uint32 b2GridShape::GetCellVertices(uint32 index, b2Vec2* vertices) const
float32 xScale = flags.m_FlipHorizontal ? -1.0f : 1.0f;
float32 yScale = flags.m_FlipVertical ? -1.0f : 1.0f;

float32 tmpX = 0.0;

for (uint32 i = 0; i < hull.m_Count; ++i)
{
vertices[i] = m_hullSet->m_vertices[hull.m_Index + i];
vertices[i].x *= xScale * m_cellWidth;
vertices[i].y *= yScale * m_cellHeight;
if (flags.m_Rotate90)
{
// Clockwise rotation (x, y) -> (y, -x)
// Also tile isn't necessary a square, that's why we should swap width and height
// We apply flip before rotation, which means scale is part of size and should be swapped as well
tmpX = vertices[i].x;
vertices[i].x = vertices[i].y * (yScale * m_cellWidth);
vertices[i].y = -1 * tmpX * (xScale * m_cellHeight);
}
else
{
vertices[i].x *= xScale * m_cellWidth;
vertices[i].y *= yScale * m_cellHeight;
}
vertices[i] += t;
}

Expand Down
8 changes: 5 additions & 3 deletions engine/physics/src/box2d/Box2D/Collision/Shapes/b2GridShape.h
Expand Up @@ -65,12 +65,14 @@ class b2GridShape : public b2Shape
CellFlags()
: m_FlipHorizontal(0)
, m_FlipVertical(0)
, m_Rotate90(0)
, m_Padding(0)
{}

uint16 m_FlipHorizontal : 1;
uint16 m_FlipVertical : 1;
uint16 m_Padding : 14;
uint8 m_FlipHorizontal : 1;
uint8 m_FlipVertical : 1;
uint8 m_Rotate90 : 1;
uint8 m_Padding : 5;
};

b2GridShape(const b2HullSet* hullSet,
Expand Down
8 changes: 5 additions & 3 deletions engine/physics/src/physics/physics.h
Expand Up @@ -99,13 +99,15 @@ namespace dmPhysics
HullFlags()
: m_FlipHorizontal(0)
, m_FlipVertical(0)
, m_Rotate90(0)
, m_Padding(0)
{
}

uint16_t m_FlipHorizontal : 1;
uint16_t m_FlipVertical : 1;
uint16_t m_Padding : 14;
uint8_t m_FlipHorizontal : 1;
uint8_t m_FlipVertical : 1;
uint8_t m_Rotate90 : 1;
uint8_t m_Padding : 5;
};

/**
Expand Down
1 change: 1 addition & 0 deletions engine/physics/src/physics/physics_2d.cpp
Expand Up @@ -669,6 +669,7 @@ namespace dmPhysics
b2GridShape::CellFlags f;
f.m_FlipHorizontal = flags.m_FlipHorizontal;
f.m_FlipVertical = flags.m_FlipVertical;
f.m_Rotate90 = flags.m_Rotate90;
grid_shape->SetCellHull(body, row, column, hull, f);
}

Expand Down