View Full Version : 2D Ray Tracing Animation

Tweener

10-15-2002, 01:47 PM

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

nexusone

10-15-2002, 03:06 PM

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

Tweener

10-15-2002, 06:20 PM

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

Thanks

MrShoe

10-15-2002, 06:59 PM

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.

Tweener

10-15-2002, 07:20 PM

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 << "\nnormalize() 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));

thit.add(A); //get hit point

}

Tweener

10-15-2002, 07:21 PM

Oh yeah, thank you guys very much for your interest in helping me.

Well, one thing that looks wrong to me is how you set the normal to the line in Line::Line(). Near the middle of GetHit(), when you call Line L(B,P), in the Line constructor you're finding a normal for the vector from (0.0, 0.0) --> B, which ends up almost parallel to the vector from point B to point P.

Powered by vBulletin® Version 4.2.3 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.