10 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:
I believe it's very similar to this (line 151 of joncom.box2d.game)
If it helps, here is an example of how it's being used:
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); }