Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 3d raycasting
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
evildaddy911




PostPosted: Sun Apr 07, 2013 11:42 am   Post subject: 3d raycasting

I'm planning to make a 3d fps soon and i was planning to use raycasting for the bullets. however, the formulas i have come up with... dont seem to be working. can somebody point out where i went wrong?
Hidden text
d.x = A * sin(a.right) // A is the hypotenuse of the triangle; a.right is the horizontal angle (0=z-axis, 90=y-axis)
d.y = B * sin(a.up) // a.up is the angle above the horizontal
Hidden text
1 = d.x^2 + d.y^2 + d.z^2 // 3d Pythagorean theorem, making the distance equal to 1
Hidden text
d.z = B * cos(a.right) = A * cos(a.up) // the bases of the 2 right triangles should both be equal to d.z, which is why i think there needs to be both A and B variables
d.z = A * cos(a.up) AND B = A * cos(a.right) / cos(a.up) // express d.z in terms of A and rearrange the above formula to isolate B
Hidden text
subbing the other equations into the 3d Pythagorean theorem, we get:
1 = [A * sin(a.right)]^2 + [B * sin(a.up)]^2 + [A * cos(a.up)]^2
subbing in B=... we get:
1 = [A * sin(a.right)]^2 + [(A * cos(a.right) / cos(a.up)) * sin(a.up)]^2 + [A * cos(a.up)]^2
simplifying, we get:
1 = A^2 * sin(a*right)^2 + A^2 * [cos(a.right) * sin(a.up) / cos(a.up)]^2 + A^2 * cos(a.up)^2
common factoring out the A^2, we get:
1 = A^2 * [sin(a*right)^2 + [cos(a.right) * sin(a.up) / cos(a.up)]^2 + cos(a.up)^2]
dividing both sides by A^2, then raising both sides to the power of -0.5, we get:
A = 1 / root [sin(a*right)^2 + [cos(a.right) * sin(a.up) / cos(a.up)]^2 + cos(a.up)^2]
Hidden text
Then to get B, we just sub A into B = A * cos(a.right) / cos(a.up)
after that, we just sub the A and B values into our d.x=... ; d.y=... ; and d.x=... formulas. however, only about half of the values i've tested satisfy the 1 = d.x^2 + d.y^2 + d.z^2 equation. I've posted the results of the testing below. the result column is root (d.x^2 + d.y^2 + d.z^2), and should be very close to 1.



error margins.txt
 Description:

Download
 Filename:  error margins.txt
 Filesize:  1.81 KB
 Downloaded:  230 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Sun Apr 07, 2013 1:33 pm   Post subject: Re: 3d raycasting

To be honest other than the 1's, none of the values are particularly close to 1.

evildaddy911 wrote:
d.z = B * cos(a.right) = A * cos(a.up)

This line is wrong, it should be
dz = A * cos(a.right) = B * cos(a.up)
Then we get
code:
I will use
a = a.right
b = a.up

A = 1 / sqrt( sin(a)^2 + (sin(b) * cos(a) / cos(b))^2 + cos(a)^2 )
B = A * cos(a) / cos(b)

dx = sin(a) * A
dy = sin(b) * B
dz = cos(a) * A = cos(b) * B

Then dx^2 + dy^2 + dz^2 = 1   ( Note that taking the square root is not necessary here)


Since Turing is using floating point numbers this might actually work even for angles where cos(b) = 0 or cos(a) = 0 (since Turing can't actually store pi exactly). But you should be very careful.
For example:
evildaddy911 wrote:
B = A * cos(a.right) / cos(a.up)

This is only true if cos(a.up) is not 0, but since Turing doesn't return exactly zero for cos(pi) you're probably ok.
evildaddy911




PostPosted: Wed Apr 10, 2013 3:02 pm   Post subject: RE:3d raycasting

excellant. thanks dreadnought! glad to know it was just a simple typo at the beginning!
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: