public MyLayer() { WYSize s = Director.getInstance().getWindowSize(); Chipmunk chipmunk = Chipmunk.make(); chipmunk.setDebugDraw(true); chipmunk.setPosition(s.width / 2, s.height / 2); addChild(chipmunk); mRandom = new Random(); Shape.resetShapeIdCounter(); mSpace = chipmunk.getSpace(); mSpace.setIterations(5); mSpace.setGravity(0, -100); mSpace.resizeActiveHash(40f, 999); mSpace.resizeStaticHash(30f, 2999); Body staticBody = Body.make(Float.MAX_VALUE, Float.MAX_VALUE); // Create vertexes for a pentagon shape. WYPoint[] verts = new WYPoint[5]; for (int i = 0; i < NUM_VERTS; i++) { float angle = -2 * (float) Math.PI * i / (NUM_VERTS); verts[i] = WYPoint.make(10 * (float) cos(angle), 10 * (float) sin(angle)); } // Vertexes for a triangle shape. WYPoint tris[] = {WYPoint.make(-15, -15), WYPoint.make(0, 10), WYPoint.make(15, -15)}; // Create the static triangles. for (int i = 0; i < 9; i++) { for (int j = 0; j < 6; j++) { float stagger = (j % 2) * 40; WYPoint offset = WYPoint.make(i * 80 - s.width / 2 + stagger, j * 70 - s.width / 2); Poly shape = Poly.make(staticBody, tris, offset); shape.setRestitution(1f); shape.setFriction(1f); shape.setLayerMask(Chipmunk.NOT_GRABABLE_MASK); mSpace.addStaticShape(shape); } } // Add lots of pentagons float moment = Chipmunk.calculateMomentForPoly(1f, verts, WYPoint.makeZero()); for (int i = 0; i < 50; i++) { Body body = Body.make(1f, moment); float x = frand() * s.width - s.width / 2; body.setPosition(x, s.height / 2 * 5 / 7); mSpace.addBody(body); Poly shape = Poly.make(body, verts, WYPoint.makeZero()); shape.setFriction(0.4f); mSpace.addShape(shape); } schedule(new TargetSelector(this, "update(float)", new Object[] {0f})); }
/** \if English Result of collision detection \else 碰撞检测的结果 \endif */ public class CDResult { /** \if English collision point count \else 碰撞点个数 \endif */ public int pointCount = 0; /** * \if English collision points, max is two and they are in world coordinates \else 碰撞点数组, * 最多两个且用的是全局坐标 \endif */ public WYPoint[] points = new WYPoint[] {WYPoint.makeZero(), WYPoint.makeZero()}; /** * \if English normal of collision, in world coordinate. It points from first node to second node. * \else 碰撞方向, 以全局坐标表示. 总是从第一个节点指向第二个节点. \endif */ public WYPoint normal = WYPoint.makeZero(); /** \if English reset result object to all zero \else 重置所有数据为0 \endif */ public void clear() { pointCount = 0; points[0].x = points[0].y = points[1].x = points[1].y = 0; normal.x = normal.y = 0; } /** * \if English Get float array of collision points * * @return float array of collision points \else 得到碰撞点的浮点数组表示形式 * @return 碰撞点的浮点数组 \endif */ public float[] getPoints() { if (pointCount == 1) return new float[] {points[0].x, points[0].y}; else if (pointCount == 2) return new float[] {points[0].x, points[0].y, points[1].x, points[1].y}; else return new float[0]; } }
/** * \if English Map a clip to external atlas texture * * @param fromClipIndex source clip index * @param tex external texture \else 映射一个分片到外部的图片集上 * @param fromClipIndex 被映射的分片索引 * @param tex 图片集的贴图对象 \endif */ public void mapClip(int fromClipIndex, Texture2D tex) { mapClip(fromClipIndex, tex, WYPoint.makeZero()); }
/** Get the global gravity vector. */ public WYPoint getGravity() { WYPoint p = WYPoint.makeZero(); nativeGetGravity(p); return p; }
/** * \if English get pin point * * @return global coordinate of pin point \else 得到pin point * @return pin point的全局坐标 \endif */ public WYPoint getPinPoint() { WYPoint p = WYPoint.makeZero(); nativeGetPinPoint(p); return p; }