<p>You don't seem to be setting circle.position.z</p>

CC BY-SA 4.0
2020-03-05T10:53:28.280Z
2020-03-05T10:53:28.280Z
Stefan Monov
<p>I have created a 3D animation with a perspective projection of white circles moving randomly in a fake 3D space projected on a 2D computer screen (GIF 1). </p>

<p><a href="https://i.sstatic.net/LWqA7.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/LWqA7.gif" alt="enter image description here"></a></p>

<p>Since I need to keep the same number of visible circles, every time a circle disappears from the frame, I have to create a new visible one within the frame. To do so, I have written this piece of code:</p>

<ul>
<li><p>First I created initial coordinates and the two angles of movements (spherical coordinates):</p>

<pre><code>for circle in circles:

    circle.position.xy = np.random.uniform(-25, 25, size=2)
    z = np.random.uniform(near_z, far_z)

    circle.position.z = z
    circle.position.x *= z/-50
    circle.position.y *= z/-50

    circle.theta_deg = np.random.rand(1) * 360
    circle.phi_deg = np.random.rand(1) * 360

    theta_rad = circle.theta_deg * np.pi / 180
    phi_rad = circle.phi_deg* np.pi / 180


    circle.dx = speed * np.sin(-phi_rad - theta_rad) / frameRate
    circle.dy = -speed * np.cos(phi_rad + theta_rad) / frameRate
    circle.dz = -speed * np.cos(theta_rad) / frameRate
</code></pre>

<ul>
<li><p>Then, in the loop that plays the animation, and updates the position of each circle, I have put this condition following the same answer that was provided to the same kind of issue <a href="https://stackoverflow.com/questions/58917165/keep-constant-number-of-visible-spheres-in-front-of-the-camera-in-3d-animation">here</a>:</p>

<pre><code>max_dist = max(abs(circle.position.x),abs(circle.position.y))
limit_dist = 25 * abs((circle.position.z-near_z) / far_z)

z_rel = np.random.uniform(near_z,far_z)

if max_dist &gt; limit_dist: 
    circle.position.x = np.random.uniform(-25, 25) * z_rel/far_z
    circle.position.y = np.random.uniform(-25, 25) * z_rel/far_z
</code></pre></li>
</ul></li>
</ul>

<p>I got a weird result as shown in GIF 2</p>

<p><a href="https://i.sstatic.net/k5leO.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/k5leO.gif" alt="enter image description here"></a></p>

<p>What is wrong with my condition and how can I detect a circle that disappears from the frame and recreate one inside the frame? </p>

<opengl><3d><projections><perspective><animation>
Keep constant number of visible circles in 3D animation
Kathia
# Keep constant number of visible circles in 3D animation

(Asked by: username_0 on 2020-03-04)

I have created a 3D animation with a perspective projection of white circles moving randomly in a fake 3D space projected on a 2D computer screen (GIF 1).

[![enter image description here](https://i.sstatic.net/LWqA7.gif)](https://i.sstatic.net/LWqA7.gif)

Since I need to keep the same number of visible circles, every time a circle disappears from the frame, I have to create a new visible one within the frame. To do so, I have written this piece of code:

* First I created initial coordinates and the two angles of movements (spherical coordinates):

  ```
  for circle in circles:

      circle.position.xy = np.random.uniform(-25, 25, size=2)
      z = np.random.uniform(near_z, far_z)

      circle.position.z = z
      circle.position.x *= z/-50
      circle.position.y *= z/-50

      circle.theta_deg = np.random.rand(1) * 360
      circle.phi_deg = np.random.rand(1) * 360

      theta_rad = circle.theta_deg * np.pi / 180
      phi_rad = circle.phi_deg* np.pi / 180

      circle.dx = speed * np.sin(-phi_rad - theta_rad) / frameRate
      circle.dy = -speed * np.cos(phi_rad + theta_rad) / frameRate
      circle.dz = -speed * np.cos(theta_rad) / frameRate

  ```

  + Then, in the loop that plays the animation, and updates the position of each circle, I have put this condition following the same answer that was provided to the same kind of issue [here](https://stackoverflow.com/questions/58917165/keep-constant-number-of-visible-spheres-in-front-of-the-camera-in-3d-animation):

    ```
    max_dist = max(abs(circle.position.x),abs(circle.position.y))
    limit_dist = 25 * abs((circle.position.z-near_z) / far_z)

    z_rel = np.random.uniform(near_z,far_z)

    if max_dist > limit_dist: 
        circle.position.x = np.random.uniform(-25, 25) * z_rel/far_z
        circle.position.y = np.random.uniform(-25, 25) * z_rel/far_z

    ```

I got a weird result as shown in GIF 2

[![enter image description here](https://i.sstatic.net/k5leO.gif)](https://i.sstatic.net/k5leO.gif)

What is wrong with my condition and how can I detect a circle that disappears from the frame and recreate one inside the frame?

**Tags:** opengl, 3d, projections, perspective, animation


---

## 1 answers

### Answer by username_1 on 2020-03-05. Score: 1

You don't seem to be setting circle.position.z


---

