1

Ok, I know this sounds really daft to be asking here, but it is programming related.

I'm working on a game, and I'm thinking of implementing a system that allows users to triangulate their 3D coordinates to locate something (eg for a task). I also want to be able to let the user make the coordinates of the points they are using for triangulation have user-determined coordinates (so the location's coordinate is relative, probably by setting up a beacon or something).

I have a method in place for calculating the distance between the points, so essentially I can calculate the lengths of the sides of the triangle/pyramid as well as all but the coordinate I am after. It has been a long time since I have done any trigonometry and I am rusty with the sin, cos and tan functions, I have a feeling they are required but have no clue how to implement them.

Can anyone give me a demonstration as to how I would go about doing this in a mathematical/programatical way?

extra info: My function returns the exact distance between the two points, so say you set two points to 0,0,0 and 4,4,0 respectively, and those points are set to scale(the game world is divided into a very large 3d grid, with each 'block' area being represented by a 3d coordinate) then it would give back a value at around 5.6.

The key point about it varying is that the user can set the points, so say they set a point to read 0,0,0, the actual location could be something like 52, 85, 93. However, providing they then count the blocks and set their other points correctly (eg, set a point 4,4,0 at the real point 56, 89, 93) then the final result will return the relative position (eg the object they are trying to locate is at real point 152, 185, 93, it will return the relative value 100,100,0). I need to be able to calculate it knowing every point but the one it's trying to locate, as well as the distances between all points.

Also, please don't ask why I can't just calculate it by using the real coordinates, I'm hoping to show the equation up on screen as it calculates the result.7

Example: Here is a diagram Example Diagram

Imagine these are points in my game on a flat plain. I want to know the point f. I know the values of points d and e, and the sides A,B and C.

Using only the data I know, I need to find out how to do this.

Answered Edit:

After many days of working on this, Sean Kenny has provided me with his time, patience and intellect, and thus I have now got a working implementation of a triangulation method.

I hope to place the different language equivalents of the code as I test them so that future coders may use this code and not have the same problem I have had.

Pharap
  • 3,826
  • 5
  • 37
  • 51
  • Can you add in the code of the method please –  Aug 06 '12 at 11:56
  • Ok, the method you have in place: does this determine the lengths to the next coordinate from the current one? Is there a clearly defined origin? –  Aug 06 '12 at 12:09
  • I will edit my post to include more info. – Pharap Aug 06 '12 at 12:21
  • With your extra information, I think my answer should provide what you need. If not, let me know. –  Aug 06 '12 at 12:30
  • Is the triangle above always a right triangle? If so I have all the trig figured out. If not, I have to go look up the law of cosines. – bigbenbt Aug 06 '12 at 15:55
  • Since right angle triangle are a specific example of a more general case, it's best to be more general in this case. –  Aug 06 '12 at 16:33
  • 1
    Person who gave a downvote, please give a reason as to why. –  Aug 06 '12 at 22:33

2 Answers2

4

I spent a bit of time working on a solution but I think the implementer, i.e you, should know what it's doing, so any errors encountered can be tackled later on. As such, I'll give my answer in the form of strong hints.

First off, we have a vector from d to e which we can work out: if we consider the coordinates as position vectors rather than absolute coordinates, how can we determine what the vector pointing from d to e is? Think about how you would determine the displacement you had moved if you only knew where you started and where you ended up? Displacement is a straight line, point A to B, no deviation, not: I had to walk around that house so I walked further. A straight line. If you started at the point (0,0) it would be easy.

Secondly, the cosine rule. Do you know what it is? If not, read up on it. How can we rearrange the form given in the link to find the angle d between vectors DE and DF? Remember you need the angle, not a function of the angle (cos is a function remember).

Next we can use a vector 'trick' called the scalar product. Notice there is a cos function in there. Now, you may be thinking, we've just found the angle, why are we doing it again?

Define DQ = [1,0]. DQ is a vector of length 1, a unit vector, along the x-axis. Which other vector do we know? Do we know of two position vectors?

Once we have two vectors (I hope you worked out the other one) we can use the scalar product to find the angle; again, just the angle, not a function of it.

Now, hopefully, we have 2 angles. Could we take one from the other to get yet another angle to our desired coordinate DF? The choice of using a unit vector earlier was not arbitrary.

The scalar product, after some cancelling, gives us this : cos(theta) = x / r Where x is the x ordinate for F and r is the length of side A.

The end result being:

theta = arccos( xe / B ) - arccos( ( (A^2) + (B^2) - (C^2) ) / ( 2*A*B ) ) 

Where theta is the angle formed between a unit vector along the line y = 0 where the origin is at point d.

With this information we can find the x and y coordinates of point f relative to d. How? Again, with the scalar product. The rest is fairly easy, so I'll give it to you.

x = r.cos(theta)

y = r.sin(theta)

From basic trigonometry.

I wouldn't advise trying to code this into one value.

Instead, try this:

//pseudo code
dx = 0
dy = 0  //initialise coordinates somehow
ex = ex
ey = ey
A = A
B = B
C = C

cosd = ex / B
cosfi = ((A^2) + (B^2) - (C^2)) / ( 2*A*B)
d = acos(cosd)      //acos is a method in java.math 
fi = acos(cosfi)   //you will have to find an equivalent in your chosen language
                  //look for a method of inverse cos
theta = fi - d

x = A cos(theta)
y = A sin(theta)

Initialise all variables as those which can take decimals. e.g float or double in Java.

Imgur

The green along the x-axis represents the x ordinate of f, and the purple the y ordinate.
The blue angle is the one we are trying to find because, hopefully you can see, we can then use simple trig to work out x and y, given that we know the length of the hypotenuse. This yellow line up to 1 is the unit vector for which scalar products are taken, this runs along the x-axis.

We need to find the black and red angles so we can deduce the blue angle by simple subtraction.

Hope this helps. Extensions can be made to 3D, all the vector functions work basically the same for 3D.

  • Ok, I understood the first step. I understand that cosin is used to find values, and it can be inverted to find angles. The resource on the scalar product wasn't overly useful. I see what the equation is, but I don't know what it does. – Pharap Aug 06 '12 at 22:03
  • The scalar product relates the two vectors by an angle. If you imagine two lines in 2D, or 3D, there is no possible configuration that they do not lie in the same plane, not necessarily an axis plane. The scalar product can be used to determine that angle between them. –  Aug 06 '12 at 22:12
  • Right, so it determines the angle between two lines. Taking the lengths of the lines as arguments? So I can calulate the angles but I still don't get how I use the angles to find the coords of the missing point. (Sorry I'm a bit slow on the uptake, by last 2 maths teachers were useless, and I had one for two years. In total I haven't done any proper maths in 4 years. – Pharap Aug 06 '12 at 22:43
  • I'll try and draw a diagram and add it in. –  Aug 06 '12 at 23:06
  • @Pharap see my edit. Hope this helps clarify. Sorry it's not particularly clear. –  Aug 06 '12 at 23:37
  • Ok, I could probably understand it better if you used less words like position vector, vector and scalar product. Sorry for the slow reply but I have been very occupied as of late and only just got chance to have a proper look into this. I now remember how to do sin/cos and asin/acos. I understand the majority of your script, giving it to me in whatever language you know of is fine (though my main are VB,C#,C++ and Lua, Java is fine too, even though I don't know it). The only bits I don't understand is which angles you are denoting as A,B,C,E and F, and also why the fi? – Pharap Aug 08 '12 at 05:06
  • The diagram you provided in question gives the points d, e and f. Angles d, e and f are the angles closest to those points. fi is the angle between the x-axis and the line from d to e. [This](http://tutorial.math.lamar.edu/Classes/CalcII/Vectors_Basics.aspx) might help. –  Aug 08 '12 at 08:14
  • So the capitals A,B and C in the diagram are sides and the capitals E,D and F are angles? – Pharap Aug 08 '12 at 14:45
  • In your diagram, the lowercase d, e and f are angles, the uppercase A, B and C are side lengths. It was D,E and F in my answer due to error, sorry. I'll edit that. –  Aug 08 '12 at 15:14
  • OK, after all is said and done, I finally managed to understand it and utilise it and (providing I do a bit of rounding at the end to iron out some discrepancies) it does work. So I'm going to accept this as my answer (though I may reword it later so future viewers who have more of a cheese brain than I do can understand what's going on. At any rate, thank you for your time and patience, I generally understand what's going on and it works, so as long as it works, that's all that matters. – Pharap Aug 09 '12 at 03:37
0

If you have the displacements from an origin, regardless of whether this is another user defined coordinate or not, the coordinate for that 3D point are simply (x, y, z).

If you are defining these lengths from a point, which also has a coordinate to take into account, you can simply write (x, y, z) + (x1, y1, z1) = (x2, y2, z2) where x2, y2 and z2 are the displacements from the (0, 0, 0) origin.

If you wish to find the length of this vector, i.e if you defined the line from A to B to be the x axis, what would the x displacement be, you can use Pythagoras for 3D vectors, it works just the same as with 2D:

Length l = sqrt((x^2) + (y^2) + (z^2))

EDIT: Say you have a user defined point A (x1, y1, z1) and you want to define this as the origin (0,0,0). You have another user chosen point B (x2, y2, z2) and you know the distance from A to B in the x, y and z plane. If you want to work out what this point is, in relation to the new origin, you can simply do

B relative to A = (x2, y2, z2) - (x1, y1, z1) = (x2-x1, y2-y1, z2-z1) = C

C is the vector A>B, a vector is a quantity which has a magnitude (the length of the lines) and a direction (the angle from A which points to B).

If you want to work out the position of B relative to the origin O, you can do the opposite:

 B relative to O = (x2, y2, z2) + (x1, y1, z1) = (x1+x2, y1+y2, z1+z2) = D

D is the vector O>B.

Edit 2:

//pseudo code
userx = x;
usery = y;
userz = z; 
//move origin
for (every block i){
 xi = xi-x;
 yi = yi - y;
 zi = zi -z;
}
  • I now explain in my post one of the reasons why I don't want to use the real coordinates. Also, I can read VB, C#, C++ and Lua, so any of those would be handy, and as little dependency on built in functions (other than the maths ones) would also be nice. I can probably get my head around a maths equation if it's clearly explained in detail. It has been over a year since I did any proper maths, and I have long forgotten all geometry theorems (aside from pythagoras' theorem, which only works for right angle triangles). – Pharap Aug 06 '12 at 12:30
  • Pythagoras' theorem, when extended, is what is used for vector modulus calculations in extended dimensions. Are you trying to effectively move the origin to be centred on the user defined block? –  Aug 06 '12 at 12:38
  • You lost me at vector. Don't bother with the big words, just explain in an insanely dumbed down way. I'm trying to get the target point's location using user defined points and the distance between points. I know all the distances between the points and all the user defined points but the one that is trying to be located. – Pharap Aug 06 '12 at 12:52
  • See my edit. Please tell me what you are struggling with if this isn't clear. –  Aug 06 '12 at 13:01
  • The code on the last bit was making a bit more sense, but I can't quite figure out what it does in a situation. I get the processing, just can't imagine it with real values. – Pharap Aug 06 '12 at 13:39
  • Oh, I do apologise, I was misconstruing the question, I will have to do a rewrite. –  Aug 06 '12 at 14:55