Ok, since I want to eventually make a 3d raycasting engine, I think I need to master 2d raycasting first. I have a simple scenario set up, but for some reason, this code simply points a line to the center of the screen:
Collision method (the for loops are simply to repeat it for all the lines of all the objects in the scene):
Part of code that draws the line:
In addition to wanting it to actually draw the line properly, I want to optimize this if possible, so if you see a lot of unnecessary junk, please let me know.
public Point Collision(double x,double y, int rot)
{
Point p = null;
for(int i=0;i<rd.size;i++)
{
for(int num = 0 ; num < rd.RObjects[i].LineNum; num++){
double xProduct=0;
double yProduct=0;
//find slope intercept for line
double b1;
double m1;
int x1 = (rd.RObjects[i].lineX1[num] +rd.RObjects[i].x);
int y1 = (rd.RObjects[i].lineY1[num] +rd.RObjects[i].y);
int x2 = (rd.RObjects[i].lineX2[num] +rd.RObjects[i].x);
int y2 = (rd.RObjects[i].lineY2[num] +rd.RObjects[i].y);
double A1 = y2-y1;
double A2 = (y - Math.sin((double)rot))-y;
double B1 = x1-x2;
double B2 = x-(x + Math.cos((double)rot));
double C1 = (A1*x1)+(B1*y1);
double C2 = (A2*x1)+(B2*y1);
double det = A1*B2 - A2*B1;
if(det == 0){
return null;
}else{
xProduct = (B2*C1 - B1*C2)/det;
yProduct = (A1*C2 - A2*C1)/det;
}
p = new Point((int) xProduct,(int) yProduct);
}
}
return p;
}for(int i=0;i<LraysNum;i++)
{
double x = Lrays[i].originX;
double y = Lrays[i].originY;
int rot = Lrays[i].angle;
Point p = Collision(x,y,rot);
if(p != null)
{
image.drawLine((int) x,(int) y,(int) p.x,(int) p.y);
}
}


