How can i do this? (calculate where a line is pointing to)

hi together,



and again one question from me :wink:



i have drawn a lot of boxes. some of these boxes are connected together with a line using the following code:



for (int j = 0; j < boxes2draw.size(); j++) {
         center = boxes2draw.elementAt(j).getCenter();
         vertex[j] = new Vector3f(center.x, center.y + boxes2draw.elementAt(j).yExtent, center.z);
      }
      
      Line connectingLine = new Line("Connecting Line", vertex, null, null, null);
      connectingLine.setMode(Line.LOOP);



the boxes are saved in the vector boxes2draw. the boxes are unambigously named.

know my problem:

i want to point with the mouse on one point of the line. then the method should calculate which two boxes are directley connect with this point of the line. i don't know how to begin. i could use intersection with the line and all the boxes which are drawn but the problem is, the line could intersect a box even if its not conencted to the line, because it's the closest way to the specific box. any hints of you how to reach that?

one idea i had is to draw not one line but instead one separate line for every two box and name the uniquely. for example if there is box A, B and C to connect. draw 3 lines, named LineAB, LineBA, LineCA. Then i could get the boxes from the line name. but there have to be better ideas? right?

You could define a hashmap and use the boxes as keys and values ;).

Each box will be added twice to the hashmap, once as a key to another box and once as a value…





(so basically, when you needed a box that was connected to another one just take the first box and get the value (other box) its associated with; this will only work when a box can ONLY be attached to one other box, for a box that can be connected to 2 or more others each box will need its own hashmap I think, and then wrap this in other hashmap to get easy access to each box…)



Anything more complex (say: branch, branch, leaf) would probably require some sort of 'tree' structure.



(Maybe thats ALL wrong though, I'm still walking up here…)

yes every box is connected to two boxes.





A ---- B


C
D ---- ....

but the problem is. i want to physically click with the mouse somewhere on the line. and the the method should calculate which boxes are next to. Using the example above:


A
B --x-- C ---- D --- ...

if i click on x, the method should return B and C

OOOOHHHH!



That MUCH more complex… Okies, time for the KISS approach :wink:



Just set use individual lines (shouldn't be TOO much a drain unless you have 1000s of lines) and set the name you were talking about above, then just use mouse picking and get the name of the line‚Ķ  (Instead of using lines, maybe you should consider using cylinders, might be easier to see and pick‚Ķ)



YUP, pretty much your original idea anyways.  Another solution could involve using some line testing, and storing things to a hashmap, but you have to do picking for that anyways. So my suggestion basically is saying, sacrifice some performance for simplicity (and less memory consumption).