lo-th / Oimo.js

Lightweight 3d physics engine for javascript
http://lo-th.github.io/Oimo.js
MIT License
3.07k stars 303 forks source link

Events? #11

Closed Canta closed 10 years ago

Canta commented 10 years ago

Hi there, lo-th. Thanks for this great work, Oimo.js is fantastic!

I'm building my own little engine for some WebGL apps, mostly games, thinking on a client-server context. Three.js is my tool of choice for client-side rendering, and i was developing my own server using node.js. Did a few steps on that direction, but given that i'm a newbie on physics it was a kinda rough path. That's how i discovered Oimo, looking for some help in the physics field.

I've already adapted Oimo's basic demo to work with node.js for experiments, and then adapted some of my own project's classes to work with Oimo's classes (mostly inheriting). It seems to work fine.

Thing is, now i need to communicate Oimo's classes with my own. My classes use an EventEmmiter pattern, with ".on()" and ".trigger()" kinda stuff for easy high-level configuration and for avoiding tight coupling. I was expecting to handle Oimo's inner events somehow (like collisions, for example), but still haven't found a way looking at Oimo's source.

So, the question is: does Oimo's classes implement some event model i can hook on?

If the answer is "no", do you have any plan on implementing an event model on it?

lo-th commented 10 years ago

hi thank's, no there is no event, i have to look best way to do that. but ShapeConfig have a extra function i don't explaine: the collision filtering. var sc = new OIMO.ShapeConfig(); var group1 = 1 << 0; var group3 = 1 << 2; var all = 0xffffffff;

sc.belongsTo = group1; sc.collidesWith = all & group3; i have to make demo on that

Canta commented 10 years ago

Oh... i will have to take a look at collidesWith then, thank you. Didn't saw it before.

For the record, while asking here, i was making a slightly modified Oimo.js version implementing some degree of a EventEmmiter pattern. I don't plan to publish that, it's just for my own use. But the strategy is simple: in World's constructor, after World instances CollisionDetectors, i set a reference to the world instance inside every detector instance, by using a simple loop. Then, in the detector code, when there's a collision, i add a call to one of the world reference's (also added by me) method: the "emit()" function. So, calling world.emit("collision", pair) would trigger any event handler attached to the world for the "collision" event, and would take the pair of objects as parameter. And then it's just an application problem to properly set those event handlers as necessary.

It's kinda trivial to implement events that way, i just need to take the time to read and understand Oimo to know where to put the "emit" call and what to pass as parameter.

There are a few NPM packages for nodejs that gives a ready to use EventEmmiter class, in case you would like to take a look at it: https://www.npmjs.org/package/event-emitter https://www.npmjs.org/package/tiny-eventemitter etc.

Cheers.

lo-th commented 10 years ago

great i add new function to world you can see in test_moving.html

you can assign name to rigidBody and test if two name is on collision or not world.checkContact('paddle', 'sphere') return true or false

only on dev version