2d raycast porting issue (P5>P3)

Hi team.
I could use some help regarding an issue I experience.
Daniel S. did a raycast tutorial in P5 (
https://www.youtube.com/watch?v=-6iIc6-Y-kk )

I am doing the same though in P3. it goes correct until at in P5 a wall function is created that is called in a class. I am lost how to declare this correctly in P3.

I tried float/Boolean/etc but I seem to miss something.

in the ray class, i do something wrong with the cast function
"void cast(Boundary wall ){ "

some tips would help to understand what I do wrong

the code:

Boundary wall;
Ray ray;




void setup(){
  size(900,900);
  
  wall = new Boundary(800,100,800,800);
  ray = new Ray(300,450);
}





void draw(){
  background(0);
  wall.show();
  ray.show();
void pt= ray.cast(wall);   /// hmmz help
  
//  float pt= rau

}


   PVector a;
   PVector b;

class Boundary{
 
  Boundary(float x1,float y1,float  x2,float y2){
   
     a= new PVector(x1,y1);
     b= new PVector(x2,y2);
  }
  
  
  
  void show(){
    stroke(255);
   line(a.x,a.y,b.x,b.y); 
  }
  
  
  
  
  
}
   PVector dir;

class Ray{
 
  Ray( float x,float y){
   
     pos= new PVector(x,y);
     dir= new PVector(1,0);
  }
  
  
  
  void show(){
    stroke(255);
    pushMatrix();
    
    translate(pos.x,pos.y);
    line(0,0,dir.x*10,dir.y*10); 
    popMatrix();
   
  }
  
  
 
 
 void cast(Boundary wall ){  //function not a class

 float   x1= wall.a.x;
 float   y1= wall.a.y;
 float   x2= wall.b.x;
 float   y2= wall.b.x;
  
 float  x3= pos.x;
 float  y3= pos.y;
 float  x4= pos.x + dir.x;
 float  y4= pos.a.x + dir.y;
  
 float den=  (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4);

      if (den==0){
         return;
       }
    
    
    float t= ((x1-x3)*)y3-y4)-(y1-y3)*(x3-x4))/den;
    float u= -(x1-x2)*(y1-y3)-(y1-y2)*(x1-x3)/den;
    
    if(t>0 && t<1 && u>0){
      return true;
         }else{
         return;
        }
      
    }
 
 
 
 

  }
1 Like

a couple of errors. class variables need to be declared within those classes. your ray.cast method needs to be a bit different to the javascript version as javascript is fast and loose with the objects you can return (so lovely so handy) but processing expects a single type to be returned i’ve included it in the code below.

Boundary wall;
Ray ray;

void setup() {
  size(400, 400);

  wall = new Boundary(300, 100, 300, 300);
  ray = new Ray(100, 200);
}

void draw() {
  background(0);
  wall.show();
  ray.show();
  ray.lookAt(mouseX, mouseY);
  
  //so this needs to return a boolean or a value
  PVector pt= ray.cast(wall);   /// hmmz help
  if(pt != null) {
    fill(255);
    ellipse(pt.x, pt.y, 8, 8);
  }
}

class Boundary {
  PVector a;
  PVector b;
  Boundary(float x1, float y1, float  x2, float y2) {

    a= new PVector(x1, y1);
    b= new PVector(x2, y2);
  }

  void show() {
    stroke(255);
    line(a.x, a.y, b.x, b.y);
  }
}

class Ray {
  PVector pos, dir;
  Ray( float x, float y) {
    pos= new PVector(x, y);
    dir= new PVector(1, 0);
  }

void lookAt(float x, float y) {
  dir.x = x - pos.x;
  dir.y = y - pos.y;
  dir.normalize();
}

  void show() {
    stroke(255);
    pushMatrix();

    translate(pos.x, pos.y);
    line(0, 0, dir.x * 10, dir.y * 10); 
    popMatrix();
  }

  PVector cast(Boundary wall ) {  //function not a class

    float  x1 = wall.a.x;
    float  y1 = wall.a.y;
    float  x2 = wall.b.x;
    float  y2 = wall.b.x;

    float  x3 = pos.x;
    float  y3 = pos.y;
    float  x4 = pos.x + dir.x;
    float  y4 = pos.y + dir.y;

    float den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);

    if (den == 0) {
      return null;
    }
    
    float t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den;
    float u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / den;

    if (t > 0 && t < 1 && u > 0) {
      return new PVector(x1 + t * (x2 - x1), y1 + t * (y2 - y1));
    } else {
      return null;
    }
  }
}

this brings you to this point in the video

wait until you get to the raymarching it’s super cool.

1 Like

Hotfooted, thank you so much for pointing in the correct direction.
the “super coolness” is at its place indeed.

i am much helped.

1 Like

no worries. i have no idea what your goals are for raycasting but i cannot help but link this it is so dope.

It will be used for line of sight, light penetration estimation and shadowmapping at first.
slowly branching the knowledge and techniques on this part. this is a perfect start.

knowing that implementing GPU for raycast tasks lies around the corner but am not in a hurry and love learning techniques (being a low level coder i need to be very modest) . slowly the capabilities are bleeding in the work being urban/building physics related.

as for " dopeness" indeed raycast/marching is a technique that has so many uses, it quite brings a smile on new ideas that pops in the head (many die in vanity, though the smile is worth it :-).
amazed how processing allows to master those pixels on the screen truelly expanding new capabilities

1 Like