# [SOLVED] P3D ellipse rotation problem

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.

``````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):

I am expecting this: http://i.imgur.com/T3qFwv7.gifv
but I’m getting this: 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

1 Like

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():

``````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.

2 Likes

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

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.

``````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:

``````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;
}
``````
1 Like

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