# 2D Ray Tracing Animation

Hello,
I am currently working on a 2D ray tracing program. Essentially I have a point that is bouncing around inside of a rectangle. I have written classes to conceptualize objects such as a Ray, a Line, a Point, and a Vector. But I am having some difficulty arriving at the correct result. If anyone has any insight on something like this I would much appreciate.
Thank You

What is the difficulty with getting results in making the animation or raytracing an image?

Originally posted by Tweener:
Hello,
I am currently working on a 2D ray tracing program. Essentially I have a point that is bouncing around inside of a rectangle. I have written classes to conceptualize objects such as a Ray, a Line, a Point, and a Vector. But I am having some difficulty arriving at the correct result. If anyone has any insight on something like this I would much appreciate.
Thank You

My problem is actually arriving at the correct hit times and calculating the points at those hit time.

Thanks

Be al little more specific.
Maybe your projecting the rays through the near plane wrong, or maybe your getting floating point inaccuracies. Also, what, your trying to ray trace a point? Errr, you can ray trace spheres and triangles, and whatever, but ray tracing a point is almost impossible as its infinitely small and none of the rays will ever hit it. Dunno.

I apologoze for being vauge. Here is sample of what I’m trying to do, maybe my code will speak for me, better than I.

//@@@@@@@@@@@@@@@VECTOR_CLASS@@@@@@@@@@@@@@@@@@@@@@@@@@@
class Vector2{
public:

float x,y;
Point2 pt;

//VECTOR CONSTRUCTORS
Vector2(float xx, float yy){x = xx; y = yy; }
Vector2(Vector2& v){x = v.x; y = v.y; }
Vector2(){x = y = 0;} //default constructor

//VECTOR METHODS
void set(float dx, float dy){ x = dx; y = dy; pt.set(x,y);}
void set(Vector2& v){ x = v.x; y = v.y;}

void setDiff(Point2& a, Point2& b)//set to difference a - b
{x = a.x - b.x; y = a.y - b.y;}

void normalize(){//adjust this vector to unit length
double sizeSq = x * x + y * y;
if(sizeSq < 0.0000001)
{
cerr << "
normalize() sees vector (0,0)!";
return; // does nothing to zero vectors;
}
float scaleFactor = 1.0/(float)sqrt(sizeSq);
x *= scaleFactor; y *= scaleFactor;
}

Point2 AtT(float &t) //Calculate Point At Hit Time
{
Point2 P;
P.x = x * t;
P.y = y * t;

``````return P;
``````

}

float dot(Vector2 b) // return this dotted with b
{return x * b.x + y * b.y;}

void perp() // perp this vector
{float tmp = x; x = -y; y = tmp;}

float perpDot(Vector2& v) // return perp of this dotted with v
{return x *v.x - y * v.y;}
};

//@@@@@@@@@@@@@@@LINE_CLASS@@@@@@@@@@@@@@@@@@@@@@@@@@@
class Line{

public:

Line(){;};
Line(Point2 b, Point2 p)
{
B=b;
P=p; //set points

``````   n.set(b.x, b.y);
n.perp(); //normal vector to line

}
//point normal form: n.(P-B)=0
``````

Point2 P, B;
Vector2 n;

};

//@@@@@@@@@@@@@@@RAY_CLASS@@@@@@@@@@@@@@@@@@@@@@@@@@@
class Ray{
public:
Ray(){;};

``````Ray(Point2 a, Point2 dir){
A=a;
c.set(dir.x, dir.y);
}

Point2 A;
Vector2 c;
//parametric eq: r(t) = s + c*t
``````

};

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// OVERVIEW OF CALCULATION HIT TIME AND POINT
// FOR A RAY INTERSECTING A LINE
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void GetHit(void){

float denom, numer, time;

``````//LINE POINTS
Point2 B(50, 300);
Point2 P(500, 200);

//RAY POINTS
Point2 A(90, 10);
Point2 C(300, 400);
``````

//****************************************

``````          Line L(B,P); //CREATE LINE
Ray R(A,C);  //CREATE RAY
``````

//****************************************

Vector2 BA; //create direction vector B-A
BA.setDiff(B,A);

numer = L.n.dot(BA); // n.(B-A)
denom = L.n.dot(R.c); //n.c

time = numer/denom; //should give hit time

Point2 thit(R.c.AtT(time));