# 3D orienting particles using velocity and position vectors in p5js

**URL:** <https://discourse.processing.org/t/3d-orienting-particles-using-velocity-and-position-vectors-in-p5js/26188>\
**Category:** Coding Questions\
**Created:** [December 13, 2020, 11:42am UTC](https://discourse.processing.org/t/3d-orienting-particles-using-velocity-and-position-vectors-in-p5js/26188 "2020-12-13T11:42:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaspar.wtf](https://avatars.discourse-cdn.com/v4/letter/k/22d042/32.png) [@kaspar.wtf](https://discourse.processing.org/u/kaspar.wtf)\
**Post date:** [December 13, 2020, 11:42am UTC](https://discourse.processing.org/t/3d-orienting-particles-using-velocity-and-position-vectors-in-p5js/26188/1 "2020-12-13T11:42:26Z")

</div>

I’m currently writting a 3D implementation of the boids algorithm in P5.js but I’m having trouble orienting my boids according to their direction (velocity). I’m having a very hard time understand what rotation matrix would work so I tried a workaround with RotateZ and RotateY. The simplest solution that I feel should work goes along these lines :

```auto
push();

translate(this.pos); 
rotateZ(atan2(this.vel.y, this.vel.x) + HALF_PI);
rotateY(atan2(this.vel.x, this.vel.z) + HALF_PI);

beginShape();
// Draw Boid Vertices..
endShape();

pop();

```

But it doesn’t. (if you zoom in you can see that some triangles still face in the wrong direction)

I’ve written a much smaller version of the program which contains only the orientation for randomly generated particles that go in a single direction. It is available here directly on the p5js website : [https://editor.p5js.org/itsKaspar/sketches/JvypSPGGh](https://editor.p5js.org/itsKaspar/sketches/JvypSPGGh) There is a default orbit control so you can zoom and drag the mouse to check the orientation of the particles.

_I know I could use this.pos.add(this.vel.mag(10)) as the coordinate of the head of each particle but I dont want to as this will complicate things when I will try to add some wing animation to the model._

Thanks so much, I’ve been stuck on this for half a day now
