 Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki Blog Search Turing Chat Room Members
3d raycasting       Author Message Posted: 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: Filename:  error margins.txt
Filesize:  1.81 KB     Posted: 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.

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:
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.  Posted: 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First        Page 1 of 1  [ 3 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: