## Z from X, Y, and rotation

• 1,457 Views
• 55 Replies
Zanzlanz
Zanzlanz
• Member since: Sep. 9, 2011
• Offline.
Forum Stats
Supporter
Level 15
Game Developer
Z from X, Y, and rotation 2012-07-25 12:07:01

Hi!

So I'm trying to find the Z coordinate on a sphere. It's based off of rotation (out of 1).
a and b are rotation. I can find the x and y point using these equations:

``````x = Math.sin(a/180*Math.PI);
y = Math.sin(b/180*Math.PI);``````

So what I need to find out, is what is z based off of both of those rotations?

What I do know with z, is:
If b == 0, z = Math.cos(b/180*Math.PI);
If a == 0, z = Math.cos(a/180*Math.PI);
But I can't figure out how to make it work if both a and b are not zero.

- Zanzlanz

http://www.newgrounds.com/portal/vi ew/579172
Yes, that's Minecraft in Flash :3

GeoKureli
GeoKureli
• Member since: Apr. 1, 2003
• Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to Z from X, Y, and rotation 2012-07-25 13:54:52

At 7/25/12 12:07 PM, zanzlanz wrote: Hi!

So I'm trying to find the Z coordinate on a sphere. It's based off of rotation (out of 1).
a and b are rotation. I can find the x and y point using these equations:

x = Math.sin(a/180*Math.PI);
y = Math.sin(b/180*Math.PI);

So what I need to find out, is what is z based off of both of those rotations?

What I do know with z, is:
If b == 0, z = Math.cos(b/180*Math.PI);
If a == 0, z = Math.cos(a/180*Math.PI);
But I can't figure out how to make it work if both a and b are not zero.

- Zanzlanz

the relationship between x and y is the same as x & z and y & z

the way you think of 2d rotation is z rotation
x = cos(zRot);
y = sin(zRot);

x = cos(yRot)
z = sin(yRot)

y = cos(xRot)
z = sin(xRot)

Zanzlanz
Zanzlanz
• Member since: Sep. 9, 2011
• Offline.
Forum Stats
Supporter
Level 15
Game Developer
Response to Z from X, Y, and rotation 2012-07-25 14:12:13

the relationship between x and y is the same as x & z and y & z

the way you think of 2d rotation is z rotation
x = cos(zRot);
y = sin(zRot);

x = cos(yRot)
z = sin(yRot)

y = cos(xRot)
z = sin(xRot)

It's just, how can I relate Z with X and Y?
I keep seeing the Pythagorean Theorem when I'm doing this, but the results come to no avail. :\

It's not very easy to explain unfortunately.

http://www.newgrounds.com/portal/vi ew/579172
Yes, that's Minecraft in Flash :3

kiwi-kiwi
kiwi-kiwi
• Member since: Mar. 6, 2009
• Offline.
Forum Stats
Member
Level 09
Programmer
Response to Z from X, Y, and rotation 2012-07-25 14:21:35

At 7/25/12 02:12 PM, zanzlanz wrote: Thanks for your reply, but I already know 2D trig like that.
It's just, how can I relate Z with X and Y?
I keep seeing the Pythagorean Theorem when I'm doing this, but the results come to no avail. :\

It's not very easy to explain unfortunately.

I'm not 100% sure what you are trying to achieve, but I'm 90% sure you are searching for this.

Zanzlanz
Zanzlanz
• Member since: Sep. 9, 2011
• Offline.
Forum Stats
Supporter
Level 15
Game Developer
Response to Z from X, Y, and rotation 2012-07-25 14:27:45

I'm not 100% sure what you are trying to achieve, but I'm 90% sure you are searching for this.

I did indeed see that.
I also found this as well:
http://en.wikipedia.org/wiki/Celestial_coordinate_system

I'll look at it for a little more while. If I can't figure out how to find the missing point, I'll just move on

http://www.newgrounds.com/portal/vi ew/579172
Yes, that's Minecraft in Flash :3

kiwi-kiwi
kiwi-kiwi
• Member since: Mar. 6, 2009
• Offline.
Forum Stats
Member
Level 09
Programmer
Response to Z from X, Y, and rotation 2012-07-25 14:41:00

At 7/25/12 02:27 PM, zanzlanz wrote:
I'm not 100% sure what you are trying to achieve, but I'm 90% sure you are searching for this.
I did indeed see that.
I also found this as well:
http://en.wikipedia.org/wiki/Celestial_coordinate_system

I'll look at it for a little more while. If I can't figure out how to find the missing point, I'll just move on

The missing point is that you are describing a circle there
For a sphere, if a and b are the rotations then
x = r * Math.sin(a / 180*Math.PI) * Math.cos(b /180 * Math.PI)
y = r * Math.sin(a /180*Math.PI) * Math.sin(b / 180 * Math.PI)
z = r * Math.cos(a / 180*Math.PI)

where r is the radius of the sphere, a and b are in degrees and 0 <= a < 360, 0 <= b < 180

Zanzlanz
Zanzlanz
• Member since: Sep. 9, 2011
• Offline.
Forum Stats
Supporter
Level 15
Game Developer
Response to Z from X, Y, and rotation 2012-07-25 14:48:45

The missing point is that you are describing a circle there
For a sphere, if a and b are the rotations then
x = r * Math.sin(a / 180*Math.PI) * Math.cos(b /180 * Math.PI)
y = r * Math.sin(a /180*Math.PI) * Math.sin(b / 180 * Math.PI)
z = r * Math.cos(a / 180*Math.PI)

where r is the radius of the sphere, a and b are in degrees and 0 <= a < 360, 0 <= b < 180

a is x rotation and b is y rotation
What if I were to put z as the z rotation?

Also, would a and b really have a limit if 361 is the same as 1 in a sin or cosine function?

http://www.newgrounds.com/portal/vi ew/579172
Yes, that's Minecraft in Flash :3

kiwi-kiwi
kiwi-kiwi
• Member since: Mar. 6, 2009
• Offline.
Forum Stats
Member
Level 09
Programmer
Response to Z from X, Y, and rotation 2012-07-25 15:10:58

At 7/25/12 02:48 PM, zanzlanz wrote:
The missing point is that you are describing a circle there
For a sphere, if a and b are the rotations then
x = r * Math.sin(a / 180*Math.PI) * Math.cos(b /180 * Math.PI)
y = r * Math.sin(a /180*Math.PI) * Math.sin(b / 180 * Math.PI)
z = r * Math.cos(a / 180*Math.PI)

where r is the radius of the sphere, a and b are in degrees and 0 <= a < 360, 0 <= b < 180
a is x rotation and b is y rotation
What if I were to put z as the z rotation?

I am still not sure what you mean by that, right now the only thing that I can imagine is that you have a point (x, y, z) and you are trying to rotate it to a certain point on a sphere.
If that is the case then you can either use Givens rotor matrices or Quaternions. I have yet to find sensible information on the internet regarding the last one though so I can't really recommend something at this time.

Also, would a and b really have a limit if 361 is the same as 1 in a sin or cosine function?

Depends on how lazy you are. It's better to clamp the values between 0 and 360 because you avoid overflows that way

no problem

Zanzlanz
Zanzlanz
• Member since: Sep. 9, 2011
• Offline.
Forum Stats
Supporter
Level 15
Game Developer
Response to Z from X, Y, and rotation 2012-07-25 15:18:25

I am still not sure what you mean by that, right now the only thing that I can imagine is that you have a point (x, y, z) and you are trying to rotate it to a certain point on a sphere.

Ohh, haha, well I mean something a bit different.
I want to get the points on a sphere using rotation.
So I have a rotation number (0-360) for the x-axis. I can easily get that point with sin.
I have a rotation number for the y-axis. I can get that with sin.
And now I NEED a point for the z-axis, but I do not have a rotation. I must use the points given from the x and y axes to find the z-point.

If that is the case then you can either use Givens rotor matrices or Quaternions. I have yet to find sensible information on the internet regarding the last one though so I can't really recommend something at this time.

Also, would a and b really have a limit if 361 is the same as 1 in a sin or cosine function?
Depends on how lazy you are. It's better to clamp the values between 0 and 360 because you avoid overflows that way

True.

http://www.newgrounds.com/portal/vi ew/579172
Yes, that's Minecraft in Flash :3

PMMurphy
PMMurphy
• Member since: May. 27, 2012
• Offline.
Forum Stats
Member
Level 01
Artist
Response to Z from X, Y, and rotation 2012-07-25 15:30:58

