Raycast broke after using raycastFromRay

Hello needle,

I’m using different raycast from my project, and I needed to pre-calcul a matrix position for an object so I use the function :

initializeHookCutoutMatrix() {
    const gingivalPoint = new THREE.Vector3(...this.landMark["Vestibular Gingival"])
    if(this.depth > 12) gingivalPoint.y += this.lowerTooth ? (this.depth - 12 )/ 2 : -(this.depth - 12 )/ 2
    const gingivalPointExternal = new THREE.Vector3(...this.landMark["Vestibular Gingival"]).multiplyScalar(1.2)
    // Create a Raycaster instance
    const raycaster = new THREE.Ray(gingivalPointExternal, gingivalPointExternal.clone().multiplyScalar(-1));

    // Perform the raycast, results will be stored in an array
    const results = this.context.physics.raycastFromRay(raycaster);


    if(results[0] && results[0].point && results[0].normal) {
      const point = results[0].point
      const normal = results[0].normal
      const pointToLookAt = point.clone().add(normal?.clone())

      const objForMatrix = new THREE.Object3D()
      objForMatrix.position.copy(point)
      objForMatrix.lookAt(pointToLookAt)

      // const matrix = objForMatrix.matrix.clone()

      this.hookCutoutMatrix = objForMatrix.matrix.clone()
    }
  }

Problem, it works well but if I call it it seems to break my main raycast.
Which is used like :

const simpleRay = this.context.physics.raycast();
        let normal : any = null
        let point : any = null

        simpleRay.forEach(ray => {
          if(ray.object == this.gameObject) {
            normal = ray.normal?.clone()
            point = ray.point?.clone()
          }
        })

Does the fonction raycastFromRay change the default settings of raycast ?

Original Post on Discord

by user 224464722878005248

Hello there – we have an experimental AI bot :robot: that might be able to help you with your question. Would you like to try this out?

How it works:
Reply to the messages of the bot that end with a thread_id to interact with it and ask follow-up questions. You can also mention the bot directly in this channel to get help.

Help requested: please wait a moment…

Here is what I found for you :brain: Discord

Hello AeistDesmos,

raycastFromRay function should not alter the default settings for raycast. The raycast function uses default parameters like mouse position and camera, while raycastFromRay lets you define your own Ray object to specify the origin and direction for raycasting. It’s possible that another part of the code is causing the issue, or there might be a state change somewhere that’s affecting how raycasts behave.

If you’re consistently seeing an issue where using raycastFromRay breaks the behavior of raycast, it would be recommendable to check the following:

  1. Ensure there is no shared state or modification of global objects between the two functions.
  2. Verify if any other code is modifying the physics world or objects’ transforms that may affect raycasting results.
  3. Double-check that the gameObject you are testing against in the simpleRay case hasn’t been altered after running raycastFromRay causing the raycast to miss.

If the problem persists, consider isolating the affected code and creating a minimal example that reproduces the issue. This way, it would be easier to identify any conflicts or unexpected behavior.

Please let me know if you need further assistance with debugging or isolating the issue.

0 messages left GPT 4
thread_hQSfQDKBKpAlZiHPGeoKQhZ5
Thanks for your feedback!

Yes but no
The only thing which changed is the call of raycastFromRay so it modify the behaviour of raycast (I siolated the code to be sure)

by user 224464722878005248

Hi @AeistDesmos I’ll try to reproduce this in a little bit. You can also use the normal raycast method and pass your ray in with the RaycastOptions argument