| 1209 | } |
| 1210 | |
| 1211 | void PhysicsDemoSlice::clipPoly(PhysicsShapePolygon* shape, Vec2 normal, float distance) |
| 1212 | { |
| 1213 | PhysicsBody* body = shape->getBody(); |
| 1214 | int count = shape->getPointsCount(); |
| 1215 | int pointsCount = 0; |
| 1216 | Vec2* points = new Vec2[count + 1]; |
| 1217 | |
| 1218 | for (int i = 0, j = count - 1; i < count; j = i, ++i) |
| 1219 | { |
| 1220 | Vec2 a = body->local2World(shape->getPoint(j)); |
| 1221 | float aDist = a.dot(normal) - distance; |
| 1222 | |
| 1223 | if (aDist < 0.0f) |
| 1224 | { |
| 1225 | points[pointsCount] = a; |
| 1226 | ++pointsCount; |
| 1227 | } |
| 1228 | |
| 1229 | Vec2 b = body->local2World(shape->getPoint(i)); |
| 1230 | float bDist = b.dot(normal) - distance; |
| 1231 | |
| 1232 | if (aDist * bDist < 0.0f) |
| 1233 | { |
| 1234 | float t = std::fabs(aDist) / (std::fabs(aDist) + std::fabs(bDist)); |
| 1235 | points[pointsCount] = a.lerp(b, t); |
| 1236 | ++pointsCount; |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | Vec2 center = PhysicsShape::getPolygonCenter(points, pointsCount); |
| 1241 | Node* node = Node::create(); |
| 1242 | PhysicsBody* polygon = PhysicsBody::createPolygon(points, pointsCount, PHYSICSBODY_MATERIAL_DEFAULT, -center); |
| 1243 | node->setPosition(center); |
| 1244 | node->addComponent(polygon); |
| 1245 | polygon->setVelocity(body->getVelocityAtWorldPoint(center)); |
| 1246 | polygon->setAngularVelocity(body->getAngularVelocity()); |
| 1247 | polygon->setTag(_sliceTag); |
| 1248 | addChild(node); |
| 1249 | |
| 1250 | delete[] points; |
| 1251 | } |
| 1252 | |
| 1253 | void PhysicsDemoSlice::onTouchEnded(Touch* touch, Event* /*event*/) |
| 1254 | { |
nothing calls this directly
no test coverage detected