# How to get the position of the point directly in front of the camera？

**URL:** https://discourse.processing.org/t/how-to-get-the-position-of-the-point-directly-in-front-of-the-camera/28042
**Category:** Coding Questions
**Created:** [February 24, 2021, 8:46am UTC](https://discourse.processing.org/t/how-to-get-the-position-of-the-point-directly-in-front-of-the-camera/28042 "2021-02-24T08:46:16Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![AbleYudada](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ableyudada/32/14190_2.png) [@AbleYudada](https://discourse.processing.org/u/AbleYudada)
#### Post date: [February 24, 2021, 8:46am UTC](https://discourse.processing.org/t/how-to-get-the-position-of-the-point-directly-in-front-of-the-camera/28042/1 "2021-02-24T08:46:16Z")

</div>

Hello everyone! I want to put a box in front of a mobile camera, but no matter how I try, I can’t get it

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [February 25, 2021, 6:59pm UTC](https://discourse.processing.org/t/how-to-get-the-position-of-the-point-directly-in-front-of-the-camera/28042/2 "2021-02-25T18:59:44Z")

</div>

Hi,

If you use the [`lookAt()`](https://p5js.org/reference/#/p5.Camera/lookAt) function to point the camera at a certain point in space and the [`setPosition()`](https://p5js.org/reference/#/p5.Camera/setPosition) function to set the location of the camera, you should be able to have the camera direction vector :

```auto
const cam = createCamera();
const cameraLocation = createVector(1, 1, 1);
const cameraLookAtPoint = createVector(0, 0, 0);

// Set the camera (order is important
cam.setPosition(cameraLocation);
cam.lookAt(cameraLocation);

```

Then compute the direction vector :

```javascript
const cameraDirectionVector = p5.Vector.sub(cameraLookAtPoint, cameraLocation).normalize();

```

Then you can get any point on the direction of the camera by multiplying by a certain value :

```javascript
const distanceFromCamera = 2;
const boxCenter = p5.Vector.add(cameraLocation, p5.Vector.mult(cameraDirectionVector, distanceFromCamera));

```

Then do the display part :

```auto
// Display the box
push();
translate(boxCenter.x, boxCenter.y, boxCenter.z);
box(0.5);
pop();

```

Here is a picture showing the principle :

 ![cameraBox](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/210614466a82850a5885d75704d3d0b427b0fa15.jpeg)

Have fun! 😉

---

<div class="post-metadata">

### Author: ![AbleYudada](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ableyudada/32/14190_2.png) [@AbleYudada](https://discourse.processing.org/u/AbleYudada)
#### Post date: [February 26, 2021, 2:31am UTC](https://discourse.processing.org/t/how-to-get-the-position-of-the-point-directly-in-front-of-the-camera/28042/3 "2021-02-26T02:31:09Z")

</div>

Thank you so much! Solved a big problem for me! By the way, is it easy to convert the mouse to space position coordinates in p5, such as hitting a model like a bullet and getting their intersection point?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 26, 2021, 8:33am UTC](https://discourse.processing.org/t/how-to-get-the-position-of-the-point-directly-in-front-of-the-camera/28042/4 "2021-02-26T08:33:47Z")

</div>

very interesting.

Would that be possible in processing too?

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [February 26, 2021, 1:51pm UTC](https://discourse.processing.org/t/how-to-get-the-position-of-the-point-directly-in-front-of-the-camera/28042/5 "2021-02-26T13:51:31Z")

</div>

You are welcome! 😉

For mouse / geometry intersection, you need to cast a ray from the camera based on the field of view of the camera and the compute the intersection with the object.

Obviously it’s easier with a plane or a primitive shape since the intersection formulae is simple.

You can check this :

[https://antongerdelan.net/opengl/raycasting.html](https://antongerdelan.net/opengl/raycasting.html)

---

<div class="post-metadata">

### Author: ![AbleYudada](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ableyudada/32/14190_2.png) [@AbleYudada](https://discourse.processing.org/u/AbleYudada)
#### Post date: [March 5, 2021, 4:44am UTC](https://discourse.processing.org/t/how-to-get-the-position-of-the-point-directly-in-front-of-the-camera/28042/6 "2021-03-05T04:44:08Z")

</div>

Thanks guy ！I wiil check soon！
