MCPcopy Create free account
hub / github.com/axmolengine/axmol / clipPoly

Method clipPoly

tests/cpp-tests/Source/PhysicsTest/PhysicsTest.cpp:1211–1251  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1209}
1210
1211void 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
1253void PhysicsDemoSlice::onTouchEnded(Touch* touch, Event* /*event*/)
1254{

Callers

nothing calls this directly

Calls 15

fabsFunction · 0.85
createFunction · 0.85
getPointsCountMethod · 0.80
local2WorldMethod · 0.80
getPointMethod · 0.80
addComponentMethod · 0.80
setTagMethod · 0.80
getBodyMethod · 0.45
dotMethod · 0.45
lerpMethod · 0.45
setPositionMethod · 0.45

Tested by

no test coverage detected