osadchi Posted October 13, 2016 Share Posted October 13, 2016 Hello everyone, I need to find out coordinates when clicking on the ground. Don't know is it correct directory for that question. I have the FollowCamera, so when got click, need to get coordinates on the ground, don't even imagine how to do that. Need a theory. //////////// added Ok, I got it, Im using the ray. var ray = new BABYLON.Ray(camera.position, camera.getTarget().subtract(camera.position)); When I'm clicking it shows me the target position on the ground. But how I can create the ray from the cursor position and camera direction? Is there some way to get cursor position relatively camera? GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
getzel Posted October 13, 2016 Share Posted October 13, 2016 Hello ! Babylon.js has some functions in the scene object for raycast like this one : scene.pick(scene.pointerX, scene.pointerY); var x; var z; function clickf(){ var pickResult = scene.pick(scene.pointerX, scene.pointerY); if (pickResult.hit) { x = pickResult.pickedPoint.x; z = pickResult.pickedPoint.z; }// if result }//clickf() window.addEventListener("click", function() { clickf(); }); A little example to move your character http://www.babylonjs-playground.com/#1JPHAD#1 osadchi 1 Quote Link to comment Share on other sites More sharing options...
osadchi Posted October 13, 2016 Author Share Posted October 13, 2016 Awesome! Thank you alot! Quote Link to comment Share on other sites More sharing options...
Dad72 Posted October 13, 2016 Share Posted October 13, 2016 @osadchi There are better than the use of the scene.pick. Babylon to a function that does everything scene.onPointerDown = function (evt, pickResult) { if (pickResult.hit) { impact.position.x = pickResult.pickedPoint.x; impact.position.y = pickResult.pickedPoint.y; impact.position.z = pickResult.pickedPoint.z; } }; http://www.babylonjs-playground.com/#1VO5GO osadchi 1 Quote Link to comment Share on other sites More sharing options...
getzel Posted October 14, 2016 Share Posted October 14, 2016 So many functions lol, this is a good one too. Quote Link to comment Share on other sites More sharing options...
osadchi Posted October 14, 2016 Author Share Posted October 14, 2016 Thank everyone, I got something like this. scene.onPointerDown = function(evt, pickResult) { if (pickResult.hit) { if (pickResult.pickedMesh == ground1) { // if pickedMesh is a ground sphere.position = pickResult.pickedPoint; } else { // if not, trying to find it with a ray and picked position var ray = new BABYLON.Ray(pickResult.pickedPoint, new BABYLON.Vector3(0,-1,0)); pickResult = scene.pickWithRay(ray, function(item) { if (item == ground1) { return true; } return false; }); if (pickResult != null && pickResult.pickedPoint != null) { sphere.position = pickResult.pickedPoint; } } } Quote Link to comment Share on other sites More sharing options...
Dad72 Posted October 14, 2016 Share Posted October 14, 2016 Your condition 'else {} ' is not necessary. Quote Link to comment Share on other sites More sharing options...
osadchi Posted October 15, 2016 Author Share Posted October 15, 2016 8 hours ago, Dad72 said: Your condition 'else {} ' is not necessary. Why? I can get another mesh, I need coordinates on the ground. Or.... it's just another Y. Is Y constant? Quote Link to comment Share on other sites More sharing options...
Dad72 Posted October 15, 2016 Share Posted October 15, 2016 If you just want to recover the ground, your 'else{}' is useless because it will never use. For me 'if {}' and to 'else {}' are the same, so your 'if {}' is sufficient. scene.onPointerDown = function(evt, pickResult) { if (pickResult.hit) { if (pickResult.pickedMesh == ground1) { // if pickedMesh is a ground sphere.position = pickResult.pickedPoint; } } }; Otherwise if you want to recover any land or other object: scene.onPointerDown = function(evt, pickResult) { if (pickResult.hit) { pickResult.pickedMesh.position = pickResult.pickedPoint; } }; Quote Link to comment Share on other sites More sharing options...
osadchi Posted October 15, 2016 Author Share Posted October 15, 2016 3 hours ago, Dad72 said: If you just want to recover the ground, your 'else{}' is useless because it will never use. It will because I can get another mesh. If Im clicking on sphere or another mesh, it returns sphere, not ground. Quote Link to comment Share on other sites More sharing options...
osadchi Posted October 15, 2016 Author Share Posted October 15, 2016 Is there any example of BABYLON about how to build a path? I mean around all mesh objects on the ground from character to the picked coordinates? Quote Link to comment Share on other sites More sharing options...
JohnK Posted October 15, 2016 Share Posted October 15, 2016 By path do you just mean a line or something that looks like a path as in garden path. In either case check out Lines or Ribbon in http://doc.babylonjs.com/tutorials/Mesh_CreateXXX_Methods_With_Options_Parameter Quote Link to comment Share on other sites More sharing options...
getzel Posted October 15, 2016 Share Posted October 15, 2016 Maybe he talks about pathfinding AI. I didn't see that here yet. Very useful to program Non Player Characters movements. You can look for A* algorithm, i think it's with 2d arrays. Or make 2 raycasts (low range) from left and right shoulder of the bot pointing forward (in bot's space). The bot goes to the desired point and when one ray hits an obstacle, the bot has to turn in the direction where the obstacle is the shortest. Or calculate where is the empty space before it moves. osadchi 1 Quote Link to comment Share on other sites More sharing options...
Dad72 Posted October 15, 2016 Share Posted October 15, 2016 2 hours ago, osadchi said: It will because I can get another mesh. If Im clicking on sphere or another mesh, it returns sphere, not ground. scene.onPointerDown = function(evt, pickResult) { if (pickResult.hit) { pickResult.pickedMesh.position = pickResult.pickedPoint; } }; This selects any type of mesh, either a sphere or land and you references the position of the click. Quote Link to comment Share on other sites More sharing options...
osadchi Posted October 15, 2016 Author Share Posted October 15, 2016 Im talking about pathfinding. If we create a ray from the character position, what can get new ray from collision position? I need to find the red - shortest way to the target. Blues are rays, from character and 2 from first collision... How use them to find the way? Quote Link to comment Share on other sites More sharing options...
getzel Posted October 15, 2016 Share Posted October 15, 2016 ^^ No. I edited my post. The first ray is good. I mentionned 2 rays (for more precise obstacle detection) with short range (like 4 meters, to detect collision 4 seconds before it happens) pointing forward in bot's space. But after the first pre-collision, the bot turns a little bit. And its rays are still pointing forward in local space. So he will turn smoothly. This is a start method, you can add some functions. Quote Link to comment Share on other sites More sharing options...
osadchi Posted October 15, 2016 Author Share Posted October 15, 2016 @getzel But for finding way before going it can be useful? I need find shortest path before start walking, is it possible without map? If I will check short place it can going on the red way, I need calculate green before it walks. OR.. I didnt understand your idea Quote Link to comment Share on other sites More sharing options...
getzel Posted October 15, 2016 Share Posted October 15, 2016 Yea you can look at the famous A* algorithm and its variants ! I haven't the exact answer cause I'm also a beginner. osadchi 1 Quote Link to comment Share on other sites More sharing options...
Dad72 Posted October 15, 2016 Share Posted October 15, 2016 Ok I understand better. this kind of algorithm is rather interesting to implement. I did not experience this, but it interests me. Quote Link to comment Share on other sites More sharing options...
Dad72 Posted October 15, 2016 Share Posted October 15, 2016 An idea can be and create a kind of moving a ghost mesh that can route all the paths and calculates what way was the course, without hindrance. this path is register memoir and character take this path. I do not know if the level of calculated time is the fastest, but I guess it can be a line of thought. Or to make it faster, we send 3 ghost mesh and is the one who arrives first determines the path to take for the character. then "dispose()" has 3 famtomes It's just an idea. Quote Link to comment Share on other sites More sharing options...
osadchi Posted October 15, 2016 Author Share Posted October 15, 2016 @getzel Yeah, it needs to be created a map for the blocking objects. https://gamedevelopment.tutsplus.com/tutorials/how-to-speed-up-a-pathfinding-with-the-jump-point-search-algorithm--gamedev-5818 Quote Link to comment Share on other sites More sharing options...
osadchi Posted October 15, 2016 Author Share Posted October 15, 2016 Holly Wiki! I got the idea of that thing : ). Thanks alot for the help! Dad72 1 Quote Link to comment Share on other sites More sharing options...
Wingnut Posted October 16, 2016 Share Posted October 16, 2016 Hi guys. Yesterday, I was touring playgrounds, and I found this playground. (Found while searching for 'boundingInfo'). Click around the outside edge (repeatedly). Some kind of "pathfinding" is happening in that playground. Not sure if useful, but I thought I should pass it to you. Also, busy forum member Sam Girardin has done some crowd sim work. http://www.visualiser.fr/Babylon/Crowd/ (choose scene #5 or higher... for interesting pathfinding) Be well. osadchi 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.