Ray - Sphere Intersection

I have an Assignment due next week, and I need help starting it. I am kind of clueless as to where to begin if anyone can help.

File:
https://drive.google.com/drive/folders/14zFUH-ifiJ0QetaCBD-wppE_aVm8Rc-S?usp=sharing

These are the files for my homework
well, it makes it pretty clear you need to use quaternions. Have you coded up a class to do those yet? If not, sounds like a place to start.
-- I honestly think they are over-used. If you are not going very long distances in the anomaly direction, you can just add like .0001 radians the get away from the problem and use normal math which is faster (q's take a lot of extra trig calls). Someone in a game can't tell the difference until you get very, very far along the same line, and who goes in a straight line for very long in any game?
That's a strange zip file. It's 50MB in size but unzips to 3MB.
I haven't been in class for a while, so I have no clue where to start
I've never heard of quaterions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <cmath>
using namespace std;

//======================================================================

struct Pt { double x, y, z; };

Pt operator + ( Pt p, Pt q )     { return { p.x + q.x, p.y + q.y, p.z + q.z }; }
Pt operator - ( Pt p, Pt q )     { return { p.x - q.x, p.y - q.y, p.z - q.z }; }
Pt operator * ( double r, Pt p ) { return {   r * p.x,   r * p.y,   r * p.z }; }
Pt operator / ( Pt p, double r ) { return {   p.x / r,   p.y / r,   p.z / r }; }
double dot( Pt p, Pt q ) { return p.x * q.x + p.y * q.y + p.z * q.z; }
ostream &operator << ( ostream &out, const Pt &p ) { return out << p.x << " " << p.y << " " << p.z; }
istream &operator >> ( istream &in ,       Pt &p ) { return in  >> p.x        >> p.y        >> p.z; }

//======================================================================

int main()
{
   Pt centre, origin, direction;
   double radius;

   cout << "Enter sphere centre (vector): ";   cin >> centre;
   cout << "Enter sphere radius: "         ;   cin >> radius;
   cout << "Enter ray origin (vector): "   ;   cin >> origin;
   cout << "Enter ray direction (vector): ";   cin >> direction;

   // t satisfies a quadratic
   double a = dot( direction, direction );
   double b = 2 * dot( direction, origin - centre );
   double c = dot( origin - centre, origin - centre ) - radius * radius;
   double discriminant = b * b - 4 * a * c;

   if ( discriminant < 0 )
   {
      cout << "No intersection.\n";
   }
   else if ( discriminant > 0 )
   {
      double t = ( -b + sqrt( discriminant ) ) / ( 2 * a );
      double t2 = -b / a - t;
      if ( abs( t2 ) < abs( t ) ) t = t2;
      cout << "Closest intersection at " << origin + t * direction << '\n';
   }
   else
   {
      cout << "Touches sphere at " << origin + (-0.5 * b / a) * direction << '\n';
   }
}


Enter sphere centre (vector): 0 0 0
Enter sphere radius: 5
Enter ray origin (vector): 2 -1 -1
Enter ray direction (vector): 0 2 3
Closest intersection at 2 -2.76807 -3.6521
Last edited on
I've never heard of quaterions

thank goodness for google! However, I am feeling merciful and the sites are often written in phd level mathspeak, so I will give you the overview. You WILL need the web to get the equations, though.
ok so the idea is, from a rocket ship perspective: if, in 3-d, you go 'straight up' rotating about the same axis (so if up is y axis, your vehicle rotates about y) and then come back down to fly level (x and z plane), you cannot tell your correct orientation because it was LOST in the zeros from the sins/cosines during the event. Every 3-d axis can do this (meaning any 3-d coordinate system/space has this problem, but not every problem in 3-d space does the moves that lose info); the trig functions are not sufficient to track the angles through. To fix that, the 3-d information is remapped to a special 4 dimensional space (quad / quat being 4 from latin roots) that has redundant information in the terms. The extra terms keep the lost information so you can restore it and know your orientation. The idea is that simple. The math under isnt bad either, but its confusing the first time you see it esp if you are not a strong math student. Thankfully the math has been done for you, and all you need to do is look it up and implement a little class with some operator overloads and maybe a conversion back to a 3-d orientation (xyzhpr all 6 dof) object (vector or array is all you need for this, not another class).
Again, I think gamers spend too much time on this. You can fudge it away, rather than deal with quaternions, for most games. Real physics models need to do it right, though. Its still something you should at least be aware of. Ever played a game where once in a rare while an object freaks out, spinning and jerking all over? This is one cause of that kind of bug.

I haven't been in class for a while, so I have no clue where to start
... well, there comes a time to drop the class or take the F. I don't know if you can recover or not, depends on how good you are and how much you want to pass or how good you are at cheating. May as well get that over with too: this isnt a place to get it done FOR you. This is where you get HELP after TRYING.
Last edited on
Is that what they're supposed to solve? I always just use a "hat" vector and I transform it together with the line of sight.
the assignment says to use quaternions about 50 times so I would say he needs them :)
jonnin wrote:
the assignment says to use quaternions about 50 times

Actually it mentions "quaternions" twice.


To be precise it says:
use quaternions and/or linear algebra

I didn't use either.
Last edited on
you don't need them here. I can't even see where you would need one, maybe this isn't the whole thing?
I will change my answer then: fill in the provided classes first, and worry about the quaternions later if you need them. C++ has a dot product, if you decide you need that... https://en.cppreference.com/w/cpp/algorithm/inner_product
What are quaternions exactly ?
Registered users can post here. Sign in or register to post.