11

I'm creating simple car game with cannon.js and I'm struggling with this.

What I want to do:

When I run with car into another object (e.g. sphere) I want to know about it.

For example increase score or whatever, but without applying forces to both objects.

What I unsuccesfully tried:

Use

chassisBody.addEventListener("collide",function(e){ "mycode"};

with combination groups

var GROUP1 = 1; etc..

but point of groups I guess is mark which object I want and don't want to collide, and I want them to "collide" but without actually applying forces to them, only registering that their bodies intersected and firing my scoring code etc.

( I added threejs tag just in case someone might have stumbled across this, and I'm using it anyway )

EDIT 2022:

Here it is how it looks, warning, its an old code :)

https://martincerven.com/games/car_simulation/test.html

martinerk0
  • 403
  • 1
  • 4
  • 17

1 Answers1

9

The following code is taken from this example. I tested with your event listener callback and it worked fine.

Set collisionResponse of the rigid body to 0:

b2.collisionResponse = 0; // no impact on other bodys
b2.addEventListener("collide", function(e){ console.log("sphere collided"); } );
Falk Thiele
  • 4,424
  • 2
  • 17
  • 44
  • Yes, but when you put car and sphere in different groups, they will not collide, but you **will not** be notified of the mesh intersection. And I want them not to physically collide, but be notified when they **intersect**. I think my question is exactly this: https://github.com/schteppe/cannon.js/issues/188 and for last hour I'm trying to implement it, but without success. ( put additional kinematic box around car, create kinematic box on the ground, and have notification when they collide). Or am I totally missing something? – martinerk0 Jul 31 '15 at 17:17
  • Ideally, I would like to have callback: `Body.addEventListener("intersect",function(e){ console.log("they intersected!"); }` – martinerk0 Jul 31 '15 at 17:24
  • 1
    I tested your specific case and indeed got no callback then. In ammo.js this is solved by setting the collision flag "no_contact_response" or by using so called "ghost bodys" as triggers. But schteppe is also reading Stackoverflow questions so eventually hes going to reply ;) – Falk Thiele Jul 31 '15 at 17:29
  • I found `collisionResponse` property in http://schteppe.github.io/cannon.js/docs/classes/Body.html properties, but when I set it to false on car and sphere it's still not working. – martinerk0 Jul 31 '15 at 17:45
  • I havent tested with a vehicle, but its working fine for me with rigid bodys. Just set the ```collisionResponse``` on the sphere to ```0``` so it becomes a ghost object and still triggers the collision callback. – Falk Thiele Jul 31 '15 at 17:57
  • 1
    this is weird: when I put it in constructor it didn't work, but when as ` sphereBody.collisionResponse = 0;` it DID WORK! – martinerk0 Jul 31 '15 at 18:09
  • Thank you for your help, and I edited out that stuff with groups, remove it please because groups have nothing to do with it. – martinerk0 Jul 31 '15 at 18:16
  • For anyone else finding this question, I suggest you look at the answer edit history because the edit history contains the solution I needed, not the current answer. TL;DR: it involves using `body.collisionFilterGroup` with `body.collisionFilterMask` (using powers of 2) to selectively make collisions happen. – aggregate1166877 Sep 20 '22 at 05:56