0

好吧,一直在使用 html 5 api,大约两天前偶然发现了使用 webgl 在 html 5 上进行 3d 的巴比伦 js;但问题是它是一项新技术,并没有做太多的工作,也没有像预期的那样提供太多的视频教程。因此,在使用该技术三天后,我已经能够尝试使用物理引擎了。

我想在添加物理状态后旋转框 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>
4

1 回答 1

0

当网格受物理引擎控制时,您必须在应用旋转更改后使用此特定代码:mesh.updatePhysicsBodyPosition()

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

于 2015-02-25T23:47:09.387 回答