Jump to content

Determine Ray destination


richardsmd
 Share

Recommended Posts

Hiya @richardsmd, welcome to the forum.

http://www.babylonjs-playground.com/#16CYXN#0

Sorry for the slow replies.  I think line 22... might be of-interest to you.  :)  Hope this helps.  Holler if you need more help.  Babylon Playground searcher reports 946 hits... when searching for 'Ray'.  Plenty of various examples to tour.   Be well, party on!

Link to comment
Share on other sites

I (perhaps incorrectly) conceptualize a ray as an origin point and a direction. Without a length the ray extends infinitely from it's origin, but a ray with a length has a constrained endpoint/destination. After I was clued into the existence of RayHelper I easily located the code:

var endPoint = ray.origin.add(ray.direction.scale(ray.length))

Perhaps I should explain why I even want this...

I'm trying to add additional movement controls to a freecamera. What I'd like to do is set scroll up/down to "jump" the camera some distance (say, 20 units) exactly along it's current path. Given the camera object, the easiest way I can think to do this is

I was expecting to discover how to do this in Scene.pickWithRay. I followed the code as far as AbstractMesh.intersects, but now get the impression that I've incorrectly conceptualized how Ray's are used.

@adam linked a playground demonstrating how to render a ray with a RayHelper. I found this in RayHelper.show which in conjunction with this playground (lines 38-42) that @Deltakosh provided in this post led to the following...

 

/** Jump camera forward/backward in reaction to WheelEvent.
*
* @param {WheelEvent} e - the event. Must contain wheelDelta property.
*/
function onWheel(e) {
    let cam = scene.activeCamera;

    let invView = new Babylon.Matrix();
    cam.getViewMatrix().invertToRef(invView);
    let direction = Babylon.Vector3.TransformNormal(Babylon.Vector3.Forward(), invView);
    direction.normalize();

    // Go "backwards" on wheel down
    if (e.wheelDelta < 0) {
      direction.scaleInPlace(-1);
    }

    cam.position.addInPlace(direction.scale(20));
}
canvas.addEventListener("mousewheel", onWheel);

Thank you all very much for your assistance!

 

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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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