我正在尝试实现类似于Fruit Ninja的分割效果,但必须在用户滑动的位置剪切对象。我按照this教程进行了分组工作,但是我无法正确分割精灵。所以我想要的是身体有一个精灵,并且精灵以与身体相同的方式分裂,我正在使用PolygonSprite并且精灵分裂成正确的形状并与身体同时移动,但是有图像中的偏移量,似乎每次进行切割时,它都会切割原始纹理而不进行旋转。这是我得到的:
第一次剪切后(注意小三角形不是原始图像的右上角,并且图像未正确居中):
在我剪切小三角形之后(注意它是如何向后旋转并且没有切割实际图像):
这是我分割对象的方法:
private void createSlice(Array<Vector2> vertices, int numVertices, Body affectedBody) {
Vector2 center = findCentroid(vertices, vertices.size);
for (int i = 0; i < numVertices; i++) {
vertices.get(i).sub(center);
}
BodyDef sliceBody = new BodyDef();
sliceBody.position.set(center.x, center.y);
sliceBody.type = BodyDef.BodyType.DynamicBody;
PolygonShape slicePoly = new PolygonShape();
float[] verticesArray = new float[vertices.size * 2];
int j = 0;
for (int i = 0; i < verticesArray.length; i+=2) {
verticesArray[i] = vertices.get(j).x;
verticesArray[i+1] = vertices.get(j++).y;
}
slicePoly.set(verticesArray);
FixtureDef sliceFixture = new FixtureDef();
sliceFixture.shape = slicePoly;
sliceFixture.density = 1;
Body worldSlice = mWorld.createBody(sliceBody);
worldSlice.createFixture(sliceFixture);
for (int i = 0; i < numVertices; i++) {
vertices.get(i).add(center);
}
// create polygon sprite
float[] scaledVertices = new float[verticesArray.length];
for (int i = 0; i < verticesArray.length; i++) {
scaledVertices[i] = verticesArray[i] * Constants.BOX_TO_WORLD;
}
short[] triangles = mTriangulator.computeTriangles(scaledVertices).toArray();
TextureRegion bodyPolyTexReg = ((PolygonSprite) affectedBody.getUserData()).getRegion().getRegion();
PolygonRegion polyReg = new PolygonRegion(bodyPolyTexReg, scaledVertices, triangles);
PolygonSprite polySprite = new PolygonSprite(polyReg);
polySprite.setOrigin(0, 0);
// set the sprite as the body's user data
worldSlice.setUserData(polySprite);
}
我的猜测是我必须将顶点旋转到0度,进行切割,然后再旋转它们,但我不知道该怎么做。
谢谢!