Jump to content

Trying to get octrees going rather than loop through all the things!


agmcleod
 Share

Recommended Posts

Hello,

 

so in a render loop, I am looping through all the objects that a laser can collide with, and perform an intersects mesh. This is only like 70ish objects total, so it's not entirely awful, but on my iPad mini i do notice some frame rate drops occasionally, so i'd like to improve it with octrees. Here's the code for the looping. This gets called in my render loop, after updating positions:

GameScene.prototype.update = function (endScene) {  this.player.update();  var enemies = this.wave.enemies;  for (var i = enemies.length - 1; i >= 0; i--) {    var enemy = enemies[i];    endScene |= enemy.update(this.player);    for (var l = this.lasers.length - 1; l >= 0; l--) {      var laser = this.lasers[l];      if (enemy.mesh.intersectsMesh(laser.mesh, false)) {        this.wave.removeEnemy(enemy);        this.removeLaser(laser);      }    }  }  for (var i = this.lasers.length - 1; i >= 0; i--) {    var laser = this.lasers[i];    laser.update();    for (var c = this.cubes.length - 1; c >= 0; c--) {      var cube = this.cubes[c];      if (cube.intersectsMesh(laser.mesh, false)) {        this.removeLaser(laser);      }    }    for (var w = this.walls.length - 1; w >= 0; w--) {      var wall = this.walls[w];      if (wall.intersectsMesh(laser.mesh, false)) {        this.removeLaser(laser);      }    }  }

So yeah it's kinda gross. Using an octree instead, here's what i have:

GameScene.prototype.update = function (endScene) {  this.player.update();  var enemies = this.wave.enemies;  for (var i = enemies.length - 1; i >= 0; i--) {    var enemy = enemies[i];  }  for (var i = this.lasers.length - 1; i >= 0; i--) {    var laser = this.lasers[i];    laser.update();  }  var octree = this.scene.createOrUpdateSelectionOctree();  for (var i = this.lasers.length - 1; i >= 0; i--) {    var laser = this.lasers[i];    var meshes = octree.intersects(laser.mesh.position, 1, false);    for (var i = meshes.length - 1; i >= 0; i--) {      var mesh = meshes.data[i];      if (mesh.name !== "ground" && mesh.name !== "player" && laser.mesh.intersectsMesh(mesh, false)) {        console.log(mesh.position, laser.mesh.position);        this.removeLaser(laser);        if (mesh.name === "enemy") {          this.wave.removeEnemy(mesh.refObject);        }      }    }  }  return endScene;}

The problem is, the data seems to return incorrect data. The laser intersects one of the cube objects, even though there should be none in range. Looking at the log, i see the mesh.position and laser.mesh.position have the same values. There something i missed here?

Link to comment
Share on other sites

Hey delta, sorry just saw the reply. Here's  a published version with the debug build: http://projects.agmprojects.com/wrath-test/.

 

The update code i referred to use near the bottom of game_screen.js. After you click "Tap to start" and the countdown, use the mouse left button to shoot. It still spawns, but the mesh position and laser position equal the same value (in console log).

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