Skip to content

Commit

Permalink
Pass contact normal into collision callback. Closes #98
Browse files Browse the repository at this point in the history
  • Loading branch information
chandlerprall committed Jul 16, 2013
1 parent ae2a598 commit 867b392
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
30 changes: 25 additions & 5 deletions physi.js
Expand Up @@ -26,6 +26,7 @@ window.Physijs = (function() {
CONSTRAINTREPORT: 3
},
REPORT_ITEMSIZE = 14,
COLLISIONREPORT_ITEMSIZE = 5,
VEHICLEREPORT_ITEMSIZE = 9,
CONSTRAINTREPORT_ITEMSIZE = 6;

Expand Down Expand Up @@ -644,14 +645,17 @@ window.Physijs = (function() {
*/

var i, j, offset, object, object2,
collisions = {}, collided_with = [];
collisions = {}, collided_with = [], normal_offsets = {};

// Build collision manifest
for ( i = 0; i < data[1]; i++ ) {
offset = 2 + i * 2;
offset = 2 + i * COLLISIONREPORT_ITEMSIZE;
object = data[ offset ];
object2 = data[ offset + 1 ];

normal_offsets[ object + '-' + object2 ] = offset + 2;
normal_offsets[ object2 + '-' + object ] = -1 * ( offset + 2 );

if ( !collisions[ object ] ) collisions[ object ] = [];
collisions[ object ].push( object2 );
}
Expand All @@ -677,10 +681,26 @@ window.Physijs = (function() {
_temp1 = _temp_vector3_1.clone();

_temp_vector3_1.subVectors( object.getAngularVelocity(), object2.getAngularVelocity() );
_temp2 = _temp_vector3_1;
_temp2 = _temp_vector3_1.clone();

var normal_offset = normal_offsets[ object._physijs.id + '-' + object2._physijs.id ];
if ( normal_offset > 0 ) {
_temp_vector3_1.set(
-data[ normal_offset ],
-data[ normal_offset + 1 ],
-data[ normal_offset + 2 ]
);
} else {
normal_offset *= -1;
_temp_vector3_1.set(
data[ normal_offset ],
data[ normal_offset + 1 ],
data[ normal_offset + 2 ]
);
}

object.dispatchEvent( 'collision', object2, _temp1, _temp2 );
object2.dispatchEvent( 'collision', object, _temp1, _temp2 );
object.dispatchEvent( 'collision', object2, _temp1, _temp2, _temp_vector3_1 );
object2.dispatchEvent( 'collision', object, _temp1, _temp2, _temp_vector3_1.negate() );
}

collided_with.push( object2._physijs.id );
Expand Down
7 changes: 6 additions & 1 deletion physijs_worker.js
Expand Up @@ -64,7 +64,7 @@ var
WORLDREPORT_ITEMSIZE = 14, // how many float values each reported item needs
worldreport,

COLLISIONREPORT_ITEMSIZE = 2, // one float for each object id
COLLISIONREPORT_ITEMSIZE = 5, // one float for each object id, and a Vec3 contact normal
collisionreport,

VEHICLEREPORT_ITEMSIZE = 9, // vehicle id, wheel index, 3 for position, 4 for rotation
Expand Down Expand Up @@ -1238,6 +1238,11 @@ reportCollisions = function() {
offset = 2 + (collisionreport[1]++) * COLLISIONREPORT_ITEMSIZE;
collisionreport[ offset ] = _objects_ammo[ manifold.getBody0() ];
collisionreport[ offset + 1 ] = _objects_ammo[ manifold.getBody1() ];

_vector = pt.get_m_normalWorldOnB();
collisionreport[ offset + 2 ] = _vector.x();
collisionreport[ offset + 3 ] = _vector.y();
collisionreport[ offset + 4 ] = _vector.z();
break;
//}

Expand Down

0 comments on commit 867b392

Please sign in to comment.