MatterJS

Introduction

Eva.js base on Matterjs, At present, only a small part of the content has been accessed. If more content is needed, continue to access it.

Install

With NPM

  1. tnpm install @eva/plugin-matterjs

In Browser

  1. <script src="https://unpkg.com/@eva/plugin-matterjs@1.2.x/dist/EVA.plugin.renderer.matterjs.min.js"></script>

Usage

  1. // 1. Introduced after installing the physics engine
  2. import {PhysicsSystem, Physics, PhysicsType} from '@eva/plugin-matterjs';
  3. // 2. Register the plugin in EVA
  4. const game = new Game({
  5. autoStart: true,
  6. frameRate: 60,
  7. systems: [
  8. new RendererSystem({
  9. transparent: true,
  10. canvas: canvasNode,
  11. backgroundColor: 0xfee79d,
  12. width: 750,
  13. height: 1624,
  14. resolution: 2,
  15. }),
  16. new GraphicsSystem(),
  17. new PhysicsSystem({
  18. resolution: 2, // Keep the resolution of the RendererSystem consistent
  19. // isTest: true, // Whether to enable debugging mode
  20. // element: document.getElementById('game-container'), // Mount point of canvas node in debug mode
  21. world: {
  22. gravity: {
  23. y: 2, // gravity
  24. },
  25. },
  26. mouse: {
  27. open: true,
  28. },
  29. }),
  30. new TextSystem(),
  31. new ImgSystem(),
  32. new EventSystem(),
  33. ],
  34. });
  35. // 3. New game entity object
  36. const box = new GameObject('box', {
  37. size: {
  38. width: 75,
  39. height: 75,
  40. },
  41. position: {
  42. x: 75 + Math.random() * 300,
  43. y: 75,
  44. },
  45. // Origin will be mapped to the center of mass of the physical system, which is the geometric center of the physics in a two-dimensional environment. It must be fixed here as {x:0.5,y:0.5}
  46. origin: {
  47. x: 0.5,
  48. y: 0.5,
  49. },
  50. });
  51. // 4. Add Componet to the game object
  52. const physics = box.addComponent(
  53. new Physics({
  54. type: PhysicsType.RECTANGLE,
  55. // body:{
  56. // options:{
  57. //}
  58. //}
  59. bodyOptions: {
  60. isStatic: false, // Whether the object is still, any force acting on the object in a static state will not produce any effect
  61. restitution: 0.8,
  62. frictionAir: 0.1,
  63. friction: 0,
  64. frictionStatic: 0,
  65. force: {
  66. x: 0,
  67. y: 0,
  68. },
  69. },
  70. stopRotation: true, // default false, usually do not need to be set
  71. }),
  72. );
  73. //Currently supported collision events collisionStart collisionActive collisionEnd
  74. //Rigid body events tick, beforeUpdate, afterUpdate, beforeRender, afterRender, afterTick usually use beforeUpdate and afterUpdate
  75. physics.on('collisionStart', (gameObject1, gameObject2) => {});
  76. console.log("physics",physics);

参数

type PhysicsType

rigid body model shape.

  • RECTANGLE
  • CIRCLE
  • POLYGON regular polygon

sides

Number of sides while type with POLYGON.

radius

The radius of POLYGON / CIRCLE.

bodyOptions

Visiting Matterjs offical to learn more.