Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (603) Games in Android Showcase (171) games submitted by our members Games in WIP (652) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Calculating the new x position  (Read 2526 times) 0 Members and 1 Guest are viewing this topic.
lcass
 « Posted 2013-11-02 15:52:07 »

Hi ive been looking on the internet for a post from 1999 about 3d graphics algorithms if anyone could link to that it would be great and would solve the entire issue.

here is the issue.
Say I have my camera and x1 y1 z1 and i have a vector (just a pixel) and x2 y2 z2. How would I figure out the position I draw the x and y if Im drawing it orthagonally?
lcass
 « Reply #1 - Posted 2013-11-02 15:56:10 »

Just to note the website wasnt about java it was just about the maths behind it, thats all im looking for I can implement the code.
Opiop
 « Reply #2 - Posted 2013-11-02 16:21:06 »

You mean centering the camera on a pixel? I don't understand.

lcass
 « Reply #3 - Posted 2013-11-02 16:25:46 »

Its for 3d programming. I want to find out how to draw the pixel via the cameras perspective.
lcass
 « Reply #4 - Posted 2013-11-02 16:39:00 »

So really the question should be , how do I get the x and y position the pixel should be drawn in based off of the distance from the object and the objects position(x,y)
Opiop
 « Reply #5 - Posted 2013-11-02 20:50:41 »

You mean the distance formula?
http://cs.selu.edu/~rbyrd/math/distance/

lcass
 « Reply #6 - Posted 2013-11-02 22:01:14 »

Nope.
What I mean is say the camera is at x 0 y 0 z 0 and I have an object at x 5 , y 2 z 15 . I want to know how to calculate the position I would draw the object relative to the camera using the coordinates. Note its for 3D movement.
Opiop
 « Reply #7 - Posted 2013-11-03 01:13:18 »

Ahh I see! If I remember correctly, use this formula:
X = cam.x + radius * cos(rotation.x) * sin(rotation.y)
Y = cam.y + radius * sin(rotation.x)  *  sin(rotation.y)
Z = cam.z +radius * cos(rotation.y)

Please note that the rotation vector should be in radians. Multiply your degrees by PI / 180.

relminator
 « Reply #8 - Posted 2013-11-03 02:36:45 »

I'm not so sure but do you mean "projection"?

If that's the case, divide the other 2 components by z(or distance).

Here's how it works:

www.rel.phatcode.net/mytutes/3dtutes/chapter1/chapter1.htm

A more in-depth explanation written my a friend of mine:

www.ocf.berkeley.edu/~horie/persp.txt
lcass
 « Reply #9 - Posted 2013-11-03 07:50:59 »

I'm not so sure but do you mean "projection"?

If that's the case, divide the other 2 components by z(or distance).

Here's how it works:

www.rel.phatcode.net/mytutes/3dtutes/chapter1/chapter1.htm

A more in-depth explanation written my a friend of mine:

www.ocf.berkeley.edu/~horie/persp.txt
When I say pojection I mean orthagonally projected from 3d to 2d.
Also thanks opiop you have even given me the rotation part
lcass
 « Reply #10 - Posted 2013-11-03 10:49:56 »

 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 `package lcass.com.graphics;import lcass.com.control.Camera;import lcass.com.core.core;import lcass.com.graphics.objects.vertex;public class RENDER3D {   private core core;   public RENDER3D(core core) {      this.core = core;   }   public void drawvertex(vertex v, Camera c,double rotx,double roty) {         double cz = c.z;      double dist = v.z - cz;      double ax = ((v.x - c.x)+dist) * Math.cos(rotx)* Math.sin(roty);      double ay = ((v.y - c.y)+dist) * Math.sin(rotx)* Math.sin(roty);      double dx = 0;      double dy = 0;      double scale = 512 / dist;            if (dist > 0) {         dx = (ax * (scale / dist)) + (core.WIDTH / 2);         dy = (ay * (scale / dist)) + (core.HEIGHT / 2);      }      System.out.println("dx " + ax + " dy " + dy);      if (dx < core.WIDTH && dy < core.HEIGHT) {         if (dx > 0 && dy > 0) {            core.screen.map[(int) dx][(int) dy] = 0xFF00FF;         }      }   }}`

