schteppe / cannon.js

A lightweight 3D physics engine written in JavaScript.
http://schteppe.github.com/cannon.js
MIT License
4.64k stars 704 forks source link

Detect collision between static and kinematic objects #188

Open Gragx opened 9 years ago

Gragx commented 9 years ago

Hello there

I'm trying to get objects flying around with a constant velocity, unaffected by gravity or anything. So far i'm using kinematic objects to achieve that. My issue is, that I still want to be able to detect collisions between these objects and other, static objects. I don't need anything to happen to the objects on collision, but I want to be able to detect when it happens.

Kinematic objects don't emit collisions with static bodies. So how should I approach this? Can I somehow get the collision event to show up anyways or do I have to get rid of the kinematic type?

schteppe commented 9 years ago

Right now it's not possible to get the collide event between static / kinematic or kinematic / kinematic. So you'll have to work around it. There are a few ways.

For example, you could use a veeery heavy dynamic body and turn off gravity. Apply gravity manually on other dynamic bodies.

Another way is to add a dynamic body and sync its position with the kinematic one. Use it only for catching the events.

If you use simple shapes, you could probably implement the collision detection manually.

Or use the Narrowphase to see if the bodies overlap (untested code):

var result = []; world.narrowphase.getContacts([bodyA], [bodyB], world, result, [], [], []); var overlaps = result.length > 0;

Collision events for kinematic bodies is something that will be added. It is straight forward to implement but will need some work.

Gragx commented 9 years ago

Thanks. I like the idea with an additional dynamic body. Will give that a try

EDIT: Works perfectly. Thanks a lot

schteppe commented 9 years ago

No problem! Will keep this issue open until the feature is implemented, if it's OK with you.

Gragx commented 9 years ago

Certainly ok with me. Am looking forward to the feature, thanks for your great work!

schteppe commented 8 years ago

Added support for kinematic vs static and kinematic vs kinematic collision detection in the new beginContact and endContact events! Not sure if you still need it but hey :)

devhang commented 6 years ago

Would you mind to give me some hints how to implement the collision detection for the KINEMATIC sphere to a STATIC plane with the events you have mentioned. I have no clue, I cannot find the event related to 'beginContact', 'endContact'. Am I misunderstanding your meaning? thanks

devhang commented 6 years ago

Oops, I just using the build/cannon.js from the GIT, it seems the version is not the latest one, after grunt rebuild, I can listen the 'beginContact', 'endContact' events from the World object. Nice & thanks!