Jump to content

How to make in-game switch?


splash27
 Share

Recommended Posts

I want to implement a switch inside scene world. https://www.babylonjs-playground.com/#SSXJEN (Scene uses WASD controls and mouse-look)

The switch is represented by the red face of the mesh. I want to launch a custom code, when the switch is activated (when user presses "e" near the switch).

I thought to use an actionManager. However, I don't know how to determine that I'm standing in front of the red face near the mesh.

At this point the custom code runs every time I press "e".

How to find out that I am near the switch and I am actually looking at it?

Link to comment
Share on other sites

Hiya splash and everyone.  Hairy Krishmas!

https://www.babylonjs-playground.com/#SSXJEN#2

There's one way of many.

Lines 44-47 establish a "sensor" plane... glued to the box.  :)  (later invisible, likely)

Lines 63-68 check that the sensor-plane is in-view of the camera, and 2 units or less away-from camera.

If all that is satisfied, 'e' keypress will work.

I tried it using the box itself and not a 'sensor', but that allows 'e' presses from the OTHER sides of the box.  The sensor worked better.

Others might have better ideas, so stay tuned.

PS-aside:  Does anyone else see the playground app... do a screen-scroll... when camera/keyboard down-arrow is used?  White "bar" appears on bottom of PG screen, removed by pressing up-arrow?  (Wingnut using Firefox).  It's been happening for a few months now, and it sure is annoying when using freecams, eh?  Maybe I'm the only one that sees it.  Something in the CSS needs overflow: none;, or something.

Link to comment
Share on other sites

Hi, Wingnut! Happy Christmas!

Great idea! 

I thought that I can use facets data in similar way, but decided that it will be hard to determine where each facet is on complex mesh. So, the sensor's approach is much more easy and predictable.

By the way, can you clarify what scene._frustumPlanes contains? At this moment it works like magic for me.

 
Link to comment
Share on other sites

hehe.  I stole it from here... https://www.babylonjs-playground.com/#DGTY21#1

I think others better explain it.  It's magic stuff, to me.  :)

I guess... at camera.minZ and camera.maxZ... there are some magical invisible non-mesh planes.  They delineate the start and end of the camera's field of view (fov) frustum/cone.

isInFrustum() uses this scene.activeCamera FOV cone, for testing. 

That's all the further I want to explain.  It can be read-about, mostly on the web.  Or, study BJS source code.  I'm pretty stupid on that subject, sorry.

Link to comment
Share on other sites

Line 96.  :)  It is this._frustumPlanes... but it is inside-of an "overload-the-scene-object" add-on function, so I know this means scene.

Why the interest in the camera frustum?  Do you have some mad scientist ideas?  ;)  I think some folks use it to rotate/position the camera so that ALL mesh in the scene, are in-view.  *shrug*

Link to comment
Share on other sites

Hi again.  You could also parent an invisible mesh to the front of the player/camera.  Then use intersectsMesh().  If the invisible mesh is not intersecting with the interaction device/mesh, no interaction allowed.  Perhaps worth some tests.

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