Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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 碰撞检测3.js的问题_Javascript_Three.js_Collision_Detection - Fatal编程技术网

Javascript 碰撞检测3.js的问题

Javascript 碰撞检测3.js的问题,javascript,three.js,collision,detection,Javascript,Three.js,Collision,Detection,我想用three.js制作一个3D突破游戏。我对墙壁进行了大部分碰撞检测,但我似乎无法获得与桨叶的碰撞 这是我的密码: //three.Breakout, a 3D breakout game by Samuel Steele, Cryptocosm Play(); function Play() { //Declare our scene and camera, easy start up stuff like golabal vars var widt

我想用three.js制作一个3D突破游戏。我对墙壁进行了大部分碰撞检测,但我似乎无法获得与桨叶的碰撞

这是我的密码:

    //three.Breakout, a 3D breakout game by Samuel Steele, Cryptocosm
    Play();
    function Play() {
    //Declare our scene and camera, easy start up stuff like golabal vars
    var width = window.innerWidth;
    var height = window.innerHeight;
    var velocityX = -1, velocityZ = -2;
    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera( 75, width / height, .1, 1000 );

    //initiate the WebGL Context
    var renderer = new THREE.WebGLRenderer();
    renderer.setSize(width, height);
    document.body.appendChild(renderer.domElement);

    //create a light
    var light = new THREE.PointLight( 0xecf0f1, 1.3, 600 );
    light.position.set( 0, 0, 32 );
    //add the light to the scene
    scene.add(light);

    //make the paddle
    var paddleGeometry = new THREE.CubeGeometry(30, 8, 3);
    var paddleMaterial = new THREE.MeshLambertMaterial({
        color: 0xF7F7F7, 
        shading: THREE.FlatShading
    });

    var paddle = new THREE.Mesh(paddleGeometry, paddleMaterial);

    var lBarGeometry = new THREE.CubeGeometry(10, 10, 100);
    var lBarMaterial = new THREE.MeshLambertMaterial({
        color: 0xbdc3c7,
        shading: THREE.FlatShading
    });
    var leftBar = new THREE.Mesh(lBarGeometry, lBarMaterial);

    var rBarGeometry = new THREE.CubeGeometry(10, 10, 100);
    var rBarMaterial = new THREE.MeshLambertMaterial({
        color: 0xbdc3c7,
        shading: THREE.FlatShading
    });
    var rightBar = new THREE.Mesh(rBarGeometry, rBarMaterial);

    var backGeometry = new THREE.CubeGeometry(130, 10, 10);
    var backMaterial = new THREE.MeshLambertMaterial({
        color: 0xbdc3c7,
        shading: THREE.FlatShading
    });
    var back = new THREE.Mesh(backGeometry, backMaterial);


    var ballGeometry = new THREE.CubeGeometry(7, 7, 7);
    var ballMaterial = new THREE.MeshLambertMaterial({
        color: 0xbdc3c7,
        shading: THREE.FlatShading,
        wireframe: false
    });
    var ball = new THREE.Mesh(ballGeometry, ballMaterial);

    //add our objects to the scene
    scene.add(paddle);
    scene.add(leftBar);
    scene.add(rightBar);
    scene.add(back);
    scene.add(ball);

    //position errythang
        //the camera
    camera.position.z = 50;
        //the paddle
    paddle.position.y = -27; 
    paddle.position.z = 5;
        //the left sideBar
    leftBar.position.x = -60;
    leftBar.position.y = -27;
    leftBar.position.z = -40;
        //the right sideBar
    rightBar.position.x = 60;
    rightBar.position.y = -27;
    rightBar.position.z = -40;
        //the back bar
    back.position.z = -90;
    back.position.y = -27;
        //the ball
    ball.position.y = -27;


    var render = function () {
        //if the ball hits the side bars
    if(ball.position.x > 50 || ball.position.x < -50) {
            velocityX = -velocityX;
    }; 
        //if the ball hits the back wall
    if(ball.position.z < -80) {
        velocityZ = -velocityZ;
    };



        // move ball
        ball.position.x += velocityX;
        ball.position.z += velocityZ;
        requestAnimationFrame(render);

        //ball.rotation.y += Math.floor(Math.random()*100);

        renderer.render(scene, camera);

    };

    document.addEventListener("keydown", onDocumentKeyDown, false);

    function onDocumentKeyDown(event){
        // Get the key code of the pressed key
        var keyCode = event.which;
        switch(keyCode) {
            //Left key pressed
                case 37: paddle.position.x -= 20, console.log(paddle.position.x);
            break;
            //Right key pressed
                case 39: paddle.position.x += 20, console.log(paddle.position.x);
            break;
            //If anything else is pressed
                default: console.error('OH LAWD!');
            break;
        }

        }
    render();
    }
