Calculating the new x position

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?

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.

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

Its for 3d programming. I want to find out how to draw the pixel via the cameras perspective.

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)

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

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.

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.

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 :slight_smile:

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

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?

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

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…

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

Yeah, sure. In my voxel engine I use this:


		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!

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

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.

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.

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.