Jump to content

Preventing objects to go out of screen.


berrur
 Share

Recommended Posts

Hello guys, i'm totally new to Babylon and I was wondering if there's a way to check if an object is hitting the border of the screen and prevent it to get off the screen.
This is what i wrote so far: http://www.babylonjs-playground.com/#SKWZP#0
I found these values manually, i hate to hard-code stuff inside my code but it was just for testing.

  if (box1.position.x > 6 ) {
    box1.position.x = 6;
  }
  if (box1.position.y > 4 ) {
    box1.position.y = 4;
  }
  if (box1.position.x < -6) {
    box1.position.x = -6;
  }
  if (box1.position.y < -4) {
    box1.position.y = -4;
  }

Can you please me help to understand how can i retrieve the screen boundaries wrt the viewport?

Link to comment
Share on other sites

Hey

you should use Vector3.Project (http://doc.babylonjs.com/classes/2.3/Vector3#static-project-vector-world-transform-viewport-rarr-vector3-classes-2-3-vector3-) to project the box position to 2d and then check against the current resolution

Other option: check if your mesh is in the active meshes by testing scene.isActiveMesh(mesh). If false then object is offscreen

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