Cocos2D v3计算碰撞后的距离

时间:2014-04-01 11:00:47

标签: cocos2d-iphone chipmunk

是否可以通过方法

计算身体飞离的距离
-(BOOL)ccPhysicsCollisionBegin:typeA:typeB

我需要准确的点,身体在碰撞后会结束。换句话说,它会飞向边界吗?力是否足够强大以将其推向边界。 有任何想法吗?谢谢!

更新: 找到一些代码。我被告知,这正是我想要的。但我不熟悉纯粹的'花栗鼠。所以,我还是无法使用它。代码需要插入draw方法。

// We need a copy of the body and shape to simulate them forwards without doing a full step.
// For performance sake, let's just copy them onto the stack using the C-API.
cpBody body = *(cage.body.body);
cpPolyShape shape = *((cpPolyShape *)cage.shape.shape);
shape.shape.body = &body;

cpVect gravity = space.gravity;

// Check ahead up to 300 frames for a collision.
for(int i=0; i<300; i++){
    // Manually update the position and velocity of the body
    cpBodyUpdatePosition(&body, FIXED_TIMESTEP);
    cpBodyUpdateVelocity(&body, gravity, 1.0f, FIXED_TIMESTEP);

    // Perform a shape query to see if the cage hit anything.
    if(cpSpaceShapeQuery(space.space, (cpShape *)&shape, NULL, NULL)){
        // If it did, draw the box's outline.
        cpVect verts[4];
        for(int i=0; i<4; i++){
            verts[i] = cpBodyLocal2World(&body, cpPolyShapeGetVert((cpShape *)&shape, i));
        }

        [self drawPolyWithVerts:verts count:4 fillColor:ccc4f(0, 0, 0, 0) borderWidth:1.0 borderColor:ccc4f(0, 0, 0, 1)];
        break;
    } else if(i%3==0){
        // Otherwise, just draw a dot every 10 frames along the path.
        [self drawDot:body.p radius:5.0 color:ccc4f(0, 0, 0, 0.5)];
    }
}

[super draw];
[self clear];

0 个答案:

没有答案