我创建了一个包含2个物体的复合物体,但是当这些物体掉落时,传感器会检测到每个物体,而不是传感器会检测20个物体,而这个传感器会检测40个物体,
创造复合体
for(let i = 0; i < total_moleculas; i++){
atomo_rojo = Bodies.circle(280,150, radio_atomo_rojo,{render:{fillStyle:'red',strokeStyle:'red'/*,sprite:{texture:'js/img2/Esfera_Grande.png'}*/}/*,id:"circulo_rojo_"+[i]*/});
atomo_blanco = Bodies.circle(((atomo_rojo.position.x) + 5),((atomo_rojo.position.y) + 15),radio_atomo_blanco,{render:{fillStyle:'white',strokeStyle:'black'/*,sprite:{texture:'js/img2/Esfera_2.png'*/}/*},id:"circulo_blanco_"+[i]*/});
moleculas_unidas = Body.create({
parts: [atomo_rojo,atomo_blanco],
restitution: 1,
friction: 0,
frictionStatic : 0,
frictionAir: 0,
inertia: Infinity,
mass: 1,
id: "molecula_unida_"+[i],
});
Body.setVelocity( moleculas_unidas, {x: velocity, y: -velocity});
moleculas_unidas.restitution = 0;
moleculas_unidas.friction = 0;
moleculas_unidas.frictionStatic = 0;
moleculas_unidas.frictionAir = 0;
moleculas_unidas.inertia = Infinity;
moleculas_unidas.mass = 1;
cambiar_color(valor_value);
array_moleculas_unidas.push(moleculas_unidas);
}
传感器
function sensor(){
var a=0;
var b=0;
var lectura = "";
Events.on(engine, 'collisionStart', function(event) {
var pairs = event.pairs;
for (let i = 0, j = pairs.length; i != j; ++i) {
var pair = pairs[i];
if (pair.bodyA === barra_superior_vaso_agua){
a=a+1;
pair.bodyB.render.strokeStyle = "green";
}
}
代码完整 https://github.com/trabucoaluca/chemistry/blob/master/x.js