Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (542) Games in Android Showcase (133) games submitted by our members Games in WIP (604) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 How do I orientate a cylinder between two points  (Read 1737 times) 0 Members and 1 Guest are viewing this topic.
rdlecler

Senior Newbie

Java games rock!

 « Posted 2004-08-28 21: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.

RDL
kevglass

« JGO Spiffy Duke »

Medals: 221
Projects: 24
Exp: 18 years

Coder, Trainee Pixel Artist, Game Reviewer

 « Reply #1 - Posted 2004-08-29 05: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

cweila2s

Senior Newbie

Java games rock!

 « Reply #2 - Posted 2004-08-30 08: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
kevglass

« JGO Spiffy Duke »

Medals: 221
Projects: 24
Exp: 18 years

Coder, Trainee Pixel Artist, Game Reviewer

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

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

Kev

rdlecler

Senior Newbie

Java games rock!

 « Reply #4 - Posted 2004-08-30 17: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
Orangy Tang

JGO Kernel

Medals: 56
Projects: 11

 « Reply #5 - Posted 2004-08-30 17: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 ]
rdlecler

Senior Newbie

Java games rock!

 « Reply #6 - Posted 2004-08-31 01: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

misterX

Junior Devvie

java forever!

 « Reply #7 - Posted 2004-08-31 05: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).
rdlecler

Senior Newbie

Java games rock!

 « Reply #8 - Posted 2004-09-01 02: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

misterX

Junior Devvie

java forever!

 « Reply #9 - Posted 2004-09-01 08: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.
rdlecler

Senior Newbie

Java games rock!

 « Reply #10 - Posted 2004-09-02 02: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

misterX

Junior Devvie

java forever!

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

rdlecler

Senior Newbie

Java games rock!

 « Reply #12 - Posted 2004-09-02 12: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.
 Elsealabs (11 views) 2014-12-28 10:39:27 CopyableCougar4 (17 views) 2014-12-28 02:10:29 BurntPizza (21 views) 2014-12-27 22:38:51 Mr.CodeIt (14 views) 2014-12-27 04:03:04 TheDudeFromCI (19 views) 2014-12-27 02:14:49 Mr.CodeIt (26 views) 2014-12-23 03:34:11 rwatson462 (57 views) 2014-12-15 09:26:44 Mr.CodeIt (47 views) 2014-12-14 19:50:38 BurntPizza (94 views) 2014-12-09 22:41:13 BurntPizza (115 views) 2014-12-08 04:46:31
 Rayvolution 50x BurntPizza 30x basil_ 29x HeroesGraveDev 25x LiquidNitrogen 22x appel 18x kpars 18x KevinWorkman 15x Riven 15x princec 14x NegativeZero 13x pitbuller 13x gouessej 13x KudoDEV 12x kevglass 11x SHC 11x
 How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50
 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