Jump to content

Emissive color change before collision


Zahir Junejo
 Share

Recommended Posts

        var canvas = document.getElementById("renderCanvas");
        var engine = new BABYLON.Engine(canvas, true);
        var initBallSpeed = 0.05;
        var ballSpeed = initBallSpeed;
        var xDirection = 1;
        var yDirection = 1;
        var boardX = -3.0;
        var boardY = 3.0; 
        var keyState = {};

        var MainScene = function () {
        
            // This creates a basic Babylon Scene object (non-mesh)
            var scene = new BABYLON.Scene(engine);
                    
            // This creates and positions a free camera (non-mesh)
            var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 0, -20), scene);
            //camera.attachControl(canvas, true);
            var light = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
            
            //Setup the bat for the scene. Dont know what else to call it.
            var bat = BABYLON.Mesh.CreateBox("bat", 0.5, scene);
            bat.scaling.x = 5.0;
            bat.position.y = -5.0;


            //Setup the ball
            var ball = BABYLON.Mesh.CreateSphere("ball", 5, 0.5,scene);
            ball.position.y = bat.position.y + 1;
            ball.position.x = bat.position.x;

            //Register keypress actions with the ball
            scene.actionManager = new BABYLON.ActionManager(scene);

            window.addEventListener('keydown',function(e){
                keyState[e.keyCode || e.which] = true;
            },true);

            window.addEventListener('keyup',function(e){
                keyState[e.keyCode || e.which] = false;
            },true);

      //       scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(
      //           BABYLON.ActionManager.OnEveryFrameTrigger,
                     //  function (evt) {
                
            // }));

            //Setup the walls
            // Top Wall
            var topWall = BABYLON.Mesh.CreateBox("topWall", 0.5, scene);
            topWall.position.y = 6.0;
            topWall.scaling.x = 25.0;

            // Left Wall
            var leftWall = BABYLON.Mesh.CreateBox("leftWall", 0.5, scene);
            leftWall.position.x = -6.0;
            leftWall.position.y = 1.0;
            leftWall.scaling.y = 25.0;

            // Right Wall
            var rightWall = BABYLON.Mesh.CreateBox("rightWall", 0.5, scene);
            rightWall.position.x = 6.0;
            rightWall.position.y = 1.0;
            rightWall.scaling.y = 25.0;

            var enemy = BABYLON.Mesh.CreateBox("enemy1", 1.0, scene);
            enemy.position.x = boardX;
            enemy.position.y = boardY;

            var matBB = new BABYLON.StandardMaterial("matBB", scene);
            matBB.emissiveColor = new BABYLON.Color3(1, 1, 1);

            enemy.material = matBB;


            scene.registerBeforeRender(function(){
               
                ball.position.x += ballSpeed * xDirection;
                ball.position.y += ballSpeed * yDirection;
                

                if(ball.position.y < -7){
                    ball.position.y = bat.position.y + 1;
                    ball.position.x = bat.position.x;
                    ballSpeed = initBallSpeed;
                    xDirection = yDirection = 1;
                }

                if(ball.intersectsMesh(rightWall, true)){
                       xDirection = -1;
                }

                 if(ball.intersectsMesh(leftWall, true)){    
                     xDirection = 1;
                }
                
                if(ball.intersectsMesh(topWall, true)){
                     yDirection = -1;
                }

                if(ball.intersectsMesh(bat, true)){
                     yDirection = 1;
                     if(ballSpeed <= 0.25)
                     ballSpeed+=0.01;
                }

                if(ball.intersectsMesh(enemy, true))
                {  
                   xDirection = -1;
                   yDirection = -1;
                   enemy.material.emissiveColor = new BABYLON.Color3(1, 0, 0);
                }


                 if (keyState[37] || keyState[65]){
                     bat.position.x-=ballSpeed*2;
                }    

                if (keyState[39] || keyState[68]){
                     bat.position.x+=ballSpeed*2;
                }


            });

            return scene;
        };
        
        var scene = MainScene();

        engine.runRenderLoop(function () {
            scene.render();
        });

        // Resize
        window.addEventListener("resize", function () {
            engine.resize();
        });
   
Either I am really high or the enemy block is turning red before my ball can even collide with the block.

 

Any ideas?

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...