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

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 trollwarrior1 (33 views) 2014-11-22 12:13:56 xFryIx (73 views) 2014-11-13 12:34:49 digdugdiggy (52 views) 2014-11-12 21:11:50 digdugdiggy (46 views) 2014-11-12 21:10:15 digdugdiggy (40 views) 2014-11-12 21:09:33 kovacsa (65 views) 2014-11-07 19:57:14 TehJavaDev (70 views) 2014-11-03 22:04:50 BurntPizza (68 views) 2014-11-03 18:54:52 moogie (83 views) 2014-11-03 06:22:04 CopyableCougar4 (82 views) 2014-11-01 23:36:41
 basil_ 29x theagentd 28x HeroesGraveDev 25x BurntPizza 20x Riven 19x Spasi 18x princec 15x KevinWorkman 15x SHC 12x gouessej 11x kpars 10x LiquidNitrogen 10x Grunnt 10x Gibbo3771 10x CopyableCougar4 9x Nate 9x
 Understanding 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:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06
 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