started to integrate matter for physics in the map

This commit is contained in:
Bachir Soussi Chiadmi 2023-03-27 22:29:33 +02:00
parent 80786f55bd
commit 772630d1a5
6 changed files with 149 additions and 29 deletions

11
package-lock.json generated
View File

@ -12,6 +12,7 @@
"@material-design-icons/svg": "^0.14.2", "@material-design-icons/svg": "^0.14.2",
"@mdi/font": "^7.1.96", "@mdi/font": "^7.1.96",
"granim": "^2.0.0", "granim": "^2.0.0",
"matter-js": "^0.19.0",
"pinia": "^2.0.21", "pinia": "^2.0.21",
"vue": "^3.2.38", "vue": "^3.2.38",
"vue-router": "^4.1.5" "vue-router": "^4.1.5"
@ -1752,6 +1753,11 @@
"sourcemap-codec": "^1.4.8" "sourcemap-codec": "^1.4.8"
} }
}, },
"node_modules/matter-js": {
"version": "0.19.0",
"resolved": "https://registry.npmjs.org/matter-js/-/matter-js-0.19.0.tgz",
"integrity": "sha512-v2huwvQGOHTGOkMqtHd2hercCG3f6QAObTisPPHg8TZqq2lz7eIY/5i/5YUV8Ibf3mEioFEmwibcPUF2/fnKKQ=="
},
"node_modules/mime-db": { "node_modules/mime-db": {
"version": "1.52.0", "version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
@ -3748,6 +3754,11 @@
"sourcemap-codec": "^1.4.8" "sourcemap-codec": "^1.4.8"
} }
}, },
"matter-js": {
"version": "0.19.0",
"resolved": "https://registry.npmjs.org/matter-js/-/matter-js-0.19.0.tgz",
"integrity": "sha512-v2huwvQGOHTGOkMqtHd2hercCG3f6QAObTisPPHg8TZqq2lz7eIY/5i/5YUV8Ibf3mEioFEmwibcPUF2/fnKKQ=="
},
"mime-db": { "mime-db": {
"version": "1.52.0", "version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",

View File

@ -12,6 +12,7 @@
"@material-design-icons/svg": "^0.14.2", "@material-design-icons/svg": "^0.14.2",
"@mdi/font": "^7.1.96", "@mdi/font": "^7.1.96",
"granim": "^2.0.0", "granim": "^2.0.0",
"matter-js": "^0.19.0",
"pinia": "^2.0.21", "pinia": "^2.0.21",
"vue": "^3.2.38", "vue": "^3.2.38",
"vue-router": "^4.1.5" "vue-router": "^4.1.5"

View File

@ -59,6 +59,14 @@ html,body{
} }
} }
#map-matter{
position: absolute;
z-index: -1;
top:0; left:0;
width:100%;
height:100%;
}
#map-concernements{ #map-concernements{
width:100%; width:100%;
height:100%; height:100%;

View File

