HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

3 Math Related Questions

11-24-2006, 04:09 PM#1
wyrmlord
Since I don't know math stuff relating to 3D grids, I have three questions:

1. How would I do something like PolarProjectionBJ except instead of just x/y coordinates it would have x/y/z coordinates?

2. How would I go about getting the angle between two x/y/z coordinates?

3. As you might already have guessed after reading the last two questions, how do you get the distance between two x/y/z coordinates? I'm not entirely sure, but here's my guess as to how I think it would work:
Collapse JASS:
function DistanceBetweenPoints2 takes real x, real y, real z, real x2, real y2, real z2 returns real
 local real dx = x - x2
 local real dy = y - y2
 local real dz = z - z2
 local real dist = SquareRoot(dx * dx + dy * dy)
    return SquareRoot(dist * dist + dz * dz)
endfunction
Would that above function work? It's just a guess.
11-24-2006, 04:32 PM#2
Captain Griffen
(dx^2 + dy^2 + dz^2)^-1 gives the distance.

Polar project would be (ang1 is flat, ang2 is intersect with the flat plane):

x = x + dis * cos(ang1) * |cos(ang2)|
y = y + dis * sin(ang1) * |cos(ang2)|
z = z + dis * sin(ang2)

I think. Test it.
11-24-2006, 06:34 PM#3
moyack
Check this:
Zoom (requires log in)

Where o is the coordinate (0,0,0)

EDIT: the coordinate c is useless, sorry for that. In order to get more information and math related, go to this link:

http://www.csgnetwork.com/trigtriformulatables.html
Attached Images
File type: jpgClip_2.jpg (22.7 KB)
11-24-2006, 06:52 PM#4
wyrmlord
Thanks for the replies, I'm going to test them and make sure they work as I would like them to.

EDIT: The angle formula either doesn't work, or doesn't work for what I'm planning on using it for. First off, here's the function I made from it:
Collapse JASS:
function AngleBetweenPoints2 takes real x1, real y1, real z1, real x2, real y2, real z2 returns real
 local real A = DistanceBetweenPoints2(0,0,0,x1,y1,z1)
 local real B = DistanceBetweenPoints2(0,0,0,x2,y2,z2)
 local real C = DistanceBetweenPoints2(x1,y1,z1,x2,y2,z2)
 
    return 1. / Cos((A*A + B*B - C*C)/2*A*B)
endfunction
To test this, I found the angle between two points without a Z coordinate (0,0 and 6,6). Blizzard's function returned the correct angle, 45, while this function returned 1.414 (I don't remember exactly). However, the distance formula is working as I would like it to.

As for the Polar Project method, I wasn't having much success with my test, here's the function, just so you can double check:
Collapse JASS:
function PolarProject2 takes real x1, real y1, real z1, real distance, real angle1, real angle2 returns location
 local real x = x1 + distance * Cos(angle1) * Cos(angle2)
 local real y = y1 + distance * Sin(angle1) * Cos(angle2)
 local real z = z1 + distance * Sin(angle2)
    set udg_Z = z //Can't create a location with Z, so I store it in a global temporarily
    return Location(x,y)
endfunction

I tried the coordinate 0,0,0 and the distance was 10. The angle1 was 90 and the angle2 was 45. My result was (-2.354, 4.696, 8.509) and I'm pretty sure that's somewhat off, at least with the X coordinate. I tried it again, but with the angle1 being 45 and I got (2.76, 4.47, 8.509).

Perhaps I should explain somewhat of some things that I am trying to do first:
These would all be done with lines.
1. Given an x/y/z coordinate, radius, and an angle, create a circle, but curved so it isn't perpendicular to the Z axis all the time.
2. Given two points and an angle (for the curve), make a curved line (like making a half circle)
3. Given an x/y/z coordinate and a radius, create a sphere.
11-24-2006, 06:56 PM#5
SeasonsOfLove
Quote:
Originally Posted by Captain Griffen
(dx^2 + dy^2 + dz^2)^-1 gives the distance.

Polar project would be (ang1 is flat, ang2 is intersect with the flat plane):

x = x + dis * cos(ang1) * |cos(ang2)|
y = y + dis * sin(ang1) * |cos(ang2)|
z = z + dis * sin(ang2)

I think. Test it.

ang2 will only go from -90 to 90, but that should work.
Usually in math, the angle from the positive Z-axis is used, so I got confused for a second there.
11-24-2006, 08:28 PM#6
moyack
Cos-1 != 1/cos
Cos-1 is the inverse function to cos

Cos(alpha)=(A^2+B^2-C^2)/2AB
11-25-2006, 01:07 AM#7
wyrmlord
Could you explain what inverse is?
11-25-2006, 01:30 AM#8
moyack
Yes :)

if f(x) = y and f-1(y)=x then f-1 is the inverse of f

Because you are using JASS (I'm happy for that) Cos-1 is the function Acos
Ahh, and I forgot. all the values used in cos, sin, tan functions must be in radians, so if you want to get cos(45), you have to convert to radians, it would be cos(45*BJ_PI/180)
11-26-2006, 12:40 AM#9
PipeDream
Blizzard.j rather conveniently defines two constants "bj_DEGTORAD" and "bj_RADTODEG". In this case you want the former.

I'm not sure what you mean by the angle between two x,y,z points. You can have an angle between three points (at the intersection of two lines) or from one line to some fixed line- like y = 0 in the plane. In 3d however you'll need two angles to define a direction, just as you see two angles in griffen's polar projection (spherical -> cartesian) formula.
Fortunately I keep all the usual coordinate system changing formula taped to my wall. For cartesian -> spherical:

r = SquareRoot(dx*dx+dy*dy+dz*dz)
ang1 = Atan2(SquareRoot(dx*dx+dy*dy),dz)
ang2 = Atan2(dy,dx)
11-26-2006, 02:49 AM#10
wyrmlord
Basically I'd want to be drawing spheres and stuff like that, so I'd take the center x/y/z and then creating lightning effects every few degrees in a vertical circle, which would result in a sphere. Based on all the helpful feedback I've gotten, I should be able to do this.