# Cannot read properties of undefined (reading 'copy')

**URL:** <https://discourse.processing.org/t/cannot-read-properties-of-undefined-reading-copy/41019>\
**Category:** Coding Questions\
**Created:** [February 23, 2023, 10:25am UTC](https://discourse.processing.org/t/cannot-read-properties-of-undefined-reading-copy/41019 "2023-02-23T10:25:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdooley](https://avatars.discourse-cdn.com/v4/letter/b/7cd45c/32.png) [@bdooley](https://discourse.processing.org/u/bdooley)\
**Post date:** [February 23, 2023, 10:25am UTC](https://discourse.processing.org/t/cannot-read-properties-of-undefined-reading-copy/41019/1 "2023-02-23T10:25:53Z")

</div>

Hi,

I am working on a sketch with rectangles and circles slowly falling to the bottom of the screen at random angles, colliding with each other and the ground and walls.

[https://editor.p5js.org/bdooley7/sketches/RBeFV18PQ](https://editor.p5js.org/bdooley7/sketches/RBeFV18PQ)

While the circles would stop at the wall and ground boundaries, the rectangle would fall through a little bit, stopping at their center point. While I was trying to fix this, I began to receive this message:

TypeError: Cannot read properties of undefined (reading ‘copy’)  
at undefined:88974:25

Still, I am having trouble handling collisions between circle-circles, rectangle-rectangles, and rectangle-circles and would greatly appreciate any help!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 23, 2023, 11:13am UTC](https://discourse.processing.org/t/cannot-read-properties-of-undefined-reading-copy/41019/2 "2023-02-23T11:13:25Z")

</div>

> [@bdooley](#):
>
> TypeError: Cannot read properties of undefined (reading ‘copy’)  
> at undefined:88974:25

This is the full exception messages:

```auto
p5.js:88974 Uncaught TypeError: Cannot read properties of undefined (reading 'copy')
    at Function.sub (p5.js:88974:25)
    at Wall.distanceTo (be2587dc-633d-49a5-9bee-00e3572370cd:524:24)
    at drawObjects (be2587dc-633d-49a5-9bee-00e3572370cd:88:36)
    at draw (be2587dc-633d-49a5-9bee-00e3572370cd:23:3)
    at _main.default.redraw (p5.js:68314:25)
    at _draw (p5.js:60836:23)

```

- `at Function.sub (p5.js:88974:25)` belongs to p5js library itself.
- `at Wall.distanceTo (...:524:24)` is from your own sketch.

This is your Wall::**distanceTo()** method from lines 522 to 534:

```auto
  distanceTo(x, y) {
    let v = createVector(x, y);
    let ab = p5.Vector.sub(this.b, this.a);
    let av = p5.Vector.sub(v, this.a);
    let t = av.dot(ab) / ab.dot(ab);
    if (t < 0) {
      t = 0;
    } else if (t > 1) {
      t = 1;
    }
    let point = p5.Vector.add(this.a, p5.Vector.mult(ab, t));
    return p5.Vector.sub(v, point).mag();
  }

```

The exception is thrown from line 524: `let ab = p5.Vector.sub(this.b, this.a);`

I couldn’t find any properties called _a_ or _b_ inside your class Wall anywhere!

> [@bdooley](#):
>
> … collisions between circle-circles, rectangle-rectangles, and rectangle-circles…

1. [Circle-Circle](https://CrHallberg.com/CollisionDetection/Website/circle-circle.html)
2. [Rect-Rect](https://CrHallberg.com/CollisionDetection/Website/rect-rect.html)
3. [Circle-Rect](https://CrHallberg.com/CollisionDetection/Website/circle-rect.html)