I think you are using a computational function that will exert too much processing speed for what you are looking for. You might want to look into breaking your sphere down into millions of planes and systematically using each plane to find your locations when something crosses or connects to it.

Your process requires rotation to occur, then requires the system to make the rotation occur. Then the system has to find a location while making rotation occur. Not only that but you have to use math functions such as sin and cos. All these other trig stuff. It is heavy on computation power and depending on its use in whatever you need ti for may cause errors and glitches and slowed performance.

If you real quick layout a million planes to create the surface of a sphere, that is one computation method only. Then you can have an algorithm which scapes out important points and saves them to an array somewhere for later use. Then when those points are accessed you can take what you need from the sphere and move on. This way its not continuous computation and things are far more effecient. There will be that spike of effciency with the million planes being generated, but past that its smoothe sailing.

Zanzlanz
Zanzlanz
• Member since: Sep. 9, 2011
• Offline.
Forum Stats
Supporter
Level 15
Game Developer
Response to Z from X, Y, and rotation 2012-07-25 15:49:24

I'm actually using the numbers in a virtual raycasting camera. I tell it rotation and it will cast rays in the world and collect data (an image). Plus, performance doesn't affect this project much.

http://www.newgrounds.com/portal/vi ew/579172
Yes, that's Minecraft in Flash :3

kiwi-kiwi
kiwi-kiwi
• Member since: Mar. 6, 2009
• Offline.
Forum Stats
Member
Level 09
Programmer
Response to Z from X, Y, and rotation 2012-07-25 15:58:40

At 7/25/12 03:30 PM, PMMurphy wrote: If you real quick layout a million planes to create the surface of a sphere, that is one computation method only. Then you can have an algorithm which scapes out important points and saves them to an array somewhere for later use. Then when those points are accessed you can take what you need from the sphere and move on. This way its not continuous computation and things are far more effecient. There will be that spike of effciency with the million planes being generated, but past that its smoothe sailing.

I'd be interested to hear more about that, 1 million planes seems like a huge waste of memory (at 4 points per plane, 3 floats per point, 4 bytes per float that would be ~45MB) and at that volume you'd have a really hard time making any search algorithm cache friendly, so what you're gaining by not having to compute trigonometric functions would be wasted on waiting for the data to be available

Zanzlanz
Zanzlanz
• Member since: Sep. 9, 2011
• Offline.
Forum Stats
Supporter
Level 15
Game Developer
Response to Z from X, Y, and rotation 2012-07-25 16:13:37

I'd be interested to hear more about that, 1 million planes seems like a huge waste of memory (at 4 points per plane, 3 floats per point, 4 bytes per float that would be ~45MB) and at that volume you'd have a really hard time making any search algorithm cache friendly, so what you're gaining by not having to compute trigonometric functions would be wasted on waiting for the data to be available

Plus it's just Flash (AS3 is my native language, haha) - it's quite slow and doesn't allot much memory.

http://zanzlanz.tumblr.com/post/27980303784
I don't think I could have posted animations, but if you wanted me to, I could try to post animated gifs.

Unfortunately Flash just overwrote my old file with something completely different, so I'll have to start all over again! >.<
(Tweaking 80 lines of code is harder than it sounds)

I might just port it to Java to save time...?

Thank you again for your ideas and help!

http://www.newgrounds.com/portal/vi ew/579172
Yes, that's Minecraft in Flash :3

kiwi-kiwi
kiwi-kiwi
• Member since: Mar. 6, 2009
• Offline.
Forum Stats
Member
Level 09
Programmer
Response to Z from X, Y, and rotation 2012-07-25 16:16:32

At 7/25/12 03:49 PM, zanzlanz wrote: I'm actually using the numbers in a virtual raycasting camera. I tell it rotation and it will cast rays in the world and collect data (an image). Plus, performance doesn't affect this project much.

Performance always affects a project, if you think it from the ground up taking hardware and algorithmic complexity into consideration you're going to thank yourself later.

Also in that case the easiest way to do this would be to have two vectors that describe your camera, a vector that says which way is up and a vector that says which way is left and you cast the rays in the direction described by the vector obtained from the cross product between the left and up vectors.
You will only need two rotation angles for each of them, a will describe the amount that the camera is rotated to the left/right and b wlll describe how much your camera is rotated up/down, you can describe a.
(x, y , 0) and the left vector would be (g,0,h)

where
x = cos(a)
y = sin(a)
g = cos(b)
h = sin(b)
and the z you are looking for would be (x,y,0) X (g,0,h) where X is the cross product (also this assumes that the y axis is up in a global reference space)

Is this what you are looking for ?

Zanzlanz
Zanzlanz
• Member since: Sep. 9, 2011
• Offline.
Forum Stats
Supporter
Level 15
Game Developer
Response to Z from X, Y, and rotation 2012-07-25 16:40:28

Is this what you are looking for ?

Hm. I'm not sure. You seem to have lost me quite a bit there.
I never really explained how it's set up.

For me, each pixel is rendered (because it's a camera). This means there are two for loops, one for x and one for y on the screen.
Each pixel casts a ray in a direction. If I remember correctly:

``````xSpeed = Math.sin(((xPixel-screenWidth/2)/screenWidth*90)/180*Math.PI);
ySpeed = Math.sin(((yPixel-screenHeight/2)/screenHeight*90/)180*Math.PI);``````

I'll always see 45 degrees up/down and left/right.

At this point, I'm looking forward, so my zSpeed is always 1.

But if I want to turn it left/right I would change xSpeed and zSpeed to this:

``````xSpeed = Math.sin(((xPixel-screenWidth/2)/screenWidth*90)/180*Math.PI);
zSpeed = Math.sin(((xPixel-screenWidth/2)/screenWidth*90)/180*Math.PI);``````

I'm not sure what you mean by having 2 vectors...

(x, y , 0) and the left vector would be (g,0,h)

Can you elaborate?

http://www.newgrounds.com/portal/vi ew/579172
Yes, that's Minecraft in Flash :3

Zanzlanz
Zanzlanz
• Member since: Sep. 9, 2011
• Offline.
Forum Stats
Supporter
Level 15
Game Developer
Response to Z from X, Y, and rotation 2012-07-25 16:44:42

But if I want to turn it left/right I would change xSpeed and zSpeed to this:

xSpeed = Math.sin(((xPixel-screenWidth/2)/screenWidth*90)/180*Math.PI );
zSpeed = Math.sin(((xPixel-screenWidth/2)/screenWidth*90)/180*Math.PI );

Gah!
I mean

``````xSpeed = Math.sin(((xPixel-screenWidth/2)/screenWidth*90+a)/180*Math.PI);
zSpeed = Math.cos(((xPixel-screenWidth/2)/screenWidth*90+a)/180*Math.PI);``````

http://www.newgrounds.com/portal/vi ew/579172
Yes, that's Minecraft in Flash :3

PMMurphy
PMMurphy
• Member since: May. 27, 2012
• Offline.
Forum Stats
Member
Level 01
Artist
Response to Z from X, Y, and rotation 2012-07-25 21:28:53

I'd be interested to hear more about that, 1 million planes seems like a huge waste of memory (at 4 points per plane, 3 floats per point, 4 bytes per float that would be ~45MB) and at that volume you'd have a really hard time making any search algorithm cache friendly, so what you're gaining by not having to compute trigonometric functions would be wasted on waiting for the data to be available

The search function is imbeded into the planes. its just calculus, surface area of a sphere.

PMMurphy
PMMurphy
• Member since: May. 27, 2012
• Offline.
Forum Stats
Member
Level 01
Artist
Response to Z from X, Y, and rotation 2012-07-25 21:31:07

At 7/25/12 09:28 PM, PMMurphy wrote:
I'd be interested to hear more about that, 1 million planes seems like a huge waste of memory (at 4 points per plane, 3 floats per point, 4 bytes per float that would be ~45MB) and at that volume you'd have a really hard time making any search algorithm cache friendly, so what you're gaining by not having to compute trigonometric functions would be wasted on waiting for the data to be available
The search function is imbeded into the planes. its just calculus, surface area of a sphere.

But performance is not an issue. So there is no need to over complicate things. to the OP. If you have trouble figuring out the trig, you can still give this method a shot.

PMMurphy
PMMurphy
• Member since: May. 27, 2012
• Offline.
Forum Stats
Member
Level 01
Artist
Response to Z from X, Y, and rotation 2012-07-25 21:52:43

At 7/25/12 09:28 PM, PMMurphy wrote:

The search function is imbeded into the planes. its just calculus, surface area of a sphere.

I gave false information. The idea is from integrals and how they work. You might know what they are you might not. But basically an integral is lower and higher estimations added together to the extent of millions.

In this case, for the application i was thinking of. We would have an action where some kind of object was moving towards it, as the object was in range of the previous object (the scannable one) the "integral" algorithm would be executed in a particular range. All important distances are calculated before hand and put into the algorithm so it doesn't compute it, then the algorithm saves all important details according to what the programmer sees is important, and deletes everything else.

So for instance, lets say we were analyzing a game we just developed and there was a sphere with dots on it. The player had to choose those dots in a particular order in x amount of time. We wanted to keep in mind of the eye sight of the cahracter and that each individual timer for each dot wont be executed until it was visible. If the person took to long to figure out the code something happens periodically.

This way we can locate these points and trigger the timers in real time. We can combine the visibility algorithm and the calculation algorithm and the timer algorithms all in one without having to sit down and store all these variables and arrays.

Make sense?

PMMurphy
PMMurphy
• Member since: May. 27, 2012
• Offline.
Forum Stats
Member
Level 01
Artist
Response to Z from X, Y, and rotation 2012-07-25 22:06:03

I'm failing, i havnt been in school for the whole summer.

Ignore the calculus reference it makes no sense.

Basically the search method IS the planes. You differ them in sizes depending on your needs. if you need highly precise accurate details, you would need millions of planes. if you don't you would need less. Also it only stores and safes important information so knowledge of geometry is very important.

Zanzlanz
Zanzlanz
• Member since: Sep. 9, 2011
• Offline.
Forum Stats
Supporter
Level 15
Game Developer
Response to Z from X, Y, and rotation 2012-07-27 18:54:47

Here's a video.
The pixels on the screen are actually points on a sphere.
I have 2 variables: X and Y rotation.
X is left/right rotation
Y is up/down rotation

I know how to make the camera turn on the X axis ONLY or the Y axis ONLY, but not both.

I just need the Math for this >.<

http://www.newgrounds.com/portal/vi ew/579172
Yes, that's Minecraft in Flash :3

PMMurphy
PMMurphy
• Member since: May. 27, 2012
• Offline.
Forum Stats
Member
Level 01
Artist
Response to Z from X, Y, and rotation 2012-07-27 19:10:33

How are your functions written? Do they execute one at a time? Are you i think its called 'threading?' im not sure. If the system is trying to execute two functions at the same time you need two programs to execute it. Or else it wont execute.

PMMurphy
PMMurphy
• Member since: May. 27, 2012
• Offline.
Forum Stats
Member
Level 01
Artist
Response to Z from X, Y, and rotation 2012-07-27 19:16:03

also by the looks of your game and the stage its currently in. I would do thurough designs on your core functions and make sure everything is as efficient and you get the highest performance level you can possibly create.

If you dont, you limiting your potential functionality and taking away a lot of capabiltiies and you may or may not run into errors that are unexplainable to the point you have to restart your project.

Zanzlanz
Zanzlanz
• Member since: Sep. 9, 2011
• Offline.
Forum Stats
Supporter
Level 15
Game Developer
Response to Z from X, Y, and rotation 2012-07-28 12:15:33

I appreciate the advice, but I just want to know how to make this work xD

``````xS = Math.sin((xPixel+xRot)/180*Math.PI);
yS = Math.sin(yPixel/180*Math.PI);
zS = Math.cos((xPixel+xRot)/180*Math.PI);``````

Say xS, yS, and zS are the speeds the rays are cast.
xPixel and yPixel are the pixels of the screen appropriately divided into degrees.
xRot is the rotation of the camera in degrees side to side.
yRot (if it were there) would be the up/down rotation, but I can't figure out how to add it properly.

http://www.newgrounds.com/portal/vi ew/579172
Yes, that's Minecraft in Flash :3

kiwi-kiwi
kiwi-kiwi
• Member since: Mar. 6, 2009
• Offline.
Forum Stats
Member
Level 09
Programmer
Response to Z from X, Y, and rotation 2012-07-28 14:49:05

At 7/28/12 12:15 PM, zanzlanz wrote:
xS = Math.sin((xPixel+xRot)/180*Math.PI);
yS = Math.sin(yPixel/180*Math.PI);
zS = Math.cos((xPixel+xRot)/180*Math.PI);
``````xS = Math.sin((xPixel+xRot)/180*Math.PI) * Math.cos((yPixel + yRot)/180*Math.PI);
yS = Math.sin((yPixel + yRot)/180*Math.PI) * Math.sin((xPixel + xRot)/180*Math.PI);
zS = Math.cos((xPixel+xRot)/180*Math.PI);``````
PMMurphy
PMMurphy
• Member since: May. 27, 2012
• Offline.
Forum Stats
Member
Level 01
Artist
Response to Z from X, Y, and rotation 2012-07-28 14:50:20

to be truthful. I would have to work out the problem your doing myself and attempt to do what your doing in the language your doing/software you are using.

I don't have the programming experience to give a good justifiable answer. Also i don't know how your functions are organized or how things work in a systematic approach.

Alot of problems can be solved with a simple good design.

kiwi-kiwi
kiwi-kiwi
• Member since: Mar. 6, 2009
• Offline.
Forum Stats
Member
Level 09
Programmer
Response to Z from X, Y, and rotation 2012-07-28 15:14:27

At 7/28/12 02:50 PM, PMMurphy wrote: to be truthful. I would have to work out the problem your doing myself and attempt to do what your doing in the language your doing/software you are using.

My guess is that he is talking about speed as in the vector of the ray.

PMMurphy
PMMurphy
• Member since: May. 27, 2012
• Offline.
Forum Stats
Member
Level 01
Artist
Response to Z from X, Y, and rotation 2012-07-28 15:22:55

i somewhat understand what he is trying to do.

I just don't think its the best method. The problem about working from a bad design is you can create problems that become so difficult to solve that its simply not worth solving. It doesn't happen all the time, but it happens sometimes.

My suggestion is to debug your code line by line, or draw out the data structures and all the variables on paper and study how everything is working step by step.

Try to pin-point the EXACT problem. Also you should have testing functions built into your code and you should be testing every step of the way. If you have testing functions to see if it will rotate and do what it needs to do. Then it will record the exact information you want it to reccord.

Maybe your math isn't wrong and its something else? Do you know for a fact its the math?

Zanzlanz
Zanzlanz
• Member since: Sep. 9, 2011
• Offline.
Forum Stats
Supporter
Level 15
Game Developer
Response to Z from X, Y, and rotation 2012-07-28 16:08:40

At 7/28/12 02:49 PM, kiwi-kiwi wrote:
xS = Math.sin((xPixel+xRot)/180*Math.PI) * Math.cos((yPixel + yRot)/180*Math.PI);
yS = Math.sin((yPixel + yRot)/180*Math.PI) * Math.sin((xPixel + xRot)/180*Math.PI);
zS = Math.cos((xPixel+xRot)/180*Math.PI);

This seems like it would work, but everything is offset and twisted, like a double cone or something.

Here's a video of the results of the change.

I keep looking at the code and wondering if it's something simple like a negative or a division symbol we're missing.
But I don't know.

http://www.newgrounds.com/portal/vi ew/579172
Yes, that's Minecraft in Flash :3

PMMurphy
PMMurphy
• Member since: May. 27, 2012
• Offline.
Forum Stats
Member
Level 01
Artist
Response to Z from X, Y, and rotation 2012-07-28 16:25:16

Ok i think i get the logic now. Makes more sense. Here is some psuedo code to work with.

The earlier video you had was better then this one. But this one shows what your doing more and really explains a possible solution to your problem. Your using the x, y position of the charector and how it rotates to determine how to move the points of the trees and the enviroment around you.

When doing this you have to keep in mind of how its moving and where its moving, if you move horizontally the enviroments pixels should only adjust to the horizontal. If you move vertically your pixels should adjust to the vertical.

you know how to do that so far, Keep that code it functions and works.

Now you need an exection to the rule. You need code that alters the x and y dots of your pixels according to the rotaion of your viewer. This should be activated and executed when and only when you need to move in diagnol directions.

you can use a boolean to determine when that happens if you want.

SO basically. Take his code. Take your code. Mesh them together.

Lets see what happens after that.