//three.Breakout,Samuel Steele的3D Breakout游戏,Cryptocosm
Play();
函数播放(){
//宣布我们的场景和摄像机,像golabal vars这样容易启动的东西
变量宽度=window.innerWidth;
var height=window.innerHeight;
变量velocityX=-1,velocityZ=-2;
var scene=new THREE.scene();
var摄像机=新的三透视摄像机(75,宽/高,.11000);
//启动WebGL上下文
var renderer=new THREE.WebGLRenderer();
设置大小(宽度、高度);
document.body.appendChild(renderer.doElement);
//创建一个灯光
var灯=新的三点灯(0xecf0f1,1.3600);
光。位置。设置(0,0,32);
//将灯光添加到场景中
场景。添加(灯光);
//划桨
var PapperGeometry=新的三立方测量法(30,8,3);
var桨叶材质=新的3.0网格LambertMaterial({
颜色:0xF7F7,
明暗处理:3.5倍明暗处理
});
var桨叶=新的三个网格(桨叶几何体、桨叶材质);
var lBarGeometry=新的三次容积法(10,10,100);
var lBarMaterial=新的3.0网格LambertMaterial({
颜色:0xbdc3c7,
明暗处理:3.5倍明暗处理
});
var leftBar=新的三网格(LBArgenometry,lBarMaterial);
var rBarGeometry=新的三立方测量法(10,10,100);
var rBarMaterial=新的3.0网格LambertMaterial({
颜色:0xbdc3c7,
明暗处理:3.5倍明暗处理
});
var rightBar=新的三网格(RBargometry,RBatarial);
var backGeometry=新的三立方测量法(130,10,10);
var backMaterial=新的3.0网格lambertMaterial({
颜色:0xbdc3c7,
明暗处理:3.5倍明暗处理
});
var back=新的三点网格(背几何体、背材质);
var ballGeometry=新的三立方测量法(7,7,7);
var ballMaterial=新的三点网格LambertMaterial({
颜色:0xbdc3c7,
明暗处理:3.5倍明暗处理,
线框:错误
});
var ball=新的三点网格(ballGeometry,ballMaterial);
//将对象添加到场景中
场景。添加(划桨);
添加(左栏);
场景.添加(右栏);
场景。添加(返回);
场景。添加(球);
//二里塘位置
//摄像机
摄像机位置z=50;
//桨
桨位置y=-27;
桨叶位置z=5;
//左侧边栏
leftBar.position.x=-60;
leftBar.position.y=-27;
leftBar.position.z=-40;
//右侧边栏
右杆位置x=60;
位置y=-27;
位置z=-40;
//后杠
back.position.z=-90;
back.position.y=-27;
//球
球的位置y=-27;
var render=函数(){
//如果球打到边栏上
如果(球位置x>50 | |球位置x<-50){
velocityX=-velocityX;
}; 
//如果球碰到后墙
如果(球位置z<-80){
velocityZ=-velocityZ;
};
//移动球
球位置x+=速度x;
球位置z+=速度z;
请求动画帧(渲染);
//ball.rotation.y+=Math.floor(Math.random()*100);
渲染器。渲染(场景、摄影机);
};
文件。添加的文件列表器(“keydown”,onDocumentKeyDown,false);
函数onDocumentKeyDown(事件){
//获取按下的键的键代码
var keyCode=event.which;
开关(钥匙代码){
//按左键
案例37:桨位置x-=20,控制台日志(桨位置x);
打破
//按右键
案例39:桨位置x+=20,控制台日志(桨位置x);
打破
//如果还有别的事
默认值:console.error('OH LAWD!');
打破
}
}
render();
}

这段代码的一个活生生的例子是,我正在使用Three.js r62。使用这个框架很容易:,以实现物理。

您尝试了什么?我看不到检测与划桨碰撞的代码。你可以使用[PhysiJs][1]。我知道我没有真正回答你的问题,但它是一个很酷的3.js物理引擎,运行在一个单独的webworker中。你可以用它来处理所有的碰撞。[1] :我试图让它检测球是否在桨的宽度和z轴上的某个点之间。。有些类似于,如果(pabble.position.x-30=5){velocityZ=-velocityZ};但它不起作用。