@ -1,14 +1,24 @@
<script> <script>
// import { mapActions, mapState } from 'pinia' // https://brm.io/matter-js/docs/classes/Engine.html
// import { ConcernementsStore } from '@/stores/concernements' import {
// Engine,
// Render,
// World,
Bodies,
Body,
Events,
Composite,
// Composites,
// Constraint,
// Vertices,
// Mouse,
// MouseConstraint,
// Query,
// Common
} from "matter-js";
// import LoginBlock from '@components/block/LoginBlock.vue'
// import UserTools from '@components/block/UserTools.vue'
// import MA from '/api/ma-axios'
export default { export default {
inject: ['canvasMap'], inject: ['canvasMap'],
data() { data() {
@ -21,18 +31,23 @@ export default {
}, },
ray: 60, ray: 60,
time: 0, time: 0,
salientPoints: [] salientPoints: [],
// matter
body: null
} }
}, },
props: ['concernement'], props: ['concernement'],
created () { created () {
// console.log("ConcernementsMapItem concernement", this.concernement); console.log("ConcernementsMapItem concernement", this.canvasMap);
this.entites = this.concernement.entites this.entites = this.concernement.entites
this.parsePoints() this.parsePoints()
this.getSalientPoints() this.getSalientPoints()
if (this.canvasMap) { },
this.initCanvasMap() mounted() {
} // console.log('concernementItem mounted', typeof this.canvasMap.canvas);
// if (this.canvasMap) {
// // this.initCanvasMap()
// }
}, },
watch: { watch: {
// canvasMap (n, o) { // canvasMap (n, o) {
@ -40,7 +55,7 @@ export default {
// } // }
canvasMap: { canvasMap: {
handler (n, o){ handler (n, o){
console.log("concernementItem watch canvasMap.ctx", o, n); // console.log("concernementItem watch canvasMap.ctx", typeof this.canvas, o, n);
if (!this.canvas) { if (!this.canvas) {
this.initCanvasMap() this.initCanvasMap()
} }
@ -50,12 +65,36 @@ export default {
}, },
methods: { methods: {
initCanvasMap (){ initCanvasMap (){
// record canvas and ctx for rendering (drawing)
this.canvas = this.canvasMap.canvas this.canvas = this.canvasMap.canvas
this.ctx = this.canvasMap.ctx this.ctx = this.canvasMap.ctx
// define init position of the item
this.pos.x = this.ray/2 + Math.random()*(this.canvas.width - this.ray) this.pos.x = this.ray/2 + Math.random()*(this.canvas.width - this.ray)
this.pos.y = this.ray/2 + Math.random()*(this.canvas.height - this.ray) this.pos.y = this.ray/2 + Math.random()*(this.canvas.height - this.ray)
// listen for animate event dispatched from parent
this.canvas.addEventListener('animate', this.animate) // MATTER
// create the matter body and add it to the engine
if (!this.body) {
console.log('concernementItem creating body');
this.body = Bodies.circle(this.pos.x, this.pos.y, this.ray, {
frictionAir: 0,
mass: this.entites.length,
restitution: 0.9
});
// Body.setPosition(this.body, this.pos);
Body.setVelocity(this.body, {
x: -50 + Math.random()*50,
y: -50 + Math.random()*50
});
// console.log('concernementItem mass', this.body.mass);
Composite.add(this.canvasMap.matter.engine.world, this.body);
}
// // listen for animate event dispatched from parent
// this.canvas.addEventListener('animate', this.animate)
// listen for afterUpdate event from Matter.Engine object
Events.on(this.canvasMap.matter.engine, "afterUpdate", this.animate)
}, },
parsePoints (){ parsePoints (){
// converts data (menace/maintien, actuel/future, prise) into atcual position x,y // converts data (menace/maintien, actuel/future, prise) into atcual position x,y
@ -120,11 +159,11 @@ export default {
} }
console.log(`this.salientPoints ${this.concernement.id}`, this.salientPoints); console.log(`this.salientPoints ${this.concernement.id}`, this.salientPoints);
}, },
animate () { animate (event) {
if (this.ctx) { if (this.ctx) {
// this var is only here to trigger the render() // this var is only here to trigger the render()
this.time = Date.now(); this.time = Date.now();
// this.pos.x += 0; this.pos = this.body.position;
} }
} }
}, },

View File

@ -11,7 +11,7 @@ export default {
}, },
mounted() { mounted() {
// this.initGradients() // this.initGradients()
this.initTrame() // this.initTrame()
}, },
// computed: { // computed: {
// }, // },

View File

@ -1,40 +1,97 @@
<script> <script>
import Granim from 'granim'
// import { mapActions, mapState } from 'pinia' // import { mapActions, mapState } from 'pinia'
import { computed } from 'vue' import { computed } from 'vue'
// import LoginBlock from '@components/block/LoginBlock.vue'
import MapBackground from '@components/MapBackground.vue' import MapBackground from '@components/MapBackground.vue'
// import MA from '/api/ma-axios' // https://brm.io/matter-js/docs/classes/Engine.html
import {
Engine,
// Render,
// World,
Bodies,
// Body,
// Events,
Composite,
// Composites,
// Constraint,
// Vertices,
// Mouse,
// MouseConstraint,
// Query,
// Common
} from "matter-js";
// https://www.digitalocean.com/community/tutorials/vuejs-vue-html5-canvas
export default { export default {
data() { data() {
return { return {
canvasMap: { canvasMap: {
canvas: null, canvas: null,
ctx: null ctx: null,
matter: null
}, },
animateEvent: new Event('animate'), animateEvent: new Event('animate'),
granim: null granim: null,
// MATTER
engine: null,
world: null,
render: null
} }
}, },
provide() { provide() {
// https://www.digitalocean.com/community/tutorials/vuejs-vue-html5-canvas
return { return {
// explicitly provide a computed property // explicitly provide a computed property
canvasMap: computed(() => this.canvasMap) canvasMap: computed(() => this.canvasMap)
} }
}, },
created() {
// MATTER
// create an engine
let engineOptions = {
enableSleeping: true,
timing: {
//timestamp: 0.5,
timeScale: 0.5
}
}
this.engine = Engine.create(engineOptions);
this.engine.gravity.scale = 0;
this.canvasMap.matter = {
engine: this.engine
}
this.world = this.engine.world;
},
mounted() { mounted() {
this.canvasMap.canvas = this.$refs['canvas-map']; this.canvasMap.canvas = this.$refs['canvas-map'];
this.canvasMap.ctx = this.canvasMap.canvas.getContext('2d'); this.canvasMap.ctx = this.canvasMap.canvas.getContext('2d');
this.canvasMap.canvas.width = this.canvasMap.canvas.parentElement.clientWidth; let canvas_w = this.canvasMap.canvas.width = this.canvasMap.canvas.parentElement.clientWidth;
this.canvasMap.canvas.height = this.canvasMap.canvas.parentElement.clientHeight; let canvas_h = this.canvasMap.canvas.height = this.canvasMap.canvas.parentElement.clientHeight;
// MATTER
let wall_w = 5;
Composite.add(this.world, [
// walls
Bodies.rectangle(canvas_w/2, 0, canvas_w, wall_w, { isStatic: true }), // top
Bodies.rectangle(canvas_w/2, canvas_h-wall_w, canvas_w, wall_w, { isStatic: true }), // bottom
Bodies.rectangle(0, canvas_h/2, wall_w, canvas_h, { isStatic: true }), // left
Bodies.rectangle(canvas_w-wall_w, canvas_h/2, wall_w, canvas_h, { isStatic: true }) // right
]);
// this.render = Render.create({
// canvas: this.$refs['canvas-engine'],
// engine: this.engine,
// options: {
// width: canvas_w,
// height: canvas_h,
// showVelocity: true
// }
// });
// Render.run(this.render);
this.animate() this.animate()
}, },
// computed: { // computed: {
@ -44,7 +101,8 @@ export default {
methods: { methods: {
animate () { animate () {
this.canvasMap.ctx.clearRect(0, 0, this.canvasMap.canvas.width, this.canvasMap.canvas.height) this.canvasMap.ctx.clearRect(0, 0, this.canvasMap.canvas.width, this.canvasMap.canvas.height)
this.canvasMap.canvas.dispatchEvent(this.animateEvent) // this.canvasMap.canvas.dispatchEvent(this.animateEvent)
Engine.update(this.canvasMap.matter.engine, 1)
window.requestAnimationFrame(this.animate); window.requestAnimationFrame(this.animate);
} }
}, },
@ -61,6 +119,9 @@ export default {
<div id="map-backgrounds"> <div id="map-backgrounds">
<MapBackground /> <MapBackground />
</div> </div>
<div id="map-matter">
<canvas ref="canvas-engine"></canvas>
</div>
<div id="map-concernements"> <div id="map-concernements">
<canvas ref="canvas-map"></canvas> <canvas ref="canvas-map"></canvas>
<slot></slot> <slot></slot>