Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (151) games submitted by our members Games in WIP (605) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Rotation3D  (Read 1411 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: 367
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.

 theagentd (11 views) 2015-03-27 23:08:20 wxwsk8er (52 views) 2015-03-20 15:39:46 Fairy Tailz (46 views) 2015-03-15 21:52:20 Olo (28 views) 2015-03-13 17:51:59 Olo (30 views) 2015-03-13 17:50:51 Olo (35 views) 2015-03-13 17:50:16 Olo (41 views) 2015-03-13 17:47:07 ClaasJG (40 views) 2015-03-10 11:36:42 ClaasJG (41 views) 2015-03-10 11:33:01 Pippogeek (47 views) 2015-03-05 14:36:23
 LiquidNitrogen 23x BurntPizza 21x KevinWorkman 21x basil_ 20x EgonOlsen 17x theagentd 16x Roquen 16x princec 12x Riven 12x Varkas 10x SHC 9x Ashedragon 8x NegativeZero 8x Ecumene 7x nsigma 7x wessles 7x
 How 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:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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