I currently have that it behaves a bit odd though and im not to sure on where I would implement the z variable.
the prefix d is for drawn
prefix v is for vertex
prefix c is for camera
Opiop
 « Reply #11 - Posted 2013-11-03 11:00:55 »

Did you make sure your rotation is converted to radians? That distance variable seems a little sketchy, too because you have it clamped to under zero, the radius really should be positive!

How are you having trouble with the z component?

lcass
 « Reply #12 - Posted 2013-11-03 11:12:17 »

oop I know how to fix the distance. With the z component I dont know how I would implement it into the code and how I would change the rendering. yep its in radians. When I render it what happens is I use each vector in a shape object which I call square. When I change the rotx roty values the square changes shape into an odd looking rectangle
Opiop
 « Reply #13 - Posted 2013-11-03 11:21:35 »

Can you post more code and screenshots? And the z component is the sane thing as the others, it just isn't as complex. All you need is the z position of the camera and the rotation on the y axis. Do you not using a z axis in your rendering? Because you would need to for it to be fully controllable in 3D...

lcass
 « Reply #14 - Posted 2013-11-03 11:37:55 »

Do you have an example of some code I could just see how you render it.
Opiop
 « Reply #15 - Posted 2013-11-03 11:44:46 »

Yeah, sure. In my voxel engine I use this:
 1 `      cube.createWireFrameCube(-this.getPosition().x + (4 * Math.cos(this.getRotation().x * (Math.PI / 180)) * Math.sin(this.getRotation().y * (Math.PI / 180))), -this.getPosition().y + (4 * Math.sin(this.getRotation().x * (Math.PI / 180)) * Math.sin(this.getRotation().y * (Math.PI / 180))), -this.getPosition().z + (4 * Math.cos(this.getRotation().y * (Math.PI / 180))), 1);`

Its honestly just the equation I gave you, with a little extra math to convert it to radians. I take the negative position because my player class was a little screwed up, maybe you should try seeing if that works!

lcass
 « Reply #16 - Posted 2013-11-03 12:22:35 »

What ill do is upload the code just so you can see the issue

Opiop
 « Reply #17 - Posted 2013-11-03 12:24:29 »

Well, what am I looking at here? I don't know what to look for or anything... Does your camera have a z axis? If it does, than that's what you need for the z component. I don't entirely understand what the problem is here as you haven't explained whats wrong.

lcass
 « Reply #18 - Posted 2013-11-03 15:21:22 »

OH the z works perfectly its the x changing what happens is instead of rotating it makes it look like you are just walking by the pixel.
lcass
 « Reply #19 - Posted 2013-11-03 16:53:33 »

And I think I just noticed why this may seem confusing... Im looking for drawing the vectors when the cameras rotation changes not rotating the vectors.
lcass
 « Reply #20 - Posted 2013-11-03 17:38:22 »

Honestly I have no goddamn clue what the hell is going on with this I want it to rotate around the camera via the y axis it doesnt even make a square it just shoots around the place for no reason....
 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 `package lcass.com.graphics;import lcass.com.control.Camera;import lcass.com.core.core;import lcass.com.graphics.objects.vertex;public class RENDER3D {   private core core;   public RENDER3D(core core) {      this.core = core;   }   public void drawvertex(vertex v, Camera c,double rotx,double roty) {         double cz = c.z;            double dist = Math.abs(v.z - cz);      double tx = v.x - c.x;      double ty = v.y - c.y;      double tz = v.z - c.z;            double nx = tx * Math.cos(roty) - ty * Math.sin(roty);      double ny = tx * Math.sin(rotx) + ty * Math.cos(rotx);      double nz = tz* Math.cos(roty);      double adist = Math.abs(nz-c.z);      double dx = 0;      double dy = 0;      System.out.println(dist);      double scale = 512 /adist;                  dx = (nx) * (scale /adist)+ (core.WIDTH / 2);         dy = (ny)* (scale / adist)+ (core.HEIGHT / 2);                  if (dx < core.WIDTH && dy < core.HEIGHT) {         if (dx > 0 && dy > 0 && nz >=0) {            core.screen.map[(int) dx][(int) dy] = 0xFF00FF;         }      }   }}`
lcass
 « Reply #21 - Posted 2013-11-03 19:11:42 »

Ok so this code works for moving foward backward up down left right but how would I rotate For example minecraft rotation like that turning the camera.
 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 `public class RENDER3D {   private core core;   public RENDER3D(core core) {      this.core = core;   }   public void drawvertex(vertex v, Camera c,double rotx,double roty) {         double cz = c.z;            double dist = Math.abs(v.z - cz);      double tx = v.x - c.x;      double ty = v.y - c.y;      double tz = v.z - c.z;      double dx = 0;      double dy = 0;      double scale = 512 /dist;                  dx = (tx) * (scale /               tz)+ (core.WIDTH / 2);         dy = (ty)* (scale / tz)+ (core.HEIGHT / 2);                  if (dx < core.WIDTH && dy < core.HEIGHT) {         if (dx > 0 && dy > 0 ) {            core.screen.map[(int) dx][(int) dy] = 0xFF00FF;         }      }   }}`
Dxu1994
 « Reply #22 - Posted 2013-11-04 03:47:21 »

No wonder its not rotating, you're not doing anything with your input params rotx and roty.

Take a look at http://www.genesis3d.com/~kdtop/Quaternions-UsingToRepresentRotation.htm , that should get you started.

lcass
 « Reply #23 - Posted 2013-11-04 07:50:04 »

No wonder its not rotating, you're not doing anything with your input params rotx and roty.

Take a look at http://www.genesis3d.com/~kdtop/Quaternions-UsingToRepresentRotation.htm , that should get you started.
I know that that is the code I was using for moving that worked.
lcass
 « Reply #24 - Posted 2013-11-04 20:47:50 »

I think instead of sin sin its sin cos also would the radius be     cuberoot(x-xx)2+(y-yy)2+(z-zz)2)  with the 2 being square?

x'=z*sin(yaw)+x*cos(yaw)
y'=y
z'=z*cos(yaw)-x*sin(yaw)

x"=x'
y"=y'*cos(pitch)-z'*sin(pitch)
z"=y'*sin(pitch)+z'*cos(pitch)

x"'=y"*sin(roll)+x"*cos(roll)
y"'=y"*cos(roll)-x"*sin(roll)
z"'=z"

Opiop
 « Reply #25 - Posted 2013-11-04 21:38:43 »

No, the formula I posted works.

lcass
 « Reply #26 - Posted 2013-11-04 21:55:55 »

okly just checking
lcass
 « Reply #27 - Posted 2013-11-05 21:22:44 »

Opiop in your code what is the radius equal to is it   the   sqrt((x*x)+(y*y)+(z*z))  ??
Opiop
 « Reply #28 - Posted 2013-11-05 21:30:25 »

No, the radius is just half the size of the sphere. So say you want to have your code pick an object 4 tiles ahead of you, you would set the radius to 4. Thats it!

lcass
 « Reply #29 - Posted 2013-11-06 16:18:54 »

Oh thanks lol I was vizualising it in my head thing this is a bit odd
Pages: [1]
 ignore  |  Print

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

 SHC (19 views) 2015-08-01 03:58:20 Jesse (19 views) 2015-07-29 04:35:27 Riven (39 views) 2015-07-27 16:38:00 Riven (21 views) 2015-07-27 15:35:20 Riven (24 views) 2015-07-27 12:26:13 Riven (14 views) 2015-07-27 12:23:39 BurntPizza (35 views) 2015-07-25 00:14:37 BurntPizza (46 views) 2015-07-24 22:06:39 BurntPizza (27 views) 2015-07-24 06:06:53 NoxInc (35 views) 2015-07-22 22:16:53
 theagentd 51x wessles 45x basil_ 35x KaiHH 26x orangepascal 26x ags1 21x Riven 19x mooman219 17x bornander 14x KudoDEV 13x Jesse 11x princec 11x CelestialCreator 11x klaus 11x Zaneris 9x philfrei 8x
 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