# Getting an up-to-date camera transform

**URL:** <https://forum.defold.com/t/getting-an-up-to-date-camera-transform/65068>\
**Category:** Questions\
**Created:** [May 8, 2020, 7:28pm UTC](https://forum.defold.com/t/getting-an-up-to-date-camera-transform/65068 "2020-05-08T19:28:06Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ross.grams](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@ross.grams](https://forum.defold.com/u/ross.grams)\
**Post date:** [May 8, 2020, 7:28pm UTC](https://forum.defold.com/t/getting-an-up-to-date-camera-transform/65068/1 "2020-05-08T19:28:06Z")

</div>

I’m trying to improve RenderCam, but there doesn’t seem to be any decent way to get an up-to-date object transform for rendering – i.e. a transform that’s not one frame behind.

It’s the same problem as in this thread: [Dealing with the one frame lag when visualising dynamic bodies](https://forum.defold.com/t/dealing-with-the-one-frame-lag-when-visualising-dynamic-bodies/64957)

**The root of the problem is:**  
A) There’s no script callback _after_ physics update but _before_ render update  
B) The render script doesn’t have access to ‘go.’ functions.

From what I can tell, the relevant part of the engine’s update loop goes like this:

1. script update
2. script on\_message (get message sent to self on update)
3. physics update (get physics messages in on\_message)
4. render script can get message from active camera component
5. render script update

Object transforms are updated between each of the first 4 steps.

- Anytime you move the camera, its world transform becomes out of date until the next point in the engine’s update cycle.

- So, if your camera is attached to a physics object, or if you move it during any physics message response, you have no way of getting it’s updated transform.

- The built-in camera component _does_ work—it messages its View and Projection matrices to the render script after the physics update—the problem is, only one active camera does this, and I want to support multiple cameras at once (for split-screen, minimaps, rear-view mirrors, etc.).

Currently my camera sends a message to itself in update(), and updates its position and stuff when it gets that message (making its world transform out-of-date). I haven’t tested the exact reasons for that in a while, but I recall it jittered like crazy if I didn’t do it. The only thing after that to hook into is physics messages. So the only current solution I can see, is to do a physics raycast every frame and get the transform in the “ray\_cast\_missed” message response. That’s a bit more “hacky” than I’m willing to be at the moment.

* * *

**Possible solutions**

The easiest fix I can see is to allow render scripts to get info about objects. In my specific case, if the render script could do `go.get_world_transform()`, then I think my problem would be solved. (Of course access to other getters would be good.)

The other way is to add another “post\_update” script callback, called after physics are updated but before rendering.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [May 8, 2020, 7:52pm UTC](https://forum.defold.com/t/getting-an-up-to-date-camera-transform/65068/2 "2020-05-08T19:52:30Z")

</div>

A nice post\_update would be lovely, and would solve a lot of issues like this!

---

<div class="post-metadata">

**Author:** ![dmitriy](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dmitriy/32/18452_2.png) [@dmitriy](https://forum.defold.com/u/dmitriy)\
**Post date:** [May 8, 2020, 9:51pm UTC](https://forum.defold.com/t/getting-an-up-to-date-camera-transform/65068/3 "2020-05-08T21:51:38Z")

</div>

> [@ross.grams](#):
>
> if the render script could do `go.get_world_transform()` , then I think my problem would be solved. (Of course access to other getters would be good.)

This is probably not what you expect, but…  
You can do this in render script:

```lua
if shared_state_module.some_go_context then -- context == self
   local current = _G[3700146495]
   _G[3700146495] = shared_state_module.some_go_context
   local result = go.get_world_transform()
   _G[3700146495] = current
   print(result)
end

```

or make extension:

```lua
static
int execute_in_context(lua_State *L) {
   dmScript::GetInstance(L);
   int self = dmScript::Ref(L, LUA_REGISTRYINDEX);

   lua_pushvalue(L, 2);
   dmScript::SetInstance(L);

   lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
   int results_count = lua_gettop(L);

   lua_rawgeti(L, LUA_REGISTRYINDEX, self);
   dmScript::SetInstance(L);
   dmScript::Unref(L, LUA_REGISTRYINDEX, self);

   return results_count;
}

```

what used like this:

```lua
extension.execute_in_context(run_what, in_which_context, param1, param2, etc...))

```

---

<div class="post-metadata">

**Author:** ![ross.grams](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@ross.grams](https://forum.defold.com/u/ross.grams)\
**Post date:** [May 8, 2020, 10:53pm UTC](https://forum.defold.com/t/getting-an-up-to-date-camera-transform/65068/4 "2020-05-08T22:53:58Z")

</div>

Haha! You know, I wondered if there was some sneaky way to change the context like this, but hadn’t looked for it yet. I probably wouldn’t have figured out what that “3700146495” key was for though.  
Thanks! I might just use this.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [May 9, 2020, 5:39am UTC](https://forum.defold.com/t/getting-an-up-to-date-camera-transform/65068/5 "2020-05-09T05:39:46Z")

</div>

> <https://github.com/britzl/gooey/blob/master/gooey/internal/core.lua#L113>

It’s different for HTML5?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [May 9, 2020, 11:31am UTC](https://forum.defold.com/t/getting-an-up-to-date-camera-transform/65068/6 "2020-05-09T11:31:09Z")

</div>

> [@Pkeod](#):
>
> It’s different for HTML5?

Lua 5.1 vs LuaJIT

---

<div class="post-metadata">

**Author:** ![ross.grams](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@ross.grams](https://forum.defold.com/u/ross.grams)\
**Post date:** [May 9, 2020, 4:59pm UTC](https://forum.defold.com/t/getting-an-up-to-date-camera-transform/65068/7 "2020-05-09T16:59:21Z")

</div>

@britzl So is this something I can rely on, or is it likely to get broken at some point?

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [May 9, 2020, 6:00pm UTC](https://forum.defold.com/t/getting-an-up-to-date-camera-transform/65068/8 "2020-05-09T18:00:25Z")

</div>

I think using that ids from lua is bad idea)  
This is undocumented feature, and can be changed in future.

You should use native extension for that.  
[https://github.com/DanEngelbrecht/LuaScriptInstance](https://github.com/DanEngelbrecht/LuaScriptInstance)

I used it a lot. And it worked))

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [May 9, 2020, 9:08pm UTC](https://forum.defold.com/t/getting-an-up-to-date-camera-transform/65068/9 "2020-05-09T21:08:21Z")

</div>

> [@ross.grams](#):
>
> So is this something I can rely on, or is it likely to get broken at some point?

> [@d954mas](#):
>
> I think using that ids from lua is bad idea)  
> This is undocumented feature, and can be changed in future.

What @d954mas said! Don’t rely on it! The native extension approach is reliable though. I’m making a note to consider this to be integrated in the engine somehow.

---

<div class="post-metadata">

**Author:** ![Jerakin](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jerakin/32/3997_2.png) [@Jerakin](https://forum.defold.com/u/Jerakin)\
**Post date:** [July 8, 2020, 4:39pm UTC](https://forum.defold.com/t/getting-an-up-to-date-camera-transform/65068/10 "2020-07-08T16:39:30Z")

</div>

> [@britzl](#):
>
> Don’t rely on it!

Sometimes you have to though. In some instances calling a Native Extension will switch the context so you will have to “force it back”, issue I reported on it [Using window.set\_listener with NE that changes your window makes the NE switch script context · Issue #5000 · defold/defold · GitHub](https://github.com/defold/editor2-issues/issues/2616)

I haven’t heard anyone else mentioning this so maybe it isn’t very common but I stumble upon it from time to time.

(sorry for the necro)

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [July 8, 2020, 9:55pm UTC](https://forum.defold.com/t/getting-an-up-to-date-camera-transform/65068/11 "2020-07-08T21:55:25Z")

</div>

> [@Jerakin](#):
>
> In some instances calling a Native Extension will switch the context so you will have to “force it back”, issue I reported on it [Using window.set\_listener with NE that changes your window makes the NE switch script context · Issue #5000 · defold/defold · GitHub](https://github.com/defold/editor2-issues/issues/2616)

I’d like to look into this. Could you please provide a minimal project reproducing this behaviour?

---

<div class="post-metadata">

**Author:** ![Jerakin](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jerakin/32/3997_2.png) [@Jerakin](https://forum.defold.com/u/Jerakin)\
**Post date:** [July 8, 2020, 9:56pm UTC](https://forum.defold.com/t/getting-an-up-to-date-camera-transform/65068/12 "2020-07-08T21:56:20Z")

</div>

Already included in the issue 🙂

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [July 9, 2020, 8:51am UTC](https://forum.defold.com/t/getting-an-up-to-date-camera-transform/65068/13 "2020-07-09T08:51:31Z")

</div>

> [@Jerakin](#):
>
> Already included in the issue

Thanks!

I have transferred the issue to the Defold repo: [Using window.set\_listener with NE that changes your window makes the NE switch script context · Issue #5000 · defold/defold · GitHub](https://github.com/defold/defold/issues/5000)
