Search the Community
Showing results for tags 'intersectmesh'.
-
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?
- 1 reply
-
- frustrated
- high
-
(and 3 more)
Tagged with:
-
Good afternoon, I'm trying to check intersection between 2 meshes. For that I created 2 bounding boxes. When the intersection happens, the NPC should pursuit the ship. Although, it's not working, for some reason, the NPC is behaving like it had intersedted all of the meshes in the screen at the same time (like if the bounding box had infinite size. there's part of the code. activate is called on the main scope, The example his checking the intersection between the this.boundingbox and the playermesh, but i already tried with the boundingboxship (of the player), and tried with "false" also, it doesn't work, with false doesn't detect anything at all. I also commented out all the collision stuff, including the ellipsoids, still nothing. Thanks for the help in advance. patroller.prototype.activate = function(){ var a = this.units.length - 1; console.log(this.arrived); if (this.arrived === true){ var tempx = Math.floor(Math.random()*(this.mapSize-1+1)+1); var tempz = Math.floor(Math.random()*(this.mapSize-1+1)+1); this.targetX = tempx; this.targetZ = tempz; this.arrived = false; } //estado patrol if ((this.arrived === false) && (typeof(units[a].getMesh())!=="undefined")&&(typeof(this.mesh)!=="undefined")){ //this.npcPosX = temp.getPositionX(); //this.npcPosZ = temp.getPositionZ(); this.move(this.targetX,this.targetZ); //var meshtemp = units[0]; //console.log(units[0].getMesh()); //console.log(meshtemp.mesh); for (var i=0; i<this.units.length; i++){ if (typeof(this.units[i].getMesh())!=="undefined" ){ //console.log(units[i].getMesh()); //console.log(this.mesh); //console.log(meshtemp); //console.log(this.boundingBox); //console.log(units[i].boundingBoxShip); if (this.boundingBox.intersectsMesh(this.units[i].getMesh()),true){ console.log("funciona"); this.attackedUnit = this.units[i]; //estado de perseguição this.move(this.units[i].getShipPosx(),this.units[i].getShipPosz()); var targetx = units[i].getShipPosx(); var targetz = units[i].getShipPosz(); var posix = this.getPositionX(); var posiz = this.getPositionZ(); var dx = targetx - posix; var dz = targetz - posiz; var vector = Math.sqrt(dx * dx + dz * dz); if (vector<=25){ //estado de ataque //attack (); } } if (this.armor<5){ //estado de fuga this.move(Math.floor(Math.random()*(this.mapSize-1+1)+1),Math.floor(Math.random()*(this.mapSize-1+1)+1)); } } }