GithubHelp home page GithubHelp logo

Assigning interactivity about matter-js HOT 12 CLOSED

mulhoon avatar mulhoon commented on May 5, 2024
Assigning interactivity

from matter-js.

Comments (12)

liabru avatar liabru commented on May 5, 2024

Cheers, glad you are liking it.

If you grab the latest master version, it includes the new collision filter system that works similar to Box2d (see #42 and then see the collision filtering demo code).

The mouse constraint has support for it too using the same properties so you should be able to use it to limit what you can drag with.

Also firing an event on touch to determine which body is being handled would be great, is this built in?

Yeah, you should use the startdrag event on the MouseConstraint object, the event payload includes the current body reference.

If you need more, all you need to do is make a copy of the MouseConstraint class and just implement what you need, maybe change the name of the class too.

Hope that helps

from matter-js.

mulhoon avatar mulhoon commented on May 5, 2024

Thanks for the reply.

I'm a newcomer to physics in js (but not js) and I've been a little confused by the categories/groups as bit fields. How do you assign bodies to groups? Do you have any examples of 2 bodies, one draggable and one not?

Sorry if this isn't the right place to ask questions, I can hop over to stack overflow if you'd prefer.

Thanks
Nic

from matter-js.

liabru avatar liabru commented on May 5, 2024

It's a little confusing, but here's a good article about it you should read:
http://www.iforce2d.net/b2dtut/collision-filtering

Basically the collisionFilter.category states what the body is and the collisionFilter.mask states what the body will collide with. Both of these are integers but should be interpreted as bit fields.

It may help for you to look at the Detector.canCollide function to get an idea of how these masks are bitwise combined when testing whether objects should collide.

If you've got the collision filter demo working that I mentioned above, here's how you can specify a collision filter mask for the mouse:

_mouseConstraint.collisionFilter.mask = defaultCategory | blueCategory | greenCategory;

This mask will now only allow mouse interaction with bodies in the default or blue or green categories (defined earlier in the demo code, the bodies have their categories set at their creation).

This means that the bodies assigned to the red category will not be draggable because their category mask has not been included in the mask.

Hope that helps a bit more.

from matter-js.

mulhoon avatar mulhoon commented on May 5, 2024

Ahh thank you!
Spent a couple of hours trying to figure this out. Your little snippet works perfectly. Brilliant!

As for the mouse event payload, event.source.body always seems to be null, along with _mouseConstraint.body hmmm...

I have the latest dev version built.

Thanks
Nic

from matter-js.

liabru avatar liabru commented on May 5, 2024

I think it should just be event.body, whereas event.source is a reference back to the event emitter?

from matter-js.

mulhoon avatar mulhoon commented on May 5, 2024

Thanks, It still returns null on event.body and event.source for me.

from matter-js.

liabru avatar liabru commented on May 5, 2024

Ah it might be that I've not updated the master build since I fixed it.

Can you try again with the latest master build?

from matter-js.

mulhoon avatar mulhoon commented on May 5, 2024

I'm afraid event.source.body is still null and there is no event.body in this build
Thanks for your help.

from matter-js.

liabru avatar liabru commented on May 5, 2024

I'm a little confused, because it works fine in the events demo. If you run it and open the console, then start draging some bodies, the event object has a body?

from matter-js.

mulhoon avatar mulhoon commented on May 5, 2024

Yes startdrag and enddrag return event.body (but not event.source.body that's still null)

mousedown and mouseup don't have event.body or event.source.body.
Is that by design?

Thanks,
Nic

from matter-js.

liabru avatar liabru commented on May 5, 2024

You should use event.body because event.source.body purposely goes null when dragging stops (although maybe I should change it to do that after the event has been fired).

Yea the mouse events are emitted purely based on mouse states, they don't indicate anything about bodies.

from matter-js.

liabru avatar liabru commented on May 5, 2024

Hopefully this answered your question? So closing this one, thanks.

from matter-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.