Pseudo static object responding to forces but not impacts

Discuss new features and future development.
Post Reply
theisegeberg
Posts: 6
Joined: Tue Aug 28, 2012 5:32 am
Contact:

Pseudo static object responding to forces but not impacts

Post by theisegeberg »

Hi guys,

I know chipmunk physics pretty well. But I've run into a problem I can't solve easily. I should say I'm using chipmunk pro.

I'd like to create a planet moving through space and affected by forces. But I want it to react to collisions like a static object. So I want it to be physics simulated and apply forces to it. But once another object hits it I want it to ignore the collision (of course the other object shouldn't ignore it but bounce off it as if it was static/infinite mass).

Obviously two of these passive objects couldn't collide, but they'll be grouped together, and will never touch by design.

I've tried to do it in collision handles, but I can't really figure it out, I'm not asking for source code, I'd like a point in the right direction though :)

Thanks,
Theis

SOLUTION:

Code: Select all

-(BOOL) begin:(cpArbiter *)arbiter space:(ChipmunkSpace *)space {
    GameSprite * objectA = ((__bridge ChipmunkBody*)arbiter->body_a_private->data).data;
    
    GameSprite * objectB = ((__bridge ChipmunkBody*)arbiter->body_b_private->data).data;
    
    //NSLog(@"Begin %@ %@",objectA,objectB);
    
    if (objectA.ignoreImpacts) {
        //objectA.storedVelocity = objectA.chipmunkBody.vel;
        objectA.storeMass = objectA.chipmunkBody.mass;
        objectA.chipmunkBody.mass = INFINITY;
        objectA.storeMoment = objectA.chipmunkBody.moment;
        objectA.chipmunkBody.moment = INFINITY;
        
    
    }
    if(objectB.ignoreImpacts) {
        objectB.storeMass = objectB.chipmunkBody.mass;
        objectB.chipmunkBody.mass = INFINITY;
        objectB.storeMoment = objectB.chipmunkBody.moment;
        objectB.chipmunkBody.moment = INFINITY;
    }
    
    return YES;
}

-(void) separate:(cpArbiter *)arbiter space:(ChipmunkSpace *)space {
    //NSLog(@"Separate");
    
    GameSprite * objectA = ((__bridge ChipmunkBody*)arbiter->body_a_private->data).data;
    
    GameSprite * objectB = ((__bridge ChipmunkBody*)arbiter->body_b_private->data).data;
    
    if (objectA.ignoreImpacts) {
        objectA.chipmunkBody.mass = objectA.storeMass;
        objectA.chipmunkBody.moment = objectA.storeMoment;
    }
    
    if (objectB.ignoreImpacts) {
        objectB.chipmunkBody.mass = objectB.storeMass;
        objectB.chipmunkBody.moment = objectB.storeMoment;
    }
}
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Pseudo static object responding to forces but not impact

Post by slembcke »

You'll need to integrate the forces to the object yourself, but that's super easy to do.

Give the pseudo static object (PSO) infinite mass. Objects colliding with it will treat it as static. Then create a velocity integration function for it (cpBody.velocity_func, see the Planet demo for an example). You can really just copy/paste the source of the default velocity function, cpBodyUpdateVelocity(). The only change you'd need to make is to use a finite mass for the body instead of the infinite one stored on it.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
theisegeberg
Posts: 6
Joined: Tue Aug 28, 2012 5:32 am
Contact:

Re: Pseudo static object responding to forces but not impact

Post by theisegeberg »

Thank you for the reply, you guys are super helpful.

I ended up using the collision method, because I can allow two PSO's to collide even if they act as if they have infinite mass towards some other objects. So I detect if both are PSO's and then collide then normally (or in a very heavy way).

Again thank you for the quick reply, I love this physics engine! :)
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests