# 2d raycast porting issue (P5\>P3)

**URL:** <https://discourse.processing.org/t/2d-raycast-porting-issue-p5-p3/10940>\
**Category:** Coding Questions\
**Created:** [May 5, 2019, 11:43am UTC](https://discourse.processing.org/t/2d-raycast-porting-issue-p5-p3/10940 "2019-05-05T11:43:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TTG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ttg/32/983_2.png) [@TTG](https://discourse.processing.org/u/TTG)\
**Post date:** [May 5, 2019, 11:43am UTC](https://discourse.processing.org/t/2d-raycast-porting-issue-p5-p3/10940/1 "2019-05-05T11:43:31Z")

</div>

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](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:

```auto
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

```

}

```auto

   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); 
  }
  
  
  
  
  
}

```

```auto
   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;
        }
      
    }
 
 
 
 

  }

```

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [May 6, 2019, 1:24am UTC](https://discourse.processing.org/t/2d-raycast-porting-issue-p5-p3/10940/2 "2019-05-06T01:24:19Z")

</div>

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.

```auto
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](https://youtu.be/-6iIc6-Y-kk?t=3954)

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

---

<div class="post-metadata">

**Author:** ![TTG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ttg/32/983_2.png) [@TTG](https://discourse.processing.org/u/TTG)\
**Post date:** [May 6, 2019, 7:16am UTC](https://discourse.processing.org/t/2d-raycast-porting-issue-p5-p3/10940/3 "2019-05-06T07:16:27Z")

</div>

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

i am much helped.

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [May 6, 2019, 1:55pm UTC](https://discourse.processing.org/t/2d-raycast-porting-issue-p5-p3/10940/4 "2019-05-06T13:55:28Z")

</div>

no worries. i have no idea what your goals are for raycasting but i cannot help but link [this](http://www.playfuljs.com/a-first-person-engine-in-265-lines/) it is so dope.

---

<div class="post-metadata">

**Author:** ![TTG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ttg/32/983_2.png) [@TTG](https://discourse.processing.org/u/TTG)\
**Post date:** [May 6, 2019, 2:15pm UTC](https://discourse.processing.org/t/2d-raycast-porting-issue-p5-p3/10940/5 "2019-05-06T14:15:02Z")

</div>

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
