[SOLVED] How to get the angle from an object to the camera, when in 2D Perspective mode?

Not favoritedFavorited Favorited 0 favourites
  • 1 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • Just sharing knowledge! Edit: formatting pseudocode is a nightmare apparently

    A few days ago I ran into an issue. I'm "flipping" a 2D object 180deg via 3D Euler rotation over the Y axis (object.RotationY or object -> Set Euler rotation), in a game using a 2D camera with Perspective projection, because it looks more interesting and engaging than my initial solution which was to just "squash" its width down to 0 then back up to its original ImageWidth. The object is a playing card on its face showing its back, and when it "disappears" to the user's perspective at 90deg to the camera (the equivalent of 0 width), I update the card sprite to show the face of the card.

    Except... with Perspective projection the angle at which it disappears depends on the position in the layout, and is not necessarily 90deg. So how do we get the "disappearing angle" of this object so that we can properly "flip" the sprite at the exact moment that the sprite's underside starts showing?

    Well, we need the angle from the object to the camera, right? So I tried using this code:

    screenCenter = ViewportWidth(CardVisuals.LayerNumber)*(VanishingPointX*0.01)+ ViewportLeft(CardVisuals.LayerNumber)

    disappearingAngle = 180 - angle(CardVisuals.X, CardVisuals.Z, screenCenter, ??? )

    ...but I realized, as you can intuit with the '???' above: what is the camera's "height" when it's a 2D camera?? Construct HAS to have some value representing camera height so that it can render 3D with depth, but unless we're using a 3D camera (this project is not), that value is not exposed!

    SO, the brilliant Skymen helped me out. He figured out the math by digging in to Construct 3's code, and I created a Function:

    static const number FIELD_OF_VIEW = 45

    static number TAN_FOV_DIV_2 = 0

    On start of layout: Set TAN_FOV_DIV_2 = tan(FIELD_OF_VIEW/2)

    On function GetCameraZ(Params: layerID) -> Number:

    number normalScale = 0

    number defaultZ = 0

    Set normalScale = (LayerScale(layerID) * LayoutScale-1) * LayerScaleRate(layerID) + 1

    Set defaultZ = ViewportHeight(layerID) / (2 * TAN_FOV_DIV_2)

    Set return value = defaultZ / normalScale

    I start by caching TAN_FOV_DIV_2, as this is a constant value based on FIELD_OF_VIEW, which will never change (NOTE: if you change Field Of View in the project properties, update it here to match!!), so that we don't need to do the math every time we call GetCameraZ. The rest of the pseudocode above explains the function and the math it uses.

    OK SWEET, now we can get disappearingAngle by doing the following:

    screenCenter = ViewportWidth(CardVisuals.LayerNumber)*(VanishingPointX*0.01)+ ViewportLeft(CardVisuals.LayerNumber)

    disappearingAngle = 180 - angle(CardVisuals.X, CardVisuals.Z, screenCenter, Functions.GetCameraZ(CardVisuals.layerNumber) )

    Now, as I flip the card via its Euler RotationY value, I can watch for RotationY to be > disappearingAngle, and update CardVisuals to show its face sprite! I also have it mirror the sprite when the card's face is showing.

    Here's before and after gifs - before simply uses 90deg, after uses our new disappearingAngle. Watch the cards on each end and you can see them update their visuals a little early or a little late:

    Before | After

    Here's the camera math copied directly from my project:

    {"is-c3-clipboard-data":true,"type":"events","items":[{"eventType":"group","disabled":false,"title":"Camera Math","description":"","isActiveOnStart":true,"children":[{"eventType":"comment","text":"var defaultZ = viewportHeight / (2 * tan(fovY / 2))\nvar normalScale = (layer.scale * layout.scale - 1) * layer.scaleRate + 1\nvar cameraZ = defaultZ / normalScale"},{"eventType":"variable","name":"FIELD_OF_VIEW","type":"number","initialValue":"45","comment":"","isStatic":true,"isConstant":true},{"eventType":"variable","name":"TAN_FOV_DIV_2","type":"number","initialValue":"0","comment":"","isStatic":true,"isConstant":false},{"eventType":"block","conditions":[{"id":"on-start-of-layout","objectClass":"System"}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"TAN_FOV_DIV_2","value":"tan(FIELD_OF_VIEW/2)"}}]},{"functionName":"GetCameraZ","functionDescription":"Get 2D camera's z height","functionCategory":"","functionReturnType":"number","functionCopyPicked":false,"functionIsAsync":false,"functionParameters":[{"name":"layerID","type":"number","initialValue":"0","comment":""}],"eventType":"function-block","conditions":[],"actions":[],"children":[{"eventType":"variable","name":"normalScale","type":"number","initialValue":"0","comment":"","isStatic":false,"isConstant":false},{"eventType":"variable","name":"defaultZ","type":"number","initialValue":"0","comment":"","isStatic":false,"isConstant":false},{"eventType":"block","conditions":[],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"normalScale","value":"(LayerScale(layerID) * LayoutScale-1) * LayerScaleRate(layerID) + 1"}},{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"defaultZ","value":"ViewportHeight(layerID) / (2 * TAN_FOV_DIV_2)"}},{"id":"set-function-return-value","objectClass":"Functions","parameters":{"value":"defaultZ / normalScale"}}]}]},{"eventType":"block","conditions":[],"actions":[]}]}]}

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)