# Offsetting 3D Points Based On Rotation + Position Of Camera

This topic is 1254 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Please keep in mind that I graduated High School in 1998, and I am very rusty on advanced math !

I have set up a basic Object that contains all my point data ( X Y Z ) for my 3D cube. Whenever it renders in the world, it does not rotate with the "camera" . ( It does move on X Y )

What are the SIMPLIFIED math formulas that are required to offset the points based on the camera's movement and rotation ?

What are the SIMPLIFIED math formulas required for scaling as the camera approaches and retreats  ?

##### Share on other sites
The formulas are what they are, and they can't be simplified, unless you happen to be restricting yourself to some particular case. I am afraid you are going to have to bite the bullet and learn about vectors and matrices and how they can be used to represent 3D movements and projection.

##### Share on other sites

The formulas are what they are, and they can't be simplified, unless you happen to be restricting yourself to some particular case. I am afraid you are going to have to bite the bullet and learn about vectors and matrices and how they can be used to represent 3D movements and projection.

I do apologize, but I have no clue how to do most of THIS TYPE of math any more ...

##### Share on other sites
I'll try to work with you, but I need a bit of information to get started: How do you currently describe your camera's position and attitude?

##### Share on other sites

In relation to space:

Global X Y Z

Object X Y Z S ( offsets to Global in theory )

Camera X Y Z S ( takes Global and offsets to camera perspective in theory )

S = scale or distance

* Objects have internal pitch and yaw - however they are not used

##### Share on other sites
I can give you some simple formulas to work with. We'll start with a very simple situation, where we measure everything in pixels and the camera is an eye at a fixed position (0, 0, -1000), looking towards the origin. Our resolution is 1024x768. We can just think of this situation as having the screen contained between (-512, -384, 0) and (+512, +384, 0). So a point (x, y, z) when z = 0 will map to the pixel (512+x,384-y).

If that's clear, we just need to know how to scale things that are farther away from or closer to the eye than the screen. The answer is that point (x,y,z) maps to pixel (512+x*1000/(z+1000), 384-y*1000/(z+1000)). You can convince yourself that this is correct by making a picture of the situation (I suggest from above, to get the formula for the x coordinate) and using triangle similarity.

Now you need to know how to handle other camera positions and angles. Let's handle yaw first. You can rotate the whole world around the origin on the XY plane by using these formulas:

x' = x * cos(angle) - y * sin(angle)
y' = x * sin(angle) + y * cos(angle)

Do that to every point in the scene and your whole world has now rotated around the origin. If you want the rotation to happen around any other point, first subtract the coordinates of the center of rotation, then rotate, then add them back. In particular, you can now perform rotations around the camera, which has the same effect as rotating the camera.

For pitch you can do the same thing in the YZ plane. The order of rotations does matter. You probably want to apply pitch first and then yaw, but if that doesn't do what you want, reverse the order.

Moving the camera around is pretty easy, since all you have to do is -again- move the whole world in the opposite direction, by subtracting some vector from every point.

That's the closest I can get to "simplified math". I hope it helps. But you should really really try to find a gentle introduction to the algebra of matrices and vectors and try to understand the formulas you've found in other places.

##### Share on other sites

I seem to be getting a lot of EOB errors and inconstant data - this worked fine before the math was introduced .

int xx = width/2 ;
int yy = height/2 ;

for (int i = 0; i < 361; i++){
//img = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
pixle( (int)( xx * Math.cos(i) - yy * Math.sin(i) ) ,(int) ( xx * Math.sin(i) +  yy * Math.cos(i) ) );
frame.repaint();
sleep(5);
}
[snip]
public void pixle(int x,int y){
System.out.println("~" + c + "\n" + x + " " + y );
try{
img.setRGB(x, y, color.getRGB() );
img.setRGB(x + 1 , y, color.getRGB() );
img.setRGB(x -1 , y, color.getRGB() );
img.setRGB(x , y + 1, color.getRGB() );
img.setRGB(x, y - 1, color.getRGB() );
} catch(Exception e){System.out.println("Error - EOB");}
c+= 1 ;
}


// without the try block

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.IntegerInterleavedRaster.setDataElements(Unknown Source)
at java.awt.image.BufferedImage.setRGB(Unknown Source)
at me.RLS0812.Mic_Code.b.pixle(b.java:72)
at me.RLS0812.Mic_Code.b.<init>(b.java:44)
at me.RLS0812.Mic_Code.Main.main(Main.java:23)

// with try block

~1
341 192
~2
22 390
Error - EOB
~3
-316 230
Error - EOB
~4
-364 -141
Error - EOB
~5
-77 -383
Error - EOB
~6
280 -272
Error - EOB
~7
381 89
~8
130 368
~9
-239 309
Error - EOB
~10
-389 -34
Error - EOB
~11
-181 -346
Error - EOB
~12
193 -340
Error - EOB
~13
390 -20
Error - EOB
~14
228 317
~15
-143 364
Error - EOB
~16
-383 75
Error - EOB
~17
-271 -282
Error - EOB
~18
90 -380
Error - EOB
~19
369 -129
Error - EOB
~20
308 240
~21
-36 389
Error - EOB

[snip]

Edited by Shippou

##### Share on other sites

Is this formula verbatim, or programmatically ?

The answer is that point (x,y,z) maps to pixel (512+x*1000/(z+1000), 384-y*1000/(z+1000)).

x = ( width / 2 + ( x * focus ) ) / ( z + focus )
y = ( width / 2 - ( y * focus ) ) / ( z + focus )
or
x = ( ( ( height / 2 ) + x ) * focus) / ( z + focus )
y = ( ( ( height / 2 ) - y ) * focus) / ( z + focus )

Neither:

x = width / 2 + x * D / (z + D)
y = height / 2 - y * D / (z + D)

But, really, you should try to figure out how this works on your own.

EDIT: I had to add a quote of the post I was responding to, because it was later edited, rendering the conversation absurd. Please don't do that! Edited by Álvaro

##### Share on other sites

I seem to be getting a lot of EOB errors and inconstant data - this worked fine before the math was introduced .

Two things:
(1) Don't edit a post in such a way that the conversation makes no sense.