matter-js | a 2D rigid body physics engine for the web ▲● ■ | Game Engine library
kandi X-RAY | matter-js Summary
kandi X-RAY | matter-js Summary
Matter.js is a JavaScript 2D rigid body physics engine for the web. Demos ・ Gallery ・ Features ・ Plugins ・ Install ・ Usage ・ Examples ・ Docs ・ Wiki ・ References ・ License.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Obtain a module .
- Define this object
- close one
- handles
- inject 1
- inner function
- inbound set
- perform a n
- recursive function
- Compute the q .
matter-js Key Features
matter-js Examples and Code Snippets
Community Discussions
Trending Discussions on matter-js
QUESTION
I building a react website with matter.js. I am using the useEffect
hook to render stuff to the canvas with matter.js (I found most of this code here). However, when I try to draw anything else to the canvas, nothing appears. Everything matter.js-related works.
ANSWER
Answered 2022-Mar-24 at 00:36Currently, you're drawing one time, up front, on the same canvas that MJS wipes per frame. So, you draw your circle, then MJS wipes the canvas immediately when it renders its first frame. You never attempt to draw again as the engine and renderer run onward.
I see a few solutions (at least!). Which is most appropriate depends on your specific use case.
- Draw on the canvas on each frame inside the
afterRender
callback for therender
object. This is the simplest and most direct solution from where you are now and I provide an example below. - Add a second transparent canvas on top of the one you're providing to MJS. This is done with absolute positioning. Now you can do whatever you want in this overlay without interference from MJS.
- Run MJS headlessly, which gives you maximum control, as the MJS built-in renderer is mostly intended for protoyping (it's a physics engine, not a graphics engine). Once you're headless, you can render whatever you want, whenever you want, however you want. I have many demos of headless rendering in my other answers, both to the plain DOM and with React and p5.js.
As an aside, this doesn't appear related to React or useEffect
in any way; the same problem would arise even if you were injecting a plain canvas in vanilla JS.
Also, keep in mind that whatever you draw is totally unrelated to MJS other than the fact that it happens to be on the same canvas. Don't expect physics to work on it, unless you're using coordinates from a body MJS knows about.
Although you haven't provided a full component, here's a minimal proof-of-concept of the afterRender
approach mentioned above:
QUESTION
I'm trying to use Matter.Query.region
to see if the character in my game is grounded. But, when I try to run region
with a bounds object that I created (displayed with the dots shown in the game), it doesn't come up with any collisions even though it is clearly intersecting other bodies.
Code:
...ANSWER
Answered 2022-Mar-24 at 00:20The bounds object doesn't appear to be properly created. The purple p5 vertices you're rendering may be giving you a false sense of confidence, since those aren't necessarily related to what MJS sees.
It's actually a pretty simple fix, passing an array of vertices instead of individual arguments:
QUESTION
I recently saw the matter.js on the coding train channel from 2017, I've managed to replicate the examples shown on those videos but now I wanted to do something more advanced. There is a Composite module called stack in the documentation used to create a soft body in the matter.js demos but I wonder if there is a way to draw this using p5 instead.
This is what I've gotten so far, any help is appreciated
...ANSWER
Answered 2021-Sep-22 at 02:38I think the main issue with your code was that you weren't adding the Composite
to the world
. Here's a working example with a few other tweaks:
QUESTION
I've been trying to make a ball pit sim using matter.js + p5.js to use as an interactive website background.
With some help from Mr Shiffman's video I've managed to get it working well with circle shapes, but I want to take it to the next level and use custom blob shapes (taken from the client's logo) and apply the same physics to them.
I've been able to get the custom shapes to render using a combination of p5's Beginshape() and matter's bodies.fromVertices. It sort of works as you can see but the physics is very weird, and the collisions don't seem to match even though I used the same vertices for both.
I think it may be to do with this quote from the p5 docs
Transformations such as translate(), rotate(), and scale() do not work within beginShape().
but I don't know what I can do to get around this as I need them to be able to translate / rotate for the physics to work...
Any ideas? help much appreciated!
Codepen ...ANSWER
Answered 2021-Aug-04 at 19:43The root issue here is that matter.js positions objects based on their center of mass rather than the origin of the coordinate system that your vertices are in, and it is clear that the origin of the coordinate system for you blob vertices is not its center of mass since all of your vertices are positive. You can calculate the center of mass for your blob and then use that offset before drawing:
QUESTION
I am playing with the matterjs library, it's awesome!
I am trying to figure out how I can draw a semicircle like these:
I tried with Bodies.circle
, Bodies.rectangle
and Bodies.polygon
but with no luck.
It seems like a pretty simple shape but I don't know if it is possible to draw a semicirle with the library.
Any recommendation would be great!
...ANSWER
Answered 2021-Jul-03 at 00:21SVGs are one approach:
QUESTION
I have recently downloaded matterjs using npm using the command npm install matter-js.
I use vscode for my html project. I have a folder called Node. And another folder ( which is automatically generated ) called node-modules.
The folder node-modules is not inside Node folder.
The matter-js module is present inside node-modules.
Now inside my Node folder I created a file called index.html and added this line 👇
...ANSWER
Answered 2021-Jul-01 at 06:56You can use CDN in HTML follow this web site https://cdnjs.com/libraries/matter-js
or you can add this
QUESTION
I'm running a MatterJS physics simulation by adding boxes to the physics engine, and at the same time creating a DOM element with the same dimensions.
Then, I'm looping through the physics boxes to get their coordinates. I then use those coordinates to position the corresponding DOM element. This seems to work correctly, except that my static "ground" box seems to have a 10 pixel invisible radius around it?
My boxes are 40x40 pixels. The ground is at 400 pixels. The boxes stop falling at 350 pixels. So why the 10 pixels discrepancy?
Here is an example of the issue in CodePen
EDIT: if I render the same boxes in a canvas, the discrepancy isn't there. Codepen Canvas
...ANSWER
Answered 2021-May-20 at 15:04The problem is that when you set your x,y positions for the box, you didn't take into account the heights. So the displayed positions didn't match the actual positions.
The only line I've really changed is this.div.style.transform = `translate(${pos.x}px, ${pos.y}px) rotate(${degrees}deg)`;
.
The 10-pixel discrepancy was caused because the 60px-deep base was being displayed 30px (half the depth) too low, while the 40px-deep boxes were being displayed 20px too low - net 10px above the base. It all looked OK when I tried setting the heights to 1px, which was how I discovered the bug.
QUESTION
Hello, I am trying to use Matter JS on codepen, I firstly received the error that the variable Matter was not defined, I already fixed this but another error came.
Code Preview:
...ANSWER
Answered 2021-Jan-26 at 22:02QUESTION
I literally copied the code from the getting started section of the Matter.js library but an Uncaught TypeError occurs: "Engine.run is not a function". I searched everywhere on the web but nothing helps. As a precaution, I added an event listener to run the code once everything is loaded.
...ANSWER
Answered 2021-Apr-14 at 10:18You are using wrong version of matter-js library, try with latest version. I just tried with 0.15.0, it's working fine.
matter-js >= 0.10.0
has the support Render.run()
. Please make sure the version which you are using for supported features.
QUESTION
total newbie with Vue.js here, so sorry if I'm missing something obvious.
I want to load components based on the content of a variable - so if the content of my variable changes, I want the component to change accordingly. I have all components imported (e.g. import Project1 from "@/components/Project1";
), which again is saved in components: {'p1': Project1}
I use
with goTo.page being defined in data as
data() {return {goTo: {page: "p1"}}}
.
The problem seems to be the reactivness of goTo.page. I update it via this.$set(this.goTo, "page", "p2");
. When running, i get the error Uncaught TypeError: Cannot read property 'page' of undefined
, which I honestly don't understand, since its initialized with "p1".
ANSWER
Answered 2021-Mar-14 at 18:05this
is not available in . Everything "inside"
this
, is available.
In addition to the above and per your update,
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install matter-js
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page