Jump to content

(quick question) How to move a mesh forward with trigonometry


Convergence
 Share

Recommended Posts

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

Link to comment
Share on other sites

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

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