0

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 ?

1 Answers1

2

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

WestLangley
  • 102,557
  • 10
  • 276
  • 276
  • Thanks a lot @WestLangley.. the issue has been resolved .. but the strips which are being detected are opposite of wat is expected i.e strips on -ve x of world-space are being detected using Vector3(1,0,0) and viceversa.. I can still use them in my logic , but I would like to know why its happening – dev_shaguar May 11 '13 at 11:28
  • You need to post another question if you are having additional problems. I suggest you provide a simple, live example (jsfiddle.net) to demonstrate the problem. – WestLangley May 11 '13 at 14:00
  • Ok ... thnx ... will put up another question – dev_shaguar May 13 '13 at 04:38