{"id":752,"date":"2022-10-14T10:34:32","date_gmt":"2022-10-14T08:34:32","guid":{"rendered":"https:\/\/betalord.com\/code\/?p=752"},"modified":"2023-04-05T19:57:39","modified_gmt":"2023-04-05T17:57:39","slug":"libgdx-is-actor-in-front-of-another","status":"publish","type":"post","link":"https:\/\/betalord.com\/code\/2022\/10\/14\/libgdx-is-actor-in-front-of-another\/","title":{"rendered":"Is actor in front of another?"},"content":{"rendered":"\n<p>Actors in LibGDX have a z-order, which is basically just their position in the Stage\u2019s children list. Actor\u2019s methods <code data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">toFront()<\/code> and <code data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">toBack()<\/code> call <code data-enlighter-language=\"java\" class=\"EnlighterJSRAW\">Actor.setZIndex()<\/code> which in turn move the actor within its parent group\u2019s children list. Actors with lower z index are drawn below actors with higher z index.<\/p>\n\n\n\n<p>Now the question is, how can I figure out if some actor is in front of another actor?<\/p>\n\n\n\n<p>LibGDX doesn\u2019t offer a direct way of doing this, so we need to do it manually. Here\u2019s the code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    \/**\n     * Returns true in case actor 'a' is in front of actor 'b' in given stage.\n     * Note that both actors must be in given stage (and hence have a common ancestor).\n     * @param a is actor a in front of actor b?\n     * @param b is actor a in front of actor b?\n     * @param stage stage in which both given actors reside\n     * @return true in case 'a' is in front of 'b'\n     * @throws IllegalArgumentException in case given actors don't have common ancestor\n     *\/\n    public static boolean isActorInFrontOfAnother(Actor a, Actor b, Stage stage) {\n    \tif (a == b) \/\/ special case\n    \t\treturn false;\n    \t\n    \tList&lt;Actor> g1 = new ArrayList&lt;>(); \/\/ genealogy of actor a. First item in the list (index 0) is actor a, then his parent, then his parent's parent, and so on, until stage's root table\n    \tList&lt;Actor> g2 = new ArrayList&lt;>(); \/\/ same as g1, but for actor b \n    \t\n    \tActor root = stage.getRoot();\n    \t\n    \tg1.add(a);\n    \tActor node = a;\n    \twhile (node.getParent() != root) {\n    \t\tg1.add(node.getParent());\n    \t\tnode = node.getParent();\n    \t}\n    \tg1.add(root);\n    \t\n    \tg2.add(b);\n    \tnode = b;\n    \twhile (node.getParent() != root) {\n    \t\tg2.add(node.getParent());\n    \t\tnode = node.getParent();\n    \t}\n    \tg2.add(root);\n    \t\n    \t\/\/ now find first common parent:\n    \tActor p1, p2; \/\/ (grand*)parents of actors a and b\n    \tint i = g1.size()-1;\n    \tint j = g2.size()-1;\n    \twhile (true) {\n    \t\tp1 = g1.get(i);\n    \t\tp2 = g2.get(j);\n    \t\tif (p1 != p2)\n    \t\t\tbreak;\n    \t\ti--;\n    \t\tj--;\n    \t\tif (i &lt; 0 || j &lt; 0)\n    \t\t\tthrow new IllegalArgumentException(\"Given actors don't have common ancestor\");\n    \t}\n    \t\n    \t\/\/ Actors p1 and p2 are both children of last common parent of a and b. Actors a and b derive from p1 and p2 (p1 and p2 could be a and b themselves too, if there are no other parents in between).\n    \treturn p1.getZIndex() > p2.getZIndex();\n    }<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Actors in LibGDX have a z-order, which is basically just their position in the Stage\u2019s children list. Actor\u2019s methods toFront() and toBack() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[10],"class_list":["post-752","post","type-post","status-publish","format-standard","hentry","category-article","tag-libgdx"],"_links":{"self":[{"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/posts\/752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/comments?post=752"}],"version-history":[{"count":32,"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/posts\/752\/revisions"}],"predecessor-version":[{"id":866,"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/posts\/752\/revisions\/866"}],"wp:attachment":[{"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/media?parent=752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/categories?post=752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/tags?post=752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}