我使用 matter.js 进行物理处理,使用 p5.js 进行渲染。当您在画布中单击时,我试图简单地创建框,然后当框到达画布的末端时,它会与地面发生碰撞,但地面不会检测到与框的碰撞。我假设问题是地面定位正确。
这是代码
// module aliases
var Engine = Matter.Engine,
World = Matter.World,
Bodies = Matter.Bodies;
var x = 0;
var engine;
let frame = document.getElementById('frames');
var ground;
var boxs = [];
function setup()
{
//put setup code here
createCanvas(640,480);
engine = Engine.create();
// run the engine
Engine.run(engine);
ground = Bodies.rectangle(0, height/2, width, 10);
World.add(engine.world, ground);
}
function mousePressed()
{
boxs.push(new Box(mouseX, mouseY, 20, 20) );
}
function draw()
{
x = x + 1;
background(0);
for (var i = 0; i < boxs.length; i++) {
boxs[i].show();
}
}
var Box = function(x, y , width, height){
this.box = Bodies.rectangle(x, y, width, height);
this.h = height;
this.w = width;
World.add(engine.world, this.box);
this.show = function (){
push();
fill(255);
let pos = this.box.position;
let angle = this.box.angle;
translate(pos.x, pos.y);
rotate(angle);
rect(0, 0, this.w, this.h);
rectMode(CENTER);
pop();
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.12.0/matter.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.0/p5.js"></script>
<html>
<head>
<title>Ball Clash</title>
</head>
<body>
<p id="frames"></p>
<!-- <canvas id="myCanvas" height="480" width="640" style="border:1px solid black;"></canvas> -->
</body>
</html>