Cannon.js是否可以检查一个对象是否与其他两个对象同时发生碰撞?

时间:2020-10-05 13:46:26

标签: javascript spark-ar-studio cannon.js

我正在使用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);
    }
});

0 个答案:

没有答案
相关问题