This site requires JavaScript, please enable it in your browser!
Greenfoot back
wabuilderman
wabuilderman wrote ...

2015/6/17

2D raycasting

wabuilderman wabuilderman

2015/6/17

#
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):
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;
   }
Part of code that draws the line:
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);
            }
        }
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.
wabuilderman wabuilderman

2015/6/18

#
*bump*
wabuilderman wabuilderman

2015/6/19

#
Still working on the same problem.... no end in sight.
davmac davmac

2015/6/19

#
What the code you posted draws will depend on the data you give it (i.e. what's in the Lrays and RObjects arrays). It might be easier to solve the problem if you can give a complete working example with some sample data, and at least explained what Lrays is supposed to contain. What I do notice:
  • Style: you have method and variable names beginning with a capital, which goes against Java conventions and can be confusing for experienced Java programmers.
  • Some of your calculations are strange.
For the latter point, an example:
                double A2 = (y - Math.sin((double)rot))-y;
This is equivalent to just 'double A2 = -Math.sin((double) rot);'. What exactly are you trying to calculate here? You should most likely be multiplying the sine by the co-ordinate, but it's not clear to me what this calculation is really doing.
wabuilderman wabuilderman

2015/6/19

#
Ok, um, so what I was trying to do was something along the lines of this page: Geometry Concepts: Line Intersection They should a line formula in the form of Ax + By = C. To follow what they did, I defined A1 as equaling y2 - y1. Since for my ray, I didn't have a y2, I instead used sin to calculate what the next y would be. I see now that that is a bit redundant. Also, I will try and correct the names. I had forgotten about the lowercase beginnings thing. So, as for a sample: Input for Collision method:
  • x = 50 (out of a possible 600)
  • y = 200 (out of a possible 400)
  • rot = 15
Now, about the Lrays. They are essentially a vector class. They contain an originX, originY, and an angle. I realize now that I could have just called for an Lray in collision, but that falls under optimization. The Lrays array is simply a large array that can be used to access any Lrays created. They aren't actors, so I think I have to keep track of them in this way. the RObjects are the colliders. They are sets of lines. In this case, it is a simple square with each line being ten pixels long. The origin of the object (the center) is in the exact middle of the screen (300,200). Since I store only the local offsets in the line data, I then add the origin coordinates. Thus the following:
  • x1 = 295
  • y1 = 205
  • x2 = 305
  • y2 = 205
Ideally, with this set, there should be no collision, therefor, it should not draw any line. Instead the program for some reason, draws a line to 300,200
danpost danpost

2015/6/19

#
You have 'rot' coming into the 'collision' method as an 'int' value. The leads me to believe that it is a measure of angle in degrees. Yet, you are using its value in the calculations of 'Math.sin' and 'Math.cos',, which both need to use angle value in terms of radians.
davmac davmac

2015/6/19

#
They should a line formula in the form of Ax + By = C. To follow what they did, I defined A1 as equaling y2 - y1. Since for my ray, I didn't have a y2, I instead used sin to calculate what the next y would be. I see now that that is a bit redundant.
Alright, I think I understand what A, B and C are supposed to represent. Looking at your code, for A2/B2/C2, you have:
                double A2 = (y - Math.sin((double)rot))-y;   // line 19
                double B2 = x-(x + Math.cos((double)rot));   // line 21
                double C2 = (A2*x1)+(B2*y1);    // line 23
... which is equivalent to:
                double A2 = - Math.sin((double)rot);
                double B2 = - Math.cos((double)rot);
                double C2 = (A2*x1)+(B2*y1);
So, the first thing, as danpost pointed out, is that 'rot' is an 'int'. So what is rot actually meant to be - the angle of the line in radians, or in degrees? If it is in degrees, you need to convert it to radians (use Math.toRadians). If it is in radians, it should probably be declared as a double rather than than an int. The third line looks wrong. It should be:
                double C2 = (A2*x)+(B2*y);
Your expressions for A2 and B2 maybe correct, if you want the rotation to be counter-clockwise. If you want to follow the Greenfoot convention, on the other hand, you have the sign of one of them wrong.
davmac davmac

2015/6/19

#
But also:
                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);
The condition you use to check for the lines intersecting (det != 0) will be true if the lines would intersect assuming they were extended infinitely in either direction. If that is the case, you make some strange calculation for xProduct and yProduct (are you trying to get the cross product?), but I'm not sure what that is meant to represent. Let me put it this way: if x=50, y=200 and rot=15, what do you want the method to return?
wabuilderman wabuilderman

2015/6/19

#
Thanks. I think the biggest problem was the typo with the x1 and y1 instead of x and y. Thanks. Now I just need to code a limitation for the lines, rather than just assuming that all the lines extend infinitely.
wabuilderman wabuilderman

2015/6/20

#
Ok, now I have that solved. It is working fine. Now I want a reflection. Pretty much, I know the location of all points involved, I know the angle of the original ray, but I don't know what angle to place the reflected ray at.
danpost danpost

2015/6/20

#
The new vector would be the old vector with the force applied along the perpendicular at the point of reflection of the surface being reflected off of negated.
davmac davmac

2015/6/20

#
Ok, now I have that solved. It is working fine. Now I want a reflection. Pretty much, I know the location of all points involved, I know the angle of the original ray, but I don't know what angle to place the reflected ray at.
This would be a bit easier if you were working proper vectors rather than angles. If you really need the angle of the reflected ray, you also need the angle of the edge that the incident ray reflects off (call it 'a'). You find the difference between the angle of the incident ray and the reflecting edge, 'd' (so d = i - a, where i is the angle of the incident ray), and then the angle of reflection is a - d, which is 2 * a - i (so you don't really need d!).
wabuilderman wabuilderman

2015/6/20

#
Ok. Thanks. :)
You need to login to post a reply.