Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How do I orientate a cylinder between two points  (Read 1586 times)
0 Members and 1 Guest are viewing this topic.
Offline rdlecler

Senior Newbie




Java games rock!


« Posted 2004-08-28 23:15:04 »

I need to draw a line (actually a cylinder) between two points. I know that the length of the clinder will just be the distance between the two points, and the center of the cylinder will be at the midpoint of the two points: ((x2-x1)/2, (y2-y1)/2, (z2-z1)/2); but how do I go about figuring out what the transformation matix will be. Sorry, I know this is basic 3d math stuff.

Thanks in advance,
RDL
Offline kevglass

JGO Kernel


Medals: 85
Projects: 25


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2004-08-29 07:36:51 »

You might be able to get away using Transform3D.lookAt() to create a transform thats looks from the centre point of the cylinder to one end.

Kev

Offline cweila2s

Senior Newbie




Java games rock!


« Reply #2 - Posted 2004-08-30 10:51:36 »

But what do you chose as Upper Vector? (0, 1, 0)? I use it the API way, lookAt(frompoint, topoint, upvector) and I'm getting bad results  Huh
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevglass

JGO Kernel


Medals: 85
Projects: 25


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #3 - Posted 2004-08-30 10:56:21 »

Your up vector should be any line thats at a tangent to the line between the two points.

Kev

Offline rdlecler

Senior Newbie




Java games rock!


« Reply #4 - Posted 2004-08-30 19:37:01 »

Any chance of anyone being a little more explicit on how to accomplish this whether using the lookAt function or just basic maths. I have been playing around with this and my Transform3D matrix ends up with NaN in the matrix or there doesn't seem to be any transformation going on at all when I try it.

I imagine this is a pretty straight forward procedure but my lack of graphics experience and a lack of understanding of the Java API is making this difficult to accomplish.

Thanks
RDL
Offline Orangy Tang

JGO Kernel


Medals: 51
Projects: 11


Monkey for a head


« Reply #5 - Posted 2004-08-30 19:46:57 »

Its been a while so I'm going to assume that the matrix lookAt takes 3 vectors, forward, up and right (or left, whatever).

Your forward vector is easy, its just the (normalised) vector from point A to point B. 'Up' can be arbitrary chosen depending on your world. Something like (0, 1, 0) perhaps. Then you can do a cross product between the 'forward' and 'up' vectors to get a 'right' vector. Then again cross your 'forward' with your new 'right' vector to get a proper up vector. Then plug those into your matrix. If you start with all vectors normalised beforehand you shouldn't need to re-normalise them again at the end.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline rdlecler

Senior Newbie




Java games rock!


« Reply #6 - Posted 2004-08-31 03:22:29 »

I tried the following (see function below) but when I setRotation on the Transform3D matrix of the object I get absolutly no change.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
      public static Matrix3f getRotationMatrixBetweenPoints(Vector3f p1, Vector3f p2){
            Transform3D t3d = new Transform3D();
            Vector3f midpoint = midPoint(p1, p2);
            System.out.println(t3d);
            t3d.lookAt(new Point3d(p1),
                        new Point3d(p2),
                        new Vector3d(0,1,0)
                        );
            System.out.println(t3d);
            Matrix3f transformationMatrix = new Matrix3f();
            t3d.get(transformationMatrix);
            return transformationMatrix;
      }


RDL



Offline misterX

Junior Member




java forever!


« Reply #7 - Posted 2004-08-31 07:47:43 »

...the code should work fine. Exept if the 'new Vector3d(0,1,0)' is in the same direction as p1 to p2 (or the opposite).
Offline rdlecler

Senior Newbie




Java games rock!


« Reply #8 - Posted 2004-09-01 04:28:52 »

I tried the lookAt function but I get the following translation matrix which has NaN numbers (?):

NaN, NaN, NaN, NaN
NaN, NaN, NaN, NaN
0.0, 1.0, 0.0, -1.0
0.0, 0.0, 0.0, 1.0

Point3d p1 = new Point3d(0,0,0);
Point3d p2 = new Point3d(0,1,0);
Vector3d mag = new Vector3f();
mag.sub(p2, p1);
Transform3D translate = new Transform3D();
Point3d eye = new Point3d(mag);
Point3d center = new Point3d(0,0,0);
translate.lookAt(eye, center, new Vector3d(0,1,0));

I am guesing I am getting this because there is some divide by 0s going on in the lookAt function and so this will be no good for me then?

Thanks
RDL


Offline misterX

Junior Member




java forever!


« Reply #9 - Posted 2004-09-01 10:04:29 »

Quote

Point3d p1 = new Point3d(0,0,0);
Point3d p2 = new Point3d(0,1,0);
Vector3d mag = new Vector3f();
mag.sub(p2, p1);
...
Point3d eye = new Point3d(mag);
Point3d center = new Point3d(0,0,0);
translate.lookAt(eye, center, new Vector3d(0,1,0));

Quote

...the code should work fine. Exept if the 'new Vector3d(0,1,0)' is in the same direction as p1 to p2 (or the opposite).

Here, it's the case. Your "from-to" vector is the same as your "up" vector: both (0,1,0)
They must not be aligned! ...and preferably perpendicular.
Try for example (0,0,1) as "up" vector and it'll be ok.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline rdlecler

Senior Newbie




Java games rock!


« Reply #10 - Posted 2004-09-02 04:58:07 »

misterX,

I don't know if you tried it out but it doesn't work for me. I did stop getting the NaNs in the rotation matrix which I changed the up axis, but the rotations still end up being off -- looking a lot like how they were off when I tried coding it myself.

For: (0,1,0) it was off by 90 on the x axis
For: (0,1,1) it was off by 90 again, being essentially perpendicular to the two points
For: (1,1,1) it was off by about 45 degrees

I did come accross this website which explains it in openGL but when I tried to implement it in Java it wouldn't work for me:

http://www.mfcogl.com/OpenGL%20-%20draw%20cylinder%20between%202%20pts.htm




Offline misterX

Junior Member




java forever!


« Reply #11 - Posted 2004-09-02 09:31:32 »

add t3d.invert(); after the t3d.lookAt(...);
Offline rdlecler

Senior Newbie




Java games rock!


« Reply #12 - Posted 2004-09-02 14:09:42 »

masterX,

Thanks but it still doesn't do the trick. It is still off by 90 degrees for the first two, and for the points: (0,0,0) & (1,1,1) is still off by 45 degrees, but with a 90 degree extra rotation as compared to one without the invert call on the transform

Best,
RDL
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (57 views)
2014-04-15 18:08:23

BurntPizza (55 views)
2014-04-15 03:46:01

UprightPath (68 views)
2014-04-14 17:39:50

UprightPath (51 views)
2014-04-14 17:35:47

Porlus (68 views)
2014-04-14 15:48:38

tom_mai78101 (93 views)
2014-04-10 04:04:31

BurntPizza (153 views)
2014-04-08 23:06:04

tom_mai78101 (249 views)
2014-04-05 13:34:39

trollwarrior1 (205 views)
2014-04-04 12:06:45

CJLetsGame (213 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!