I got incorrect worldTolocal value if parent object is rotated at angle.
Here is related code for that.
first, I create one object3d and add it to scene.
after that I moved that object to required position at ‘pos’
and set z rotation to ‘rot.z’
after that updateMatrixWorld().
now I have array of xyPos from which I want to cast ray.
everything works fine when I set z rotation to 0.
but if I changed rotation I got incorrect worldTolocal position.
I also attached console screenshot for both.
function createHolesB(xyPos, pos, rot) {
var mainObj = new THREE.Object3D()
mainObj.name = "mainPattern_" + mainPatternGrp
mainPatternGrp++
scene.add(mainObj)
mainObj.position.copy(pos)
mainObj.rotation.z = rot.z
// console.log(mainObj.rotation);
mainObj.updateMatrixWorld()
xyPos.forEach((e, i) => {
// debugger
var raycaster = new THREE.Raycaster();
console.log(scene.getObjectByName("mainPattern_0").position);
debugger
var castPo = mainObj.worldToLocal(new THREE.Vector3(e.x, e.y, 1000))
console.log(scene.getObjectByName("mainPattern_0").position);
castPo.negate()
castPo.z = 1000
console.log(castPo);
raycaster.set(castPo, new THREE.Vector3(0, 0, -1))
var intersection = raycaster.intersectObjects(addedObject, true);
console.log(intersection);
debugger