Jump to content

Simple User Interaction Example


robinrowe
 Share

Recommended Posts

Here code. Press 1 or 2

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title>BABYLON - BASICS</title>    <!--Loading babylon engine -->    <script src="../babylon.js"></script>    <script src="../hand.js"></script>    <!-- CSS -->    <!-- ------ -->    <style>        html, body, div, canvas {            width: 100%;            height: 100%;            padding: 0;            margin: 0;            overflow: hidden;        }    </style></head><!-- BODY --><!-- ------ --><body>    <div id="rootDiv">        <!-- Main Canvas -->        <canvas id="renderCanvas"></canvas>    </div>    </body>    <!-- BABYLON SCRIPT -->    <!-- -------------- -->    <script type="text/javascript">        // Get the Canvas element from our HTML below        var canvas = document.getElementById("renderCanvas");        // Load BABYLON 3D engine and set the root directory        var engine = new BABYLON.Engine(canvas, true);        //Create a new scene with a camera (mandatory), a light (better) and a sphere (to see the origin)        var scene = new BABYLON.Scene(engine);        // Creating a camera looking to the zero point (0,0,0)        var camera = new BABYLON.ArcRotateCamera("Camera", 1, 0.8, 10, new BABYLON.Vector3(0, 0, 0), scene);        // Creating a omnidirectional light        var light0 = new BABYLON.PointLight("Omni", new BABYLON.Vector3(0, 0, 10), scene);        // Creating a sphere of size 1, at 0,0,0        var origin = BABYLON.Mesh.CreateSphere("origin", 10, 1.0, scene);// here code for move shere//--------------------------------------------------        window.addEventListener("keydown", function (evt) {        switch (evt.keyCode) {            case 49:  origin.position.x = origin.position.x+1; break;            case 50:  origin.position.x = origin.position.x-1; break;            default: break;        } });//--------------------------------------------------        // Attach the camera to the scene        scene.activeCamera.attachControl(canvas);        // Once the scene is loaded, just register a render loop to render it        engine.runRenderLoop(function () {            scene.render();        });    </script></html>
Link to comment
Share on other sites

A non-keyboard example. Tap/Click on the ground plane and dude will move to where you tap

<!DOCTYPE html>
<head>
    <title>RAVE Babylon</title>
    <script src="babylon.js"></script> 
    <script src="hand.js"></script>
    <script>
        var canvas, engine, scene, light0, origin, camera,
            dudemesh, targetx, targetz, rot;
    function start() {
        canvas = document.getElementById("cvRAVE");
        engine = new BABYLON.Engine(canvas, true);
        scene = new BABYLON.Scene(engine);
 
        camera = new BABYLON.FreeCamera("Camera", new BABYLON.Vector3(0, 50, -200), scene);
        light0 = new BABYLON.DirectionalLight("omni", new BABYLON.Vector3(1, 1, 0), scene);
        origin = BABYLON.Mesh.CreateSphere("origin", 10, 2.0, scene);
        ground = BABYLON.Mesh.CreateGround("ground", 10000, 1000, 1, scene);
        targetx = 0; targetz = 0; rot = 0;
        BABYLON.SceneLoader.ImportMesh("him", "Dude/", "dude.babylon", scene, function (newMeshes, particleSystems, skeletons) {
            dudemesh=newMeshes;
            newMeshes[0].position = new BABYLON.Vector3(0, 0, 0);  // The original dude
            scene.beginAnimation(skeletons[0], 0, 120, 1.0, true);
        });
       
        scene.activeCamera.attachControl(canvas);
        engine.runRenderLoop(function () {
            if (dudemesh) {
                dx = targetx - dudemesh[0].position.x;
                dz = targetz - dudemesh[0].position.z;
                rot = Math.atan2(dx, dz); // or dz,dx, but modify rotation
                len = Math.sqrt(dx * dx + dz * dz);
                if (len == 0) len = 1;
                dx /= len;
                dz /= len;
                dudemesh[0].rotation.y = rot+Math.PI; 
                dudemesh[0].position.x += dx;
                dudemesh[0].position.z += dz;
            }
            scene.render();
        });
    }
 
    function moveDude(event) {
        var pickResult = scene.pick(event.clientX, event.clientY);
        targetx = pickResult.pickedPoint.x;
        targetz = pickResult.pickedPoint.z;
    }
    </script>
    <style>
        html, body { From tutorial width:100%;
            height: 100%;
            padding: 0;
            margin: 0;
            overflow: hidden;
        }
        #cvRAVE {
            width:100%;height:100%;
        }
    </style>
</head>
<body onload="start()">
    <canvas onclick="moveDude(event)" id="cvRAVE" ></canvas>
</body>
</html>
 
post-5799-0-13645000-1387519333.png

Dude.zip

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...