# Ch 4: MathLibrary

**URL:** <https://forums.kodeco.com/t/ch-4-mathlibrary/57293>\
**Category:** Metal by Tutorials\
**Created:** [December 9, 2018, 9:37pm UTC](https://forums.kodeco.com/t/ch-4-mathlibrary/57293 "2018-12-09T21:37:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kwakita](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kwakita/32/78443_2.png) [@kwakita](https://forums.kodeco.com/u/kwakita)\
**Post date:** [December 9, 2018, 9:37pm UTC](https://forums.kodeco.com/t/ch-4-mathlibrary/57293/1 "2018-12-09T21:37:29Z")

</div>

I think I found a bug in init(eye:, center:, up:) of MathLibrary.swift. It says

```
let W = float4(w.x, w.y, x.z, 1)

```

but the third argument should be w.z rather than x.z.

This fix alone seems not address the look-at transformation, though…

Ken

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [December 11, 2018, 6:48am UTC](https://forums.kodeco.com/t/ch-4-mathlibrary/57293/2 "2018-12-11T06:48:05Z")

</div>

@kwakita - thank you for reporting this. I’ll look into this in the next few days.

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [December 12, 2018, 2:52am UTC](https://forums.kodeco.com/t/ch-4-mathlibrary/57293/3 "2018-12-12T02:52:25Z")

</div>

@kwakita - the `lookAt` matrix is back-to-front. It’s used only twice in the book I think, for shadows, and the code there compensates for it being back-to-front by negating the eye position before calling it.

Try this `lookAt` matrix:

```auto
init(eye: float3, center: float3, up: float3) {
  let z = normalize(center - eye)
  let x = normalize(cross(up, z))
  let y = cross(z, x)
    
  let X = float4(x.x, y.x, z.x, 0)
  let Y = float4(x.y, y.y, z.y, 0)
  let Z = float4(x.z, y.z, z.z, 0)
  let W = float4(-dot(x, eye), -dot(y, eye), -dot(z, eye), 1)
    
  self.init()
  columns = (X, Y, Z, W)
}

```

For further insight about `viewMatrix` and various camera matrices, check out: [https://www.3dgep.com/understanding-the-view-matrix/](https://www.3dgep.com/understanding-the-view-matrix/) - although be aware that he is using a right handed coordinate system.

(And while it’s not relevant here, OpenGL uses NDC coordinates of (-1, 1) on the z axis, whereas Metal uses (0, 1) on the z axis.)

---

<div class="post-metadata">

**Author:** ![kwakita](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kwakita/32/78443_2.png) [@kwakita](https://forums.kodeco.com/u/kwakita)\
**Post date:** [December 14, 2018, 9:28am UTC](https://forums.kodeco.com/t/ch-4-mathlibrary/57293/4 "2018-12-14T09:28:57Z")

</div>

@caroline thanks for your support and for giving me a pointer to further note. I will try the code and come here back when I need more support.

ken
