2

Is it possible to calculate distance at which the body will fly off in method

-(BOOL)ccPhysicsCollisionBegin:typeA:typeB

I need the exact point, where the body would end up after collision. In other words, will it fly off towards boundary or not; whether the force is strong enough to push it to the boundary or not. Any ideas? Thanks!

UPDATE: Found some code. I was told, it's exactly what I want. But I'm not familiar with 'pure' Chipmunk. So, I can't use it anyway yet. The code needs to be inserted in draw method.

// 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];
James Webster
  • 31,873
  • 11
  • 70
  • 114
Vitalii Vashchenko
  • 1,777
  • 1
  • 13
  • 23
  • Difficult to predict if anything can get in the way. Otherwise it requires integrating position with velocity and gravity over a limited period of time or until gravity turns velocity.y negative. – CodeSmile Apr 01 '14 at 12:29

0 Answers0