Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (604) Games in Android Showcase (171) games submitted by our members Games in WIP (653) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Rotation of an 3d point  (Read 2945 times) 0 Members and 1 Guest are viewing this topic.
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Posted 2013-03-19 23:05:05 »

Hello, im having another problem, with math this time.

Im having the following problem:

I want to rotate certain parts of an model at creation to give an random look.
However this involves the rotation of 3d points, and im not really sure how to handle this.
Rotation in one way was easy, but 3D seems very complicated (i dont really get the explanations on the internet).

Basicly im looking for an code representation of gl.glRotatef(rotate, 0f, 0f, 1f), so i can calculate these points at creation.
Does anyone have some retard proof explanation / sample or simply some resource to get an implementation from?
Im trying to solve most math problems myself, but im just clueless about 3d rotations.

Any help would be greatly appriciated.
Agro
 « Reply #1 - Posted 2013-03-19 23:06:22 »

This can help:

http://en.wikipedia.org/wiki/Rotation_matrix

RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #2 - Posted 2013-03-19 23:09:50 »

Thank you, but this is what i ment as complicated explanation.

I maybe would be able to implement this with a lot of effort, but then i end up with matrixes like these:

Im not really sure how i can use these to move an point.

Edit:

The rotation matrix i would need would be this one:

Is there some equalation what would use an Vector3f as input as input and output?
Agro
 « Reply #3 - Posted 2013-03-19 23:11:37 »

Matrix multiplication. It shouldn't be that hard.

Also, if you have a 3D point, multiply it by these matrices:

I think.

RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #4 - Posted 2013-03-19 23:14:37 »

Yeha, but how does this multiplication work?
I see 9 variables within the blocks, and im not really sure how i should use them in combination with eachother.

Sorry to bother you so much
Agro
 « Reply #5 - Posted 2013-03-19 23:21:38 »

Alright, I got it. Matrix multiplication is kind of hard to explain, but here is a link:

http://en.wikipedia.org/wiki/Matrix_multiplication

I just learned that this is a better source for 3d rotations: http://www.cprogramming.com/tutorial/3d/rotationMatrices.html

How it works is that you take all the rotation matrices and and put the angle you want to rotate. this will result in a final rotation matrix, then multiply that matrix with your 3D point matrix(vector). Make sure the order is correct.

Roquen
 « Reply #6 - Posted 2013-03-20 06:54:50 »

A better description of your problem would be handy.  Using matrices when you don't know them might be like attempting to use a pile-driver to drive a nail.  Maybe you just need a hammer.
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #7 - Posted 2013-03-20 09:26:19 »

Oke ill try to explain my problem better.

Im drawing some terrain with self drawn trees (dont laugh):

I already added randomness with color, size and texture coords, but i also want to add some rotation randomness.
Im building the tree with some segments, wich i would like to be able to rotate in 3 axis.
Exterragated effect:

I could just call glRotatef(rotate, 0f, 0f, 1f) before every segment, but that would not be efficient, and then i wont be able to keep groups of trees in a single vbo, since then i would need to render every segment alone.
Roquen
 « Reply #8 - Posted 2013-03-20 10:36:36 »

OK. So basically you want an animation hierarchy, which in this special case you want to add some procedurally generated random rotations at each joint to make identical models look slightly different.  In addition to rotations scaling effects (among others) would be handy.  Building or finding a library for animation hierarchies is the way I would go.  Additionally with this is place you could augment your system to support things like wind based effects.  Ultimately quaternions fit the problem much better than matrices.
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #9 - Posted 2013-03-20 11:13:01 »

OK. So basically you want an animation hierarchy, which in this special case you want to add some procedurally generated random rotations at each joint to make identical models look slightly different.  In addition to rotations scaling effects (among others) would be handy.  Building or finding a library for animation hierarchies is the way I would go.  Additionally with this is place you could augment your system to support things like wind based effects.  Ultimately quaternions fit the problem much better than matrices.

Exactly, so my problem is now building this library wih rotation / scaling.
I was also thinking about wind effects and maybe falling trees, so were on one line

Scaling is really easy, but im having no clue about rotations.
1 dimensional rotation is simple:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20 `    //Calculates 4 points of rectangle    public static Vector2f[] rotate(Vector4f src, float rotation){        Vector2f[] dst = new Vector2f[4];                float sin = sinf(rotation);        float cos = cosf(rotation);        float wsin = sin * (src.z / 2);        float wcos = cos * (src.z / 2);        float hsin = sin * (src.w / 2);        float hcos = cos * (src.w / 2);        //x' = x*cos(t) - y*sin(t)        //y' = x*sin(t) + y*cos(t)                dst[0] = new Vector2f(src.x + wcos - hsin, src.y + hcos + wsin);        dst[1] = new Vector2f(src.x - wcos - hsin, src.y + hcos - wsin);        dst[2] = new Vector2f(src.x + wcos + hsin, src.y - hcos + wsin);        dst[3] = new Vector2f(src.x - wcos + hsin, src.y - hcos - wsin);                return dst;    }`

but now the problem is creating an functon like this:
 1  2  3 `public static Vector3f rotate(Vector3f src, Vector3f point, Vector3f rotation){    //Rotate point around src with some math magic}`

quaternions and matrices sounds good, and looks like it is an solution, bu i have no clue about how to use them.
I have tryd to understand them on wikipedia or math tutorials, but im really clueless how to use them inside an function.
Basicly im just trying to find out how create this function (in java and / or glsl(wind effect) ).

If there is some precreated code somewhere, its fine to, reverse enginering the solution would still give me the information i need.
Roquen
 « Reply #10 - Posted 2013-03-20 12:36:02 »

Apparently I can't even explain complex numbers in a manner that's understandable, but I'll start a wiki page for quaternions...I guess I'm a glutton for wasting time.
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #11 - Posted 2013-03-20 13:33:01 »

Apparently I can't even explain complex numbers in a manner that's understandable, but I'll start a wiki page for quaternions...I guess I'm a glutton for wasting time.

Yeah im sorry, your article seemed very good, but i dont really understand how to use them.
So there is'nt an basic solution for this problem?
Riven
« League of Dukes »

« JGO Overlord »

Medals: 1019
Projects: 4
Exp: 16 years

 « Reply #12 - Posted 2013-03-20 13:39:05 »

As he was originally* looking for axis-aligned rotations around the origin... I think this (untested) code solves his problem quite well. (* before feature creep started)

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34 `public class Rotation {   private float cos, sin; // this is secretly a 'complex number', woohoo!   public Rotation() {      cos = 1.0f;      sin = 0.0f;   }   public void setAngle(double radians) {      cos = (float) Math.cos(radians);      sin = (float) Math.sin(radians);   }   public void rotateAroundX(Vec3 src, Vec3 dst) {      float y = src.y, z = src.z;      dst.x = src.x;      dst.y = cos * y - sin * z;      dst.z = sin * y + cos * z;   }   public void rotateAroundY(Vec3 src, Vec3 dst) {      float x = src.x, z = src.z;      dst.x = cos * x - sin * z;      dst.y = src.y;      dst.z = sin * x + cos * z;   }   public void rotateAroundZ(Vec3 src, Vec3 dst) {      float x = src.x, y = src.y;      dst.x = cos * x - sin * y;      dst.y = sin * x + cos * y;      dst.z = src.z;   }}`

Hi, appreciate more people! Î£ â™¥ = Â¾
Learn how to award medals... and work your way up the social rankings!
Roquen
 « Reply #13 - Posted 2013-03-20 13:53:52 »

Missing copying the src to dst of unrotated component.
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #14 - Posted 2013-03-20 14:11:24 »

Awsome!
I did not know it would be this simple (using 1d rotation on 3 axis).
Perfect, this saves me a lot of headache with i would have with those matrix calculations.
Now i can continue my tree coding

Also many thanks to you Roquen an Agro to help with my noobish problem.
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #15 - Posted 2013-03-20 14:38:35 »

Just used your code, and it works perfect so far in all 3 rotations.

Used this code to combine your code, until i feel the need to attempt to optimize this:

 1  2  3  4  5  6  7  8  9 `public static Vector3f rotate(Vector3f src, Vector3f point, Vector3f rotation){        Vector3f use = Vector3f.sub(point, src, null);                use = rotateAroundX(use, rotation.x);        use = rotateAroundY(use, rotation.y);        use = rotateAroundZ(use, rotation.z);                return Vector3f.add(use, src, null);    }`

Thanks so much again, this is really good.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 SHC (26 views) 2015-08-01 03:58:20 Jesse (19 views) 2015-07-29 04:35:27 Riven (40 views) 2015-07-27 16:38:00 Riven (21 views) 2015-07-27 15:35:20 Riven (24 views) 2015-07-27 12:26:13 Riven (14 views) 2015-07-27 12:23:39 BurntPizza (36 views) 2015-07-25 00:14:37 BurntPizza (46 views) 2015-07-24 22:06:39 BurntPizza (30 views) 2015-07-24 06:06:53 NoxInc (37 views) 2015-07-22 22:16:53
 theagentd 52x wessles 48x basil_ 35x KaiHH 26x orangepascal 25x ags1 21x Riven 19x mooman219 17x bornander 14x KudoDEV 13x CelestialCreator 11x princec 11x Jesse 11x klaus 10x pquiring 9x chrislo27 8x
 List of Learning Resourcesby gouessej2015-07-09 11:29:36How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21
 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