lcass


«
Posted
20131102 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
20131102 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
20131102 16:21:06 » 

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




Games published by our own members! Check 'em out!


lcass


«
Reply #3  Posted
20131102 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
20131102 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
20131102 20:50:41 » 





lcass


«
Reply #6  Posted
20131102 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
20131103 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.





lcass


«
Reply #9  Posted
20131103 07:50:59 » 

When I say pojection I mean orthagonally projected from 3d to 2d. Also thanks opiop you have even given me the rotation part




Games published by our own members! Check 'em out!


lcass


«
Reply #10  Posted
20131103 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




Opiop


«
Reply #11  Posted
20131103 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
20131103 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
20131103 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
20131103 11:37:55 » 

Do you have an example of some code I could just see how you render it.




Opiop


«
Reply #15  Posted
20131103 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
20131103 12:22:35 » 





Opiop


«
Reply #17  Posted
20131103 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
20131103 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
20131103 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
20131103 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(nzc.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
20131103 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; } } } } 





lcass


«
Reply #23  Posted
20131104 07:50:04 » 

I know that that is the code I was using for moving that worked.




lcass


«
Reply #24  Posted
20131104 20:47:50 » 

I think instead of sin sin its sin cos also would the radius be cuberoot(xxx)2+(yyy)2+(zzz)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
20131104 21:38:43 » 

No, the formula I posted works.




lcass


«
Reply #26  Posted
20131104 21:55:55 » 

okly just checking




lcass


«
Reply #27  Posted
20131105 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
20131105 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
20131106 16:18:54 » 

Oh thanks lol I was vizualising it in my head thing this is a bit odd




