0

Let's say I have two vertices and I want to know the 3D angle between the two. By 3D angle, I mean the angle between the one vertex and another on each of the three dimensional planes, stored as

{ x, y, z }

function getAngleBetweenVertices(vert1, vert2){
  return {
    x: 0, // ?
    y: 0, // ?
    z: 0  // ?
  }
}

// Classes

class Vertex {
  constructor(position){
    this.position = {
      x: 0,
      y: 0,
      z: 0,
      ...position
    };
  }
}

// Init

let vert1 = new Vertex({ x: 1, y: 0, z: 1 });
let vert2 = new Vertex({ x: 0, y: 1, z: 1 });
let angle = getAngleBetweenVertices(vert1, vert2);
console.log("angle ", angle);

enter image description here

For example in the above image, I've traced over a line segment joining two vertices on a triangle. It should be possible to find the angle between the two vertex positions on the x, y, and z axis.

How can I calculate the three dimensional angle between two vertices?

J.Todd
  • 209
  • 4
  • 23
  • In its current state, the question might be closed as unclear since (based on your comment), it seems to be unclear even to you. What are you trying to achieve? – Robby Cornelissen Jun 11 '19 at 04:47
  • [this](https://stackoverflow.com/questions/2827393/angles-between-two-n-dimensional-vectors-in-python/13849249#13849249) was linked as a possible duplicate candidate, but I was unable to decipher the python there and discern the correct JS version of the solution. – J.Todd Jun 11 '19 at 06:04
  • @RobbyCornelissen The angle from one vertex to another. – J.Todd Jun 11 '19 at 06:11
  • When I first posted this question I was using the term "vector" instead of "3D angle", which was incorrect. I'm looking for the angle between two locations in 3D space on all three planes. – J.Todd Jun 11 '19 at 06:37
  • @Spektre well what term should be used for a collection of 3 angles along the 3 dimensional planes? – J.Todd Jun 11 '19 at 08:08
  • `acos( (x1*x2 + y1*y2 + z1*z1) / sqrt( (x1*x1 + y1*y1 + z1*z1)*(x2*x2+y2*y2+z2*z2) ) )` doesnt seem like what Im looking for anyway, because I dont see how you can describe a 3D angle with less than three separate values and that func outputs a single value. But thank you for the translation because I wasnt sure about that vector math translated to js. – J.Todd Jun 11 '19 at 08:14
  • It sounds as though you're looking for [direction cosines](https://en.wikipedia.org/wiki/Direction_cosine). – Mark Dickinson Jun 11 '19 at 12:01
  • 1
    @Spektre The purpose of getting the angles is to then build a simplistic (non-perfect, but fast) function for figuring out where a ray should bounce when it collides with a tri. By knowing the angle from one vertex of the tri to the next, for all three edges, I believe I can calculate a pretty fast and somewhat accurate vector for where the ray should bounce. Ultimately Im working on figuring out my own version of reflective light in a raytracing system, for the sake of fun. – J.Todd Jun 11 '19 at 17:42
  • @Spektre Ill have a look, thanks! – J.Todd Jun 11 '19 at 23:55
  • @Viziionary I moved the comments into answer and add a sketch for you so its clear what is what and how I got there ... – Spektre Jun 12 '19 at 06:41

2 Answers2

2

You can have angle between two directions v1,v2 (vectors) like this:

ang = acos(dot(v1,v2)/(|v1|.|v2|))

which translates in 3D to:

ang = acos( (x1*x2 + y1*y2 + z1*z1) / sqrt( (x1*x1 + y1*y1 + z1*z1)*(x2*x2+y2*y2+z2*z2) ) )

However you can not have angle between two points that simply has no meaning. Also beware 3D angle is not what you think it is (its angle in steradians and you can look at it as a volume coverage ... normal angle is area coverage) and yes its also scalar value. So what you are looking for are direction cosines or Euler angles (for which you need more info and order of transforms not to be ambitious) or transform matrices.

But as I suspected its an XY problem and based on your comments I was right.

So your real problem (based on comments) is to find the reflected ray from (triangle) face. Using angles (direction cosines nor euler angles nor transform matrices) is a really bad idea as that would be extremly slow. Instead use simple vector math I see it like this:

reflect

So you got ray direction dir and want the reflected one dir' from face with normal nor so:

dir' = 2 * ( nor*dot(-dir,nor) + dir ) - dir
dir' = 2 * ( -nor*dot(dir,nor) + dir ) - dir
dir' = -2*nor*dot(dir,nor) + 2*dir - dir
dir' = -2*nor*dot(dir,nor) + dir
dir' = dir-2*nor*dot(dir,nor)

so in 3D it is:

dir=(dx,dy,dz)
nor=(nx,ny,nz)
t = 2*(dx*nx + dy*ny + dz*nz) // 2*dot(dir,nor)
dx' = dx-t*nx
dy' = dy-t*ny
dz' = dz-t*nz

as you can see no goniometrics or angles are needed whatsoever... Also does not matter if normal points in or out of face/object the dot handles the signs on its own...

In case you need the normal can be computed by cross product of its 2 sides so if the triangle is defined by v0,v1,v2 points then:

nor = cross( v1-v0 , v2-v1 )

Here an example where I use this technique for a raytracer:

its mine GLSL ray tracer supporting reflections on triangle faces and it has no goniometrics in it ... look for // reflect comment in the fragment shader especially look for:

ray[rays].dir=ray[rays].dir-(2.0*t*ray[rays].nor);

its the reflection where

t=dot(ray[i0].dir,ray[i0].nor);

where dir is ray direction and nor is face normal (look familiar? yes its the same equation)...

Spektre
  • 41,942
  • 8
  • 91
  • 312
0

I'm not sure if this code is correct but I think its what I was looking for.

// Utilities

function normalizeAngle(angle){
  if (angle > 360) return angle - 360;
  if (angle < 0) return 360 + angle;
  else return angle;
}

function getAngleBetweenPoints(cx, cy, ex, ey){
  var dy = ey - cy;
  var dx = ex - cx;
  var theta = Math.atan2(dy, dx);
  theta *= 180 / Math.PI;
  return theta;
}

function getAngleBetweenVertices(vert1, vert2){
  return {
    x: normalizeAngle(getAngleBetweenPoints(vert1.position.z, 
        vert1.position.x, vert2.position.z, vert2.position.x)),
    y: normalizeAngle(getAngleBetweenPoints(vert1.position.z, 
        vert1.position.y, vert2.position.z, vert2.position.y)),
    z: normalizeAngle(getAngleBetweenPoints(vert1.position.x, 
        vert1.position.y, vert2.position.x, vert2.position.y))
  }
}

// Classes

class Vertex {
  constructor(position){
    this.position = {
      x: 0,
      y: 0,
      z: 0,
      ...position
    };
  }
}

// Init

let vert1 = new Vertex({ x: 1, y: 0, z: 1 });
let vert2 = new Vertex({ x: 0, y: 1, z: 1 });
let angle = getAngleBetweenVertices(vert1, vert2);
console.log("angle ", angle);
J.Todd
  • 209
  • 4
  • 23