Question

I have used raycasting method to detect different colored strips on either side of the track and keeping my car object in position by calculating the distance. But the problem is the ray always points in the constant direction in the global coordinate system and doesnt change with the movement(rotation) of car object. It could have if the ray direction were in the reference frame of car which I am not able to figure out how to do. Currently I am doing this

var ray = new THREE.RayCaster(car.position, new THREE.Vector3(-1,0,0),0,50);

The movement of car is in the X-Z plane

Can someone point out a solution ?

Était-ce utile?

La solution

Your ray-casting is being done in world-space, so you need the correct world-space vector.

I am assuming the car is a child of the scene, and not some other rotated object.

To construct a unit vector that points in the direction the car is heading in the world coordinate system, first construct a unit vector that points in the direction the car is heading in it's local coordinate system -- whatever that happens to be in your case:

var vector = new THREE.Vector3( -1, 0, 0 );

Then apply the same rotation to that vector as is applied to the car.

vector.applyQuaternion( car.quaternion );

EDIT: Updated to three.js r.66

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top