0
\$\begingroup\$

What I really need is the x,y of the object when it is outside of the screen:

enter image description here

I'm actually coding a UI in Objective-C, not a game.

In this different kind of UI you navigate a 3D space with objects.

An observer is at (x=0,y=0,z=0) and objects have realCenter and realSize.

realSize is the size of the objects at z=1.

I have calculated the angular center of the objects to be:

angularCenterX = (objectRealCenter.x - observerCenterX) / distanceFromObserver

Objective-C code:

CGPoint angularCenter = CGPointMake((object.realCenter.x - observer.center.x) / distance,
                                    (object.realCenter.y - observer.center.y) / distance);

It works but when the observer passes the objects, the objects warp around. Instead of disappearing like they normally should, they come back.

The slider controls the z of the observer, as it moves and passes an object the objects comes back:

I'm sorry the gif is too slow.

enter image description here

\$\endgroup\$
10
  • 1
    \$\begingroup\$ Did you consider checking whether the object is in front of the view point, and skipping drawing it if not? \$\endgroup\$
    – DMGregory
    Feb 9, 2021 at 19:33
  • \$\begingroup\$ When the observer is in the center the equation is like this: realCenterX / distanceFromObserver. When the observe is very close to the object, the object is almost at its realCenterX, when the observer passes the object they are again very close and the object returns. I know for sure the equation I stumbled upon is definitely wrong. \$\endgroup\$
    – Vulkan
    Feb 9, 2021 at 19:38
  • 1
    \$\begingroup\$ No, this is pretty typical for projections. In games we manually cull items behind the camera, because otherwise they still project to valid on-screen positions (just mirrored, because we use a signed distance along the camera's forward vector for the division) \$\endgroup\$
    – DMGregory
    Feb 9, 2021 at 19:43
  • 1
    \$\begingroup\$ If the object is behind the observer, then it does not have a valid projection onto the plane. If you try to use a projection function, you will get a false projection that puts it in front of the observer, where the plane is. That is not what you want. What you can do instead is grab the position of the object just before it passes behind the observer, and animate to that distant position instead. \$\endgroup\$
    – DMGregory
    Feb 9, 2021 at 20:16
  • 2
    \$\begingroup\$ You might have discovered the reason that games don't handle perspective projection via animation tweens. 😉 We just have a game loop, and each frame we take the current position of the object relative to the camera and project that. \$\endgroup\$
    – DMGregory
    Feb 9, 2021 at 20:47

0

You must log in to answer this question.