Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (843) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Rotation3D  (Read 4347 times) 0 Members and 1 Guest are viewing this topic.
trollwarrior1
 « Posted 2013-11-25 15:47:22 »

I just though somebody might find this useful!

 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
ra4king

JGO Kernel

Medals: 508
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);}`

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

Oops I forgot to include the center into the calculations
Pages: [1]
 ignore  |  Print

 EgonOlsen (42 views) 2018-06-10 19:43:48 EgonOlsen (22 views) 2018-06-10 19:43:44 EgonOlsen (43 views) 2018-06-10 19:43:20 DesertCoockie (197 views) 2018-05-13 18:23:11 nelsongames (124 views) 2018-04-24 18:15:36 nelsongames (123 views) 2018-04-24 18:14:32 ivj94 (863 views) 2018-03-24 14:47:39 ivj94 (124 views) 2018-03-24 14:46:31 ivj94 (768 views) 2018-03-24 14:43:53 Solater (140 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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