Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

9 years ago by itsOgden

I have a game that was in progress a year ago and has sat dormant in the meantime. I am working on getting Box2D switched over to Joncom's Box2D Sugar and adding Impact++.

Most of the Box2D programming was done by a guy who is no longer on the project so I'm trying to figure out how to convert this particular bit of code over. Any help from someone who understands it would be very wonderful!

Current Code:
addContactListener: function() {
    var listener = new b2.ContactListener();
    listener.Add = function(point){
        //console.log('Add Contact', point);
        var body1 = point.shape1.GetBody();
        var body2 = point.shape2.GetBody();
        
        if((body1 && body2) &&
           ((body1.m_userData || {}).type == 'player' || (body2.m_userData || {}).type == 'player') &&
           (((body1.m_userData || {}).type == 'magnet' || (body2.m_userData || {}).type == 'magnet') ||
           ((body1.m_userData || {}).type == 'collidable' || (body2.m_userData || {}).type == 'collidable'))
           ) {
            var eA = body1.m_userData.type == 'player' ? body1.entity : body2.entity; 
            var eB = body1.m_userData.type != 'player' ? body1.entity : body2.entity;
            
            if (eA && eB) {
                eA.hit(5, eB);
                eB.hit(5, eA);
            }
        }
    }
    
    listener.Remove = function(point){
        //console.log('Add Contact', point);
        var body1 = point.shape1.GetBody();
        var body2 = point.shape2.GetBody();
        
        if((body1 && body2) &&
           ((body1.m_userData || {}).type == 'player' || (body2.m_userData || {}).type == 'player') &&
           (((body1.m_userData || {}).type == 'magnet' || (body2.m_userData || {}).type == 'magnet') ||
           ((body1.m_userData || {}).type == 'collidable' || (body2.m_userData || {}).type == 'collidable'))
           ) {
            var eA = body1.m_userData.type == 'player' ? body1.entity : body2.entity; 
            var eB = body1.m_userData.type != 'player' ? body1.entity : body2.entity;
            
            if (eA && eB) {
                eA.unhit(5, eB);
                eB.unhit(5, eA);
            }
        }
    }
},

I believe it's very similar to this (line 151 of joncom.box2d.game)
setupContactListener: function() {
    var callback = function(method, contact, argument) {
        var a = contact.GetFixtureA().GetBody().entity;
        var b = contact.GetFixtureB().GetBody().entity;
        if(a && b) {
            a[method](b, contact, argument);
            b[method](a, contact, argument);
        } else if(a && !b) {
            a[method](null, contact, argument);
        } else if(b && !a) {
            b[method](null, contact, argument);
        }
    };
    var listener = new Box2D.Dynamics.b2ContactListener();
    listener.BeginContact = function(contact) {
        callback('beginContact', contact);
    };
    listener.EndContact = function(contact) {
        callback('endContact', contact);
    };
    listener.PostSolve = function(contact, impulse) {
        callback('postSolve', contact, impulse);
    };
    listener.PreSolve = function(contact, oldManifold) {
        callback('preSolve', contact, oldManifold);
    };
    ig.world.SetContactListener(listener);
},

If it helps, here is an example of how it's being used:
if( !ig.global.wm ) {
    this.body.m_userData.type = 'magnet';
    
    // Set up some box2d properties
    this.body.m_linearDamping = this.initLinearDamping;
    this.body.m_angularDamping = this.initAngularDamping;
    
    // Set up our custom physics properties
    this.body.polarity = settings.polarity;
    this.body.strengthMultiplier = settings.strengthMultiplier;
    this.body.stationary = settings.stationary;
    
    // Save our initial position
    this.initPosition = new b2.Vec2(this.body.GetPosition().x, this.body.GetPosition().y);
}

9 years ago by Joncom

Where is that "current code" from? My feeling is that you'd probably just delete all of it. Because Box2D Sugar handles setting up of the contact-listening. Then you would, for example, just overload your entity .beginContact function:

beginContact: funciton(contact) {
    console.log("Entity just touched something...");
}

9 years ago by itsOgden

Hey Joncom,

Side note: Your work is awesome and I really appreciate it!

The "current code" is stuff that the previous programmer had injected into the original Box2D code. Unfortunately, once that's gone, all my entities using Box2D break so I'm trying to figure out exactly what it's doing and how to convert the example code so that it responds properly.

I'll be honest, Box2D really confuses me so I'm struggling a bit figuring out how to convert what the previous programmer did.
Page 1 of 1
« first « previous next › last »