mrdoob/three.js — Issue #960: Ray: how to find the direction
Author: lonelydatum
Created: 2011-12-24 01:32:38+00:00
State: open

Hello,

I've gone through the collision demos but I need help with the direction parameter when setting the Ray.
I noticed that all the demos use a static direction such as:
new THREE.Ray( sphere.position, new THREE.Vector3( 0, 0, 1 ) );

In my case, I have a character walking so this direction varies. In this demo I'm trying to calculate the distance:
http://meru.ca/story/webGL/proto/7/

Is there a way I can use the characters rotation Y to convert that to a direction? Below is sample of the code.

STORY.Interaction.prototype.moveLeadActor = function( v ){

var tweenMove = new TWEEN.Tween( this.leadActor.pos() ).to( { x:v.x, z:v.z }, 1000 ).start();
var me = this;
tweenMove.onUpdate( function(){
if( me.checkCollisions() ){
tweenMove.stop();
}else{
me.leadActor.walkCycle();
me.poparazi(); // camera follows character
}
} );
}

STORY.Interaction.prototype.checkCollisions = function( ){

var origin = this.leadActor.pos();
var dir = new THREE.Vector3( -1, 0, 0 ); ////////// HELP. how do I calculate this every time the character updates tween movement ///
var ray = new THREE.Ray( origin, dir );

var c = THREE.Collisions.rayCastNearest( ray );
if( c ){
if( c.distance < 600 ){
// is getting too close
}
}
}