Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (602)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Calculating the new x position  (Read 1778 times)
0 Members and 1 Guest are viewing this topic.
Offline 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?
Offline 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.
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #2 - Posted 2013-11-02 16:21:06 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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.
Offline 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)
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #5 - Posted 2013-11-02 20:50:41 »

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

Offline 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.
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« 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.

Offline 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
Offline 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 Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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 a is for adjusted
prefix v is for vertex
prefix c is for camera
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« 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?

Offline 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
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« 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...

Offline 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.
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« 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!

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

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

https://drive.google.com/file/d/0B48ywWBymi3OZDcwYjJLQTBQdUE/edit?usp=sharing
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« 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.

Offline 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.
Offline 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.
Offline 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;
         }
      }
   }
}
Offline 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;
         }
      }
   }
}
Offline 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.

Offline 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 Tongue that is the code I was using for moving that worked.
Offline 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"

Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


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

No, the formula I posted works.

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

okly just checking Smiley
Offline 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))  ??
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« 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!

Offline 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 Smiley
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.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (49 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (103 views)
2014-11-26 15:20:36

toopeicgaming1999 (31 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!