Convergence Posted October 29, 2015 Share Posted October 29, 2015 I feel silly asking about a relatively simple problem, but my trigonometry has been fading for over a decade now.. So, moving a mesh (character, projectile) forward taking into account its rotation in 2D space is simple enough with a Google search:m.rotation.y = Math.PI / 2; // or any value < 2pi.var x = Math.sin(m.rotation.y) * speed * delta;var z = Math.cos(m.rotation.y) * speed * delta;m.position.addInPlace(new BABYLON.Vector3(x, 0, z)); How to extend it to account for its rotation on the x axis? simple trying either sin() or cos() on .rotation.x resulted in straying projectiles. edit: probably the mesh-orientation-towards-target calculation needs extending as well to account for the y-axis // x and z are coords of desired mesh target locationdx = (mesh.position.x - x);dz = (mesh.position.z - z);var r = Math.atan(dx / dz);if (dz < 0) { r -= Math.PI;}mesh.rotation.y = r;edit: playground http://www.babylonjs-playground.com/#ZWONO Quote Link to comment Share on other sites More sharing options...
iiceman Posted October 29, 2015 Share Posted October 29, 2015 Different approach, but maybe not what you want (since it stops at the sphere and maybe some other differences): http://www.babylonjs-playground.com/#ZWONO#1 Convergence and JohnK 2 Quote Link to comment Share on other sites More sharing options...
JohnK Posted October 29, 2015 Share Posted October 29, 2015 Yet another approach, which again might not be what you want http://www.babylonjs-playground.com/#ZWONO#2 By variations in the given angle and speed (which are determined in such a way as to hit the sphere) you can make it miss. EDIT Added a lookAt to orientate the box. http://www.babylonjs-playground.com/#ZWONO#3 Convergence 1 Quote Link to comment Share on other sites More sharing options...
Convergence Posted October 29, 2015 Author Share Posted October 29, 2015 Thanks! both are helpful, and cleaner than my original. edit: Also thanks for the code comments. Quote Link to comment Share on other sites More sharing options...
adam Posted October 30, 2015 Share Posted October 30, 2015 and another: http://www.babylonjs-playground.com/#ZWONO#4 Quote Link to comment Share on other sites More sharing options...
Convergence Posted October 31, 2015 Author Share Posted October 31, 2015 and another:http://www.babylonjs-playground.com/#ZWONO#4Thanks, also using this slightly modified for unguided projectiles 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.