Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Cannon.js是否有办法检查一个对象是否同时与其他两个对象发生碰撞?_Javascript_Spark Ar Studio_Cannon.js - Fatal编程技术网

Javascript Cannon.js是否有办法检查一个对象是否同时与其他两个对象发生碰撞?

Javascript Cannon.js是否有办法检查一个对象是否同时与其他两个对象发生碰撞?,javascript,spark-ar-studio,cannon.js,Javascript,Spark Ar Studio,Cannon.js,我正在使用Cannon.js制作一个游戏,其中一个球从你的头部反弹,屏幕的两侧以方框作为边界。当球与球头相撞时,得分会增加一分我想检查球是否同时与一面墙和头部碰撞,因为当发生这种情况时,我不想让它在分数上增加一分。有人知道如何为此创建支票吗 我的代码: const UIManager = require("./UIManager.js"); var playing = false; // Create cannon world and setting gravity co

我正在使用Cannon.js制作一个游戏,其中一个球从你的头部反弹,屏幕的两侧以方框作为边界。当球与球头相撞时,得分会增加一分我想检查球是否同时与一面墙和头部碰撞,因为当发生这种情况时,我不想让它在分数上增加一分。有人知道如何为此创建支票吗

我的代码:

const UIManager = require("./UIManager.js");

var playing = false;

// Create cannon world and setting gravity
const world = new CANNON.World();
world.gravity.set(0, -0.52, 0);

// Create sphere body and setting its shape and properties
var mat1 = new CANNON.Material();

// Radius for the ball object
const Radius = 0.05;

// Radius for the head hitbox
const Radius2 = 0.08;

const BallProp = {
    mass: 2,
    position: new CANNON.Vec3(0, 1.1, 0),
    radius: Radius,
    material: mat1,
    shape: new CANNON.Sphere(Radius),
}

// Add the ball to the physics world
const BallBody = new CANNON.Body(BallProp);
world.addBody(BallBody);

// Create a material for the head hitbox
var HeadMaterial = new CANNON.Material();

// Create ground body and settings its shape and properties
const HeadProp = {
    mass: 0,
    position: new CANNON.Vec3(0, 0, 0),
    radius: Radius2,
    material: HeadMaterial,
    shape: new CANNON.Sphere(Radius2),
}
const HeadBody = new CANNON.Body(HeadProp);

// Rotate the ground so it is flat (facing upwards)
const angle = -Math.PI / 2;
const xAxis = new CANNON.Vec3(1, 0, 0);
HeadBody.quaternion.setFromAxisAngle(xAxis, angle);

// Add the hitbox to the physics world
world.addBody(HeadBody);

// Create a new material for the walls
var WallMaterial = new CANNON.Material();

// Create walls and settings its shape and properties
const WallProp1 = {
    mass: 0,
    position: new CANNON.Vec3(-1.19, 0.6, 0),
    material: WallMaterial,
    shape: new CANNON.Box(new CANNON.Vec3(1, 1, 1)),
}

const WallProp2 = {
    mass: 0,
    position: new CANNON.Vec3(1.19, 0.6, 0),
    material: WallMaterial,
    shape: new CANNON.Box(new CANNON.Vec3(1, 1, 1)),
}

const Wall1Body = new CANNON.Body(WallProp1);
const Wall2Body = new CANNON.Body(WallProp2);

// Add the walls to the physics world
world.addBody(Wall1Body);
world.addBody(Wall2Body);

// Create a death plane and settings its shape and properties
const deathProps = {
    mass: 0,
    position: new CANNON.Vec3(0, -0.35, 0),
    shape: new CANNON.Plane(),
}
const DeathBody = new CANNON.Body(deathProps);

// Set the rotation correctly
DeathBody.quaternion.setFromAxisAngle(xAxis, angle);

// Add the deathplane to the physics world
world.addBody(DeathBody);

// Add new settings to the materials
var mat1_ground = new CANNON.ContactMaterial(HeadMaterial, mat1, { friction: 0.0, restitution: 1});
var mat1_wall = new CANNON.ContactMaterial(WallMaterial, mat1, { friction: 0.0, restitution: 0.65});
world.addContactMaterial(mat1_ground);
world.addContactMaterial(mat1_wall);

// Configure time step for Cannon
const fixedTimeStep = 1.0 / 60.0;
const maxSubSteps = 3;
const timeInterval = 30;
let lastTime;

var score = 0;

//checks for collision with the head hitbox
HeadBody.addEventListener("collide",function(e){
    if(playing)
    {
        score++;
        Diagnostics.log("Bounced amount / Score = " + score);
    }
    else
    {
        BallBody.velocity.set(0,0,0);
        BallBody.position.set(0,1,0);
    }
});

//checks for collision with the death plane
DeathBody.addEventListener("collide",function(e){
    if(playing)
    {
        EndGame(score);
    }
    else
    {
        BallBody.velocity.set(0,0,0);
        BallBody.position.set(0,1,0);
    }
});