Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (427)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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  
  Rotation of an 3d point  (Read 1452 times)
0 Members and 1 Guest are viewing this topic.
Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Posted 2013-03-20 00: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.
Offline Agro
« Reply #1 - Posted 2013-03-20 00:06:22 »

This can help:

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

Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #2 - Posted 2013-03-20 00: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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Agro
« Reply #3 - Posted 2013-03-20 00:11:37 »

Matrix multiplication. It shouldn't be that hard.

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



I think.

Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #4 - Posted 2013-03-20 00: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 Smiley
Offline Agro
« Reply #5 - Posted 2013-03-20 00: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.

Online Roquen
« Reply #6 - Posted 2013-03-20 07: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.
Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #7 - Posted 2013-03-20 10: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.
Online Roquen
« Reply #8 - Posted 2013-03-20 11: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.
Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #9 - Posted 2013-03-20 12: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 Cheesy

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Roquen
« Reply #10 - Posted 2013-03-20 13: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.
Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #11 - Posted 2013-03-20 14: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?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 612
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2013-03-20 14: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
Online Roquen
« Reply #13 - Posted 2013-03-20 14:53:52 »

Missing copying the src to dst of unrotated component.
Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #14 - Posted 2013-03-20 15: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 Cheesy


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

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #15 - Posted 2013-03-20 15: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.

 

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 (78 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (221 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!