three.js - THREE.raycaster : Possible to use it for enemy Ai? -


i've started learning 3 , have been messing three.js example of controllable md2 characters try , fashion 3rd person shooter kind of game. i've been trying write simple algorithm enemy characters , i'm pretty sure ray-casting ideal.the whole idea enemies should stop rotating once they're facing player. here's problem that's giving me sleepless nights! :

let's say, enemy object origin ray caster ray. no matter direction set direction of ray ( even, example (1,0,0) - positive x-axis), ray's direction pointing towards center of scene!!!

please help! haven't been able find example online kind of use ray caster (apart collision detection don't need @ moment).

if want enemies stop rotating when looking @ player, consider checking direction between them, it's lot faster casting ray see if intersects:

// assuming `enemy` three.mesh var targetdir = enemy.position.clone().sub(player.position).normalize(); var currentdir = (new three.vector3()).applymatrix4(enemy.matrixworld).sub(enemy.position).normalize(); var amounttorotate = currentdir.sub(targetdir); var offset = amounttorotate.length(); 

then rotate each axis no more value axis in amounttorotate if offset greater threshold.

that said, here how use raycaster, given variables above:

var raycaster = new three.raycaster(enemy.position, targetdir); var intersections = raycaster.intersectobject(player); 

note if running of above code in animation loop, create lot of garbage collection churn because creating bunch of new objects , throwing them away. better pattern, used lot in library itself, initialize objects once, copy values them if need to, , use copies computation. example, create function raycasting this:

var isenemylookingatplayer = (function() {   var raycaster = new three.raycaster();   var pos = new three.vector3();   return function(enemy) {     raycaster.ray.origin.copy(enemy.position);     raycaster.ray.direction.copy(pos.copy(enemy.position).sub(player.position).normalize());     return !!raycaster.intersectobject(player).length;   }; })(); 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -