Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
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  
  Rotation3D  (Read 1250 times)
0 Members and 1 Guest are viewing this topic.
Online trollwarrior1
« Posted 2013-11-25 15:47:22 »

I just though somebody might find this useful! Cheesy

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  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
public class Rotation3D {

   public static vec3 point(vec3 center, vec3 point, float xr, float yr, float zr) {
     

      vec3 p = point.copy();
     
      p = Rotation3D.rotateX(center, p, xr);
      p = Rotation3D.rotateY(center, p, yr);
      p = Rotation3D.rotateZ(center, p, zr);
     
     
      return p;
   }

   public static vec3 rotateX(vec3 center, vec3 point, float xr) {
     
      float xrot = (float) Math.toRadians(xr);
     
      float px = point.x;
      float py = point.y;
      float pz = point.z;
     
      float x = px;
      float y = (float) (Math.cos(xrot) * py - Math.sin(xrot) * pz);
      float z = (float) (Math.sin(xrot) * py + Math.cos(xrot) * pz);
     
      return new vec3(x, y, z);
   }
   
   public static vec3 rotateY(vec3 center, vec3 point, float yr) {
     
      float yrot = (float) Math.toRadians(yr);
     
      float px = point.x;
      float py = point.y;
      float pz = point.z;
     
      float x = (float) (Math.cos(yrot) * px + Math.sin(yrot) * pz);
      float y = (float) py;
      float z = (float) (-Math.sin(yrot) * px + Math.cos(yrot) * pz);
     
      return new vec3(x, y, z);
   }
   
   public static vec3 rotateZ(vec3 center, vec3 point, float zr) {
     
      float zrot = -(float) Math.toRadians(zr);
     
      float px = point.x;
      float py = point.y;
      float pz = point.z;
     
      float x = (float) (Math.cos(zrot) * px - Math.sin(zrot) * py);
      float y = (float) (Math.sin(zrot) * px + Math.cos(zrot) * py);
      float z = pz;
     
      return new vec3(x, y, z);
     
   }
   
}


I have done this using wikipedia on rotation matrices.

PS

vec3 is just a class which stores xyz floats
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2013-11-26 00:50:11 »

And then rotate around an arbitrary axis!

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  
public Vector3 rotate(float angle, float x, float y, float z) {
   float cos = (float)Math.cos(angle);
   float sin = (float)Math.sin(angle);
   float oneMinusCos = 1 - cos;
   
   float len = (float)Math.sqrt(x * x + y * y + z * z);
   x /= len;
   y /= len;
   z /= len;
   
   float mat00 = x * x * oneMinusCos + cos;
   float mat10 = x * y * oneMinusCos - z * sin;
   float mat20 = x * z * oneMinusCos + y * sin;
   
   float mat01 = y * x * oneMinusCos + z * sin;
   float mat11 = y * y * oneMinusCos + cos;
   float mat21 = y * z * oneMinusCos - x * sin;
   
   float mat02 = z * x * oneMinusCos - y * sin;
   float mat12 = z * y * oneMinusCos + x * sin;
   float mat22 = z * z * oneMinusCos  + cos;
   
   return new Vector3(x * mat00 + y * mat10 + z * mat20,
                      x * mat01 + y * mat11 + z * mat21,
                      x * mat02 + y * mat12 + z * mat22);
}

Online trollwarrior1
« Reply #2 - Posted 2013-11-26 11:37:13 »

Oops I forgot to include the center into the calculations Tongue
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.

Riven (7 views)
2014-10-02 14:36:20

Pippogeek (41 views)
2014-09-24 16:13:29

Pippogeek (32 views)
2014-09-24 16:12:22

Pippogeek (22 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (30 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (44 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!