Jump to content

scene.pickWithRay blues


Recommended Posts

I have a character, a target (a box attached to a camera) and wall. For some reason, when Target is behind Wall, Character can still see the bloody Target!

What's the correct way of using pickWithRay? Is there a way to visualize rays, e.g. draw a line?

Link to comment
Share on other sites

It is better if you send a Playground of your bloody shooting demo ;)


There is a predicate function with this method. And you have in parameter the mesh hitten.

function predicate (mesh){

return mesh === target;


It will return true if the wall is not between in theory.



Check that your ray is well positioned (y > 0), make a big wall to test.


I had some issues when I tried to pick a character's mesh. When he turned, the result was false because he's skinny.

So I attached an invisible box to him and it was ok.

Sometimes the ray touched the character even behind wall but maybe it's over now.

A debug function to draw the ray could be cool.



I have a little question :

is there a function to pick multiple objects with a ray and return an array of pickedMesh ? (like a bullet passing through several things)

Link to comment
Share on other sites

Heya getzel, max, and any other bloody readers.  ;)

G... I don't think there IS a multi-hit ray... but... are you thinking what I am thinking?

You mentioned predicate.  What if... you kept targeting the same mesh, but you repeatedly change the ray's origin/source mesh... to be the most recently-hit mesh?  Keep moving the ray launch-point... to the hit mesh... and fire again.  :)

I think rays ALWAYS stop at the first hit.  But then... store that hit in a "whichMeshHitSoFar" array, then change the ray origin to the hit mesh, and fire again.  Rinse and repeat... until the origin and the target are the same mesh.  No need to fire any more rays, then.

Can you imagine this?  5 mesh in a line, try to fire ray from 1 to 5.  Ray hits mesh 2 (a blocker).  Push mesh2 into array, move ray origin to mesh2, and fire again.  Hits mesh3 (a blocker).  Store, make it origin, and fire again.... etc etc.  Lily pad hopping!  :)  Might work.  Sort of demented, so likely fun!

Carpenter 1 needs the distance to carpenter 5.  But while he is extending his tape measure towards C5, it hits carpenter 2 and is blocked.  So C1 asks C2 to pull the measuring tape (and spool) to himself, and continue measuring "down the line".  C2 gathers the tape and spool, and then extends the tape measure towards C5, but it hits carpenter 3... etc, etc, etc.  heh.  Goofy.  Hand-along tape measuring.  :)

Link to comment
Share on other sites

Hey, bloody babylonians :)

@getzel, I'll try to put together a PG - might take time though: in my game a Blender scene is loaded and then loads of objects get instanced and placed based on a json config with a bunch of coordinates. The Charecter (shooter) is then set to lookAt the camera's box and runs PickWithRay to check if the target is in 'line of sight'. Maybe here I'm doing something fishy?

@Wingnut, a cleaner solution would be an all-penetrating ray returning an array of pickedMesh objects ordered on hit-first basis. And that would be one bloody useful feature too!

Link to comment
Share on other sites

Try first on your side to make a simple scene with basic meshes (not loaded) and check if you have the same problem.



a cleaner solution would be an all-penetrating ray returning an array of pickedMesh objects ordered on hit-first basis. And that would be one bloody useful feature too!

Yea :)

Link to comment
Share on other sites


Max123... um... you didn't give many details but... could your parented-to-camera wall be see-through... because camera.minZ is set to high?  (just checking).

Nah, that doesn't sound correct.  Target is parented to camera, not wall.  hmm

Yep, Max needs to build us a playground... or go searching for picking ray examples with a bloody playground search.  :)

Link to comment
Share on other sites

Hello dear friends !


Here is a PG with a ray working :


It is without predicate so it picks the first mesh.

At the beginning, it touches box2 before the elements get positionned so we can wait before launching the raycast() if we need.

After, wall get touched, then box2 and finally nothing once all mesh have moved away.

I don't know if my math method is the best.


I made a raycast2 function with another method :

function raycast2(){

    var vector1 = new BABYLON.Vector3(0,0,6);
    var vector2 = new BABYLON.Vector3(0,0,1);
    var ray = new BABYLON.Ray(vector1, vector2, 100);
    ray = BABYLON.Ray.Transform(ray, box1.getWorldMatrix());    // new method, there is an another one to create a ray
    var raycast = scene.pickWithRay(ray, null, false);
    if (raycast.hit){

} // raycast2()


Is it possible to make a draw ray function please (to debug) ?

How the Text2d works ? I can't display it. A simple example is welcome.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...