# \[SOLVED\] P3D ellipse rotation problem

**URL:** <https://discourse.processing.org/t/solved-p3d-ellipse-rotation-problem/5908>\
**Category:** Coding Questions\
**Created:** [November 25, 2018, 3:39am UTC](https://discourse.processing.org/t/solved-p3d-ellipse-rotation-problem/5908 "2018-11-25T03:39:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![megagoose](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@megagoose](https://discourse.processing.org/u/megagoose)\
**Post date:** [November 25, 2018, 3:39am UTC](https://discourse.processing.org/t/solved-p3d-ellipse-rotation-problem/5908/1 "2018-11-25T03:39:16Z")

</div>

Hello there fellow coders!

Let’s say I want to rotate an ellipse around it’s axis. I came up with the following code, but it is not doing what I want.

```auto
float alpha = 0;
int r = 200;

void setup()
{
  size(500, 500, P3D);
}

void draw()
{
  background(0);
  pushMatrix();
  translate(width/2, height/2, 0);
  noFill();
  stroke(255);
  rotateY(alpha);
  ellipse(0, 0, r, r);
  popMatrix();
  alpha += 0.01;
}

```

For reference, my problem is the same as what this guy posted (no solution):

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/16918/rotating-ellipse-around-its-own-y-axis)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

I am expecting this: [http://i.imgur.com/T3qFwv7.gifv](http://i.imgur.com/T3qFwv7.gifv)  
but I’m getting this: [http://i.imgur.com/twLn9vQ.gifv](http://i.imgur.com/twLn9vQ.gifv)

So it’s obviously not rotating around its Y axis, and I can’t understand why that is. Setting a different **ellipseMode** doesn’t fix the issue.

Any idea would be greatly appreciated.

Cheers

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 25, 2018, 3:51am UTC](https://discourse.processing.org/t/solved-p3d-ellipse-rotation-problem/5908/2 "2018-11-25T03:51:21Z")

</div>

> [@megagoose](#):
>
> I am expecting this: [Imgur](http://i.imgur.com/T3qFwv7.gifv)  
> but I’m getting this: [Imgur](http://i.imgur.com/twLn9vQ.gifv)

🍍🍍 for this alone!

The problem is that the sketch you are drawing is 3D!  
So the part of the ring that is far away from the camera looks smaller than the part that is closer.  
It’s a pretty weird effect.

The fix is simple: Change your camera to ignore the Z distance. In short, call ortho():

```auto
float alpha = 0;
int r = 200;

void setup()
{
  size(500, 500, P3D);
  ortho();
}

void draw()
{
  background(0);
  pushMatrix();
  translate(width/2, height/2, 0);
  noFill();
  stroke(255);
  rotateY(alpha);
  ellipse(0, 0, r, r);
  popMatrix();
  alpha += 0.01;
}

```

You might also consider not rotating the ring at all, but drawing a 2D ellipse who’s width is constantly decreasing/increasing.

---

<div class="post-metadata">

**Author:** ![megagoose](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@megagoose](https://discourse.processing.org/u/megagoose)\
**Post date:** [November 25, 2018, 4:00am UTC](https://discourse.processing.org/t/solved-p3d-ellipse-rotation-problem/5908/3 "2018-11-25T04:00:40Z")

</div>

Fantastic, ortho() worked! Thanks a lot!  
It had been driving me nuts

Yeah I knew about the width trick, but my project is more complex and involves rotations of multiple objects along X, Y and Z. I just simplified it to get straight to the point.

Thanks again @TfGuy44 😃  
Have a great day

---

<div class="post-metadata">

**Author:** ![megagoose](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@megagoose](https://discourse.processing.org/u/megagoose)\
**Post date:** [November 25, 2018, 4:21am UTC](https://discourse.processing.org/t/solved-p3d-ellipse-rotation-problem/5908/4 "2018-11-25T04:21:17Z")

</div>

Just want to add that you made me understand it was a perspective problem.  
So I fiddled with that and was able to mitigate the weird effect without having to completely give up on perspective.

```auto
void setup()
{
  size(500, 500, P3D);
  float fov = PI/24.0;
  float cameraZ = (height/2.0) / tan(fov/2.0);
  perspective(fov, float(width)/float(height), 
            cameraZ/10.0, cameraZ*10.0);
}

```

Using the code above and setting the object very far on the Z axis (around -3200), yields a decent result. Full code below:

```auto
float alpha = 0;
int r = 200;

void setup()
{
  size(500, 500, P3D);
  float fov = PI/24.0;
  float cameraZ = (height/2.0) / tan(fov/2.0);
  perspective(fov, float(width)/float(height), 
            cameraZ/10.0, cameraZ*10.0);
}

void draw()
{
  background(0);
  pushMatrix();
  translate(width/2, height/2, -3200);
  noFill();
  stroke(255);
  rotateY(alpha);
  ellipse(0, 0, r, r);
  popMatrix();
  alpha += 0.01;
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [November 25, 2018, 3:09pm UTC](https://discourse.processing.org/t/solved-p3d-ellipse-rotation-problem/5908/5 "2018-11-25T15:09:25Z")

</div>

Thanks for sharing this perspective solution to your problem with the community - very helpful!
