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 (654) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Rotation3D  (Read 1614 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: 389
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

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

 bilznatch (24 views) 2015-08-04 11:03:17 SHC (40 views) 2015-08-01 03:58:20 Jesse (24 views) 2015-07-29 04:35:27 Riven (46 views) 2015-07-27 16:38:00 Riven (25 views) 2015-07-27 15:35:20 Riven (27 views) 2015-07-27 12:26:13 Riven (18 views) 2015-07-27 12:23:39 BurntPizza (41 views) 2015-07-25 00:14:37 BurntPizza (54 views) 2015-07-24 22:06:39 BurntPizza (34 views) 2015-07-24 06:06:53
 theagentd 50x wessles 49x basil_ 33x KaiHH 26x Riven 21x orangepascal 18x mooman219 17x bornander 16x ags1 16x KudoDEV 13x CelestialCreator 11x princec 11x klaus 11x pquiring 11x israelg99 9x Jesse 9x
 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