fateriddle Posted October 16, 2018 Share Posted October 16, 2018 How to create a mesh that's always approaching closer to your camera? I've got a universalCamera that you can move around using arrow keys, I kinda what enemies(meshes) spawn around the camera position, and always move towards camera, how do I do that? Quote Link to comment Share on other sites More sharing options...
ssaket Posted October 16, 2018 Share Posted October 16, 2018 Use the A* algorithm; you can spawn enemy meshes at random locations and then find the min. path to the camera's position . Here, checkout this blog (http://theory.stanford.edu/~amitp/GameProgramming/) you can also use sound effects to determine from where the enemy is coming (like behind/ right etc) Quote Link to comment Share on other sites More sharing options...
trevordev Posted October 16, 2018 Share Posted October 16, 2018 If you want just a simple direct path to camera you can try this http://playground.babylonjs.com/#349JX1 . Quote Link to comment Share on other sites More sharing options...
Dad72 Posted October 17, 2018 Share Posted October 17, 2018 You can use getFrontPosition which allows to create for example a object in front of the camera. scene.activeCamera.getFrontPosition(8); Quote Link to comment Share on other sites More sharing options...
fateriddle Posted October 17, 2018 Author Share Posted October 17, 2018 9 hours ago, trevordev said: If you want just a simple direct path to camera you can try this http://playground.babylonjs.com/#349JX1 . Big thanks! This works just fine for me. Another question, how do you spawn those meshes once every second? Every example I find, meshes all exist at the very beginning. trevordev 1 Quote Link to comment Share on other sites More sharing options...
Sebavan Posted October 17, 2018 Share Posted October 17, 2018 Reusing is usually a good practice that reduces memory pressure throughout the application. If you know your max number, you could create them upfront and disable them. Then when you need you pick in your pool enable do something and disable once done. This would greatly improve the perf of your app. In case you need dynamic creation, you can still call createMesh on demand ? Quote Link to comment Share on other sites More sharing options...
trevordev Posted October 17, 2018 Share Posted October 17, 2018 Try http://playground.babylonjs.com/#349JX1#1, this creates a new one every time w is pressed, for perf you could create them up front and make them rendered only when a new one is "created" like Seb said. To get it to render every second you can just add up the deltatimes until it equals one or use a settimeout method. Quote Link to comment Share on other sites More sharing options...
fateriddle Posted October 18, 2018 Author Share Posted October 18, 2018 16 hours ago, Sebavan said: Reusing is usually a good practice that reduces memory pressure throughout the application. If you know your max number, you could create them upfront and disable them.? Thank you! I've got questions: 1. How reusability works in your description? I thought reusing means you create and recycle 1 or maybe 5 meshes. But in this case, we create them all upfront, how's that gonna benefit perf? 2. If there aren't max number for meshes(they spawn infinitely), but I'll disable all those who get closer enough or hit the camera. How to code in this case? Quote Link to comment Share on other sites More sharing options...
fateriddle Posted October 18, 2018 Author Share Posted October 18, 2018 9 hours ago, trevordev said: Try http://playground.babylonjs.com/#349JX1#1 1. I notice every ball has a "long tail" behind it, is this a concern? 2. About coding style and performance: say the whole scene consist of 3 stages. Is it better to add 3 onBeforeRenderObservable, each doing exclusive the animation on its stage; or is it better having one gigantic onBeforeRenderObservable and putting all the code in there? 3. With the help of deltaTime and a timer, I find onBeforeRenderObservable can cover all the cases of animation, do we still need beginAnimation? What in your working experience is the case? Quote Link to comment Share on other sites More sharing options...
Sebavan Posted October 18, 2018 Share Posted October 18, 2018 1. How reusability works in your description? I thought reusing means you create and recycle 1 or maybe 5 meshes. But in this case, we create them all upfront, how's that gonna benefit perf? Imagine you know that maximum you have 10 balls visible (based on max distance visible, speed and emit rate), you create 10 balls all invisible and then you pick the 10 first from the pool. As soon as one of the 10 dies you can use it for 11 and so on. 2. If there aren't max number for meshes(they spawn infinitely), but I'll disable all those who get closer enough or hit the camera. How to code in this case? Basically the disabled ones become available to be reused. trevordev 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.