Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.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 物理冒名顶替者之后的巴比伦js旋转_Javascript_Babylonjs - Fatal编程技术网

Javascript 物理冒名顶替者之后的巴比伦js旋转

Javascript 物理冒名顶替者之后的巴比伦js旋转,javascript,babylonjs,Javascript,Babylonjs,好吧,在HTML5API和大约两天前偶然发现巴比伦JSfor3D在HTML5上使用webgl;但问题是,这是一项新技术,并没有做太多的工作,也没有太多的视频教程。因此,在使用这项技术的三天里,我已经能够尝试一下物理引擎 我想在添加物理状态后旋转方框2,但我做不到。我只能在添加物理状态之前旋转对象 我知道在现代游戏中,即使是空中的物体也可以旋转,但由于重力的作用会掉落。对此我能做些什么,我必须移除物理属性,旋转对象并重新旋转对象。下面是我的代码: <!DOCTYPE html> <

好吧,在HTML5API和大约两天前偶然发现巴比伦JSfor3D在HTML5上使用webgl;但问题是,这是一项新技术,并没有做太多的工作,也没有太多的视频教程。因此,在使用这项技术的三天里,我已经能够尝试一下物理引擎

我想在添加物理状态后旋转方框2,但我做不到。我只能在添加物理状态之前旋转对象

我知道在现代游戏中,即使是空中的物体也可以旋转,但由于重力的作用会掉落。对此我能做些什么,我必须移除物理属性,旋转对象并重新旋转对象。下面是我的代码:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        html, body {
            overflow: hidden;
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }

        #renderCanvas {
            width: 100%;
            height: 100%;
            touch-action: none;
        }
    </style>
    <script src="../js/babylon.2.0.js"></script>
    <script src="../js/hand-1.3.8.js"></script>
    <script src="../js/cannon.js"></script>  <!-- optional physics engine -->
    <!-- <script src="../js/Oimo.js"></script>  New physics engine -->
</head>
<body>
<canvas id="renderCanvas"></canvas>
<script type="text/javascript">
    // Get the canvas element from our HTML below
    var canvas = document.querySelector("#renderCanvas");

    // Load the BABYLON 3D engine
    var MyScene = function(){
        console.log("MyScene Activated: Trying to test Object Oriented Javascript With Babylon js")
    };
    MyScene.prototype.engine = new BABYLON.Engine(canvas, true);
    MyScene.prototype.createScene = function(){
        // Now create a basic Babylon Scene object
        var scene = new BABYLON.Scene(this.engine);

        scene.enablePhysics(null,new BABYLON.CannonJSPlugin());
        scene.setGravity(new BABYLON.Vector3(0,-10,0));

        // Change the scene background color to green.
        scene.clearColor = new BABYLON.Color3(200, 1, 0.3);
        var camera = new BABYLON.ArcRotateCamera("camera",1,1.4,53,new BABYLON.Vector3(0,0,0),scene);

        // This attaches the camera to the canvas
        camera.attachControl(canvas, false);

        // This creates a light, aiming 0,1,0 - to the sky.
        var light = new BABYLON.PointLight("light1", new BABYLON.Vector3(0, 0, 10), scene);

        // Dim the light a small amount
        light.intensity = .5;

        MyScene.prototype.ball = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene);
        MyScene.prototype.ball.position.y = 10;
        MyScene.prototype.ball.setPhysicsState({ impostor : BABYLON.PhysicsEngine.BoxImpostor, friction : 0.5 , mass: 10, restitution : 0.7});

        MyScene.prototype.box1 = BABYLON.Mesh.CreateBox("box1",3,scene);
        MyScene.prototype.box1.position.x = 3;
        MyScene.prototype.box1.scaling.x = 1;
        MyScene.prototype.box1.position.y = -6;
        MyScene.prototype.box1.setPhysicsState({ impostor : BABYLON.PhysicsEngine.BoxImpostor, friction : 0.5 , mass: 10, restitution : 0.7});

        MyScene.prototype.box2 = BABYLON.Mesh.CreateBox("box2",3,scene);
        MyScene.prototype.box2.position.x = 6;
        MyScene.prototype.box2.position.y = 10;


        MyScene.prototype.box2.setPhysicsState({ impostor : BABYLON.PhysicsEngine.BoxImpostor, friction : 0.5 , mass: 10, restitution : 0.7});



        MyScene.prototype.box3 = BABYLON.Mesh.CreateBox("box3",3,scene);
        MyScene.prototype.box3.position.x = 1;
        MyScene.prototype.box3.setPhysicsState({ impostor : BABYLON.PhysicsEngine.BoxImpostor, friction : 0.5 , mass: 10, restitution : 0.7});


        MyScene.prototype.ground = BABYLON.Mesh.CreateBox("box",50,scene);
        MyScene.prototype.ground.position.y = -10;
        MyScene.prototype.ground.scaling.y = 0.1;
        MyScene.prototype.ground.setPhysicsState({ impostor : BABYLON.PhysicsEngine.BoxImpostor, friction : 0.5 , mass: 0, restitution : 0.7});

        // Leave this function
        return scene;
    };
    MyScene.prototype.getEngine = function(){
        return this.engine;
    };
    MyScene.prototype.scale = function(){
        counter = 0;
       while(counter <= 10){

           MyScene.prototype.box2.rotate(BABYLON.Axis.X,counter,BABYLON.Space.LOCAL);
           MyScene.prototype.box2.rotate(BABYLON.Axis.Y,counter,BABYLON.Space.LOCAL);
           MyScene.prototype.box2.rotate(BABYLON.Axis.Z,counter,BABYLON.Space.LOCAL);
           ++counter;
       }

    }
    MyScene.prototype.pos = function(){
        this.box1.position.x += 0.2;
        this.box1.scaling.x += 0.2;
        this.box2.position.x += 0.2;
        this.box2.scaling.x += 0.2;
        this.box3.position.x += 0.2;
        this.box3.scaling.x += 0.2;


    };
    var Myscene = new MyScene();




    var scene = Myscene.createScene();

    // Register a render loop to repeatedly render the scene
    Myscene.getEngine().runRenderLoop(function () {

        scene.render();
        Myscene.scale();

    });

    // Watch for browser/canvas resize events
    window.addEventListener("resize", function () {
        Myscene.getEngine().resize();

    });
</script>

</body>
</html>

当网格受物理引擎控制时,在应用旋转更改后必须使用以下特定代码:mesh.updatephysicsbodposition


此功能将保持物理模拟的更新

,但也可以添加到该功能中,是否可以从blender加载完整的场景,然后使用babylon js控制场景中的每个材质。例如,我有一个包含三栋房子、一条道路、两辆车和五家商店的场景。我希望能够在路上移动汽车,当汽车撞到有玻璃门的商店时,门应该会被打破