| 381 | static const float SCALE_EPSILON = 0.000001f; |
| 382 | |
| 383 | static void UpdateScale(HWorld2D world, Body* body, bool force_update) |
| 384 | { |
| 385 | dmTransform::Transform world_transform; |
| 386 | (*world->m_GetWorldTransformCallback)(b2Body_GetUserData(body->m_BodyId), world_transform); |
| 387 | |
| 388 | float object_scale = GetUniformScale2D(world_transform); |
| 389 | bool allow_sleep = true; |
| 390 | |
| 391 | for (int i = 0; i < body->m_ShapeCount; ++i) |
| 392 | { |
| 393 | ShapeData* shape_data = body->m_Shapes[i]; |
| 394 | b2ShapeId shape_id = shape_data->m_ShapeId; |
| 395 | |
| 396 | if (fabsf(shape_data->m_LastScale - object_scale) <= SCALE_EPSILON && !force_update) |
| 397 | { |
| 398 | continue; |
| 399 | } |
| 400 | |
| 401 | shape_data->m_LastScale = object_scale; |
| 402 | allow_sleep = false; |
| 403 | |
| 404 | if (shape_data->m_Type == SHAPE_TYPE_CIRCLE) |
| 405 | { |
| 406 | CircleShapeData* circle_shape_data = (CircleShapeData*) shape_data; |
| 407 | circle_shape_data->m_Circle = b2Shape_GetCircle(shape_id); |
| 408 | |
| 409 | // creation scale for circles, is the initial radius |
| 410 | circle_shape_data->m_Circle.radius = shape_data->m_CreationScale * object_scale; |
| 411 | circle_shape_data->m_Circle.center.x = shape_data->m_CreationPosition.x * object_scale; |
| 412 | circle_shape_data->m_Circle.center.y = shape_data->m_CreationPosition.y * object_scale; |
| 413 | b2Shape_SetCircle(shape_id, &circle_shape_data->m_Circle); |
| 414 | } |
| 415 | else if (shape_data->m_Type == SHAPE_TYPE_POLYGON) |
| 416 | { |
| 417 | PolygonShapeData* polygon_shape_data = (PolygonShapeData*) shape_data; |
| 418 | polygon_shape_data->m_Polygon = b2Shape_GetPolygon(shape_id); |
| 419 | |
| 420 | float s = object_scale / shape_data->m_CreationScale; |
| 421 | |
| 422 | for( int i = 0; i < polygon_shape_data->m_Polygon.count; ++i) |
| 423 | { |
| 424 | b2Vec2 p = polygon_shape_data->m_VerticesOriginal[i]; |
| 425 | polygon_shape_data->m_Polygon.vertices[i].x = p.x * s; |
| 426 | polygon_shape_data->m_Polygon.vertices[i].y = p.y * s; |
| 427 | } |
| 428 | polygon_shape_data->m_Polygon.centroid = polygon_shape_data->m_CentroidOriginal * s; |
| 429 | b2Shape_SetPolygon(shape_id, &polygon_shape_data->m_Polygon); |
| 430 | } |
| 431 | else if (shape_data->m_Type == SHAPE_TYPE_GRID) |
| 432 | { |
| 433 | GridShapeData* grid_shape = (GridShapeData*) shape_data; |
| 434 | |
| 435 | b2Vec2 vertices[B2_MAX_POLYGON_VERTICES]; |
| 436 | uint32_t cell_count = grid_shape->m_RowCount * grid_shape->m_ColumnCount; |
| 437 | for (uint32_t i = 0; i < cell_count; ++i) |
| 438 | { |
| 439 | if (grid_shape->m_Cells[i].m_Index == B2GRIDSHAPE_EMPTY_CELL) |
| 440 | continue; |
no test coverage detected