Comments (12)
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.
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.
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.
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.
I think it should just be event.body
, whereas event.source
is a reference back to the event emitter?
from matter-js.
Thanks, It still returns null
on event.body
and event.source
for me.
from matter-js.
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.
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.
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.
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.
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.
Hopefully this answered your question? So closing this one, thanks.
from matter-js.
Related Issues (20)
- Render.setSize is not present in the build
- Trying to change seed, to take control of deterministic behavior
- How to suppress body tremors? HOT 2
- Why does the attractor only work on one of the two BODYs? HOT 1
- Using compound bodies with Bodies.fromVertices does not work it just shows a filled in shape. HOT 1
- Why don't bodies sometimes collide? HOT 1
- Constraints with distance limits (e.g. for ropes)
- Why might there be such an error? HOT 1
- Simple Polygon Physics Issues HOT 1
- How to add project to the Wiki gallery ?
- How to scale the world? HOT 2
- [Phaser 3] Position of matter sprite's image not correct in container HOT 1
- loss of sprite quality on mobile HOT 1
- `Composite.translate` didn't move the bodies included
- Create Matter.Bodies.fromVertices with rounded vertices
- MouseConstraint preventing clicks on buttons on mobile. HOT 2
- Vulnerability related to the formula of universal gravitation
- Body with high mass and low inertia behaves erratically
- The startdrag event of dragging a body does not trigger after losing focus.
- How to move camera in matter.js world?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from matter-js.