A-Frame Trigger javascript function on collide with camera










3















I am trying to trigger my score function whenever the camera collide or touch an object like:



<a-entity id="rock" static-body obj-model="obj:models/rock_mesh.obj;mtl:images/rock_mesh.mtl" rotation="0 90 0" position="7.30242379045994 0.3 0">
</a-entity>


I equip my a score text on my camera:



 <a-text id="score" value="0" position="-0.2 -0.5 -1" color="red" width="5" anchor="left"></a-text>


And trying to trigger a function like this:



let score = 0;
score = score + 1
$("#score").setAttribute('text','value','Score '+score)


This is just a draft up code, I am still new to javascript



How can i do this? Incrementing the score on the screen whenever my camera touches this "rock" object?



How can i detect the collision or touch with the object and my camera?



Thanks in advance.










share|improve this question




























    3















    I am trying to trigger my score function whenever the camera collide or touch an object like:



    <a-entity id="rock" static-body obj-model="obj:models/rock_mesh.obj;mtl:images/rock_mesh.mtl" rotation="0 90 0" position="7.30242379045994 0.3 0">
    </a-entity>


    I equip my a score text on my camera:



     <a-text id="score" value="0" position="-0.2 -0.5 -1" color="red" width="5" anchor="left"></a-text>


    And trying to trigger a function like this:



    let score = 0;
    score = score + 1
    $("#score").setAttribute('text','value','Score '+score)


    This is just a draft up code, I am still new to javascript



    How can i do this? Incrementing the score on the screen whenever my camera touches this "rock" object?



    How can i detect the collision or touch with the object and my camera?



    Thanks in advance.










    share|improve this question


























      3












      3








      3


      2






      I am trying to trigger my score function whenever the camera collide or touch an object like:



      <a-entity id="rock" static-body obj-model="obj:models/rock_mesh.obj;mtl:images/rock_mesh.mtl" rotation="0 90 0" position="7.30242379045994 0.3 0">
      </a-entity>


      I equip my a score text on my camera:



       <a-text id="score" value="0" position="-0.2 -0.5 -1" color="red" width="5" anchor="left"></a-text>


      And trying to trigger a function like this:



      let score = 0;
      score = score + 1
      $("#score").setAttribute('text','value','Score '+score)


      This is just a draft up code, I am still new to javascript



      How can i do this? Incrementing the score on the screen whenever my camera touches this "rock" object?



      How can i detect the collision or touch with the object and my camera?



      Thanks in advance.










      share|improve this question
















      I am trying to trigger my score function whenever the camera collide or touch an object like:



      <a-entity id="rock" static-body obj-model="obj:models/rock_mesh.obj;mtl:images/rock_mesh.mtl" rotation="0 90 0" position="7.30242379045994 0.3 0">
      </a-entity>


      I equip my a score text on my camera:



       <a-text id="score" value="0" position="-0.2 -0.5 -1" color="red" width="5" anchor="left"></a-text>


      And trying to trigger a function like this:



      let score = 0;
      score = score + 1
      $("#score").setAttribute('text','value','Score '+score)


      This is just a draft up code, I am still new to javascript



      How can i do this? Incrementing the score on the screen whenever my camera touches this "rock" object?



      How can i detect the collision or touch with the object and my camera?



      Thanks in advance.







      javascript jquery html aframe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 3 '18 at 22:45









      Jack Bashford

      6,91031336




      6,91031336










      asked Nov 11 '18 at 4:36









      TYCTYC

      9618




      9618






















          2 Answers
          2






          active

          oldest

          votes


















          3














          The simplest way to detect collisions would be detecting if the THREE bounding boxes are overlapping



          You can use Ngo Kevins aabb-collider which emits hitstart upon collision. Remember though, the camera does not have it's own geometry:



          <a-camera foo geometry="primitive: box" aabb-collider="objects: a-box"></a-camera>
          <a-box scale="2 2 2" class="box" color="blue" position="0 1.6 -5" ></a-box>


          with foo being a simple event listener for hitstart.



          AFRAME.registerComponent("foo", 
          init: function()
          this.el.addEventListener("hitstart", (e)=>
          // Collision ! increment the score
          )

          )


          Fiddle here.



          If possible, I wouldn't detect collisions with your model, but create some collision boxes.




          It's also worth mentioning, if You'd want to use a physics engine in your project, Don McCurdys Physics System also enables collision detection. Instead of hitstart, You'd need to listen for collision.






          share|improve this answer

























          • nice,Ngo Kevins is what i am looking for

            – TYC
            Nov 11 '18 at 11:18












          • glad i could help!

            – Piotr Adam Milewski
            Nov 11 '18 at 11:31


















          1














          Just do this:



          score++;
          $("#score").attr("value", score);





          share|improve this answer























          • how do i detect the collision or touch with the object, so i can call the function afterward

            – TYC
            Nov 11 '18 at 9:39











          • Look at the above answer

            – Jack Bashford
            Nov 11 '18 at 20:16










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245876%2fa-frame-trigger-javascript-function-on-collide-with-camera%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          The simplest way to detect collisions would be detecting if the THREE bounding boxes are overlapping



          You can use Ngo Kevins aabb-collider which emits hitstart upon collision. Remember though, the camera does not have it's own geometry:



          <a-camera foo geometry="primitive: box" aabb-collider="objects: a-box"></a-camera>
          <a-box scale="2 2 2" class="box" color="blue" position="0 1.6 -5" ></a-box>


          with foo being a simple event listener for hitstart.



          AFRAME.registerComponent("foo", 
          init: function()
          this.el.addEventListener("hitstart", (e)=>
          // Collision ! increment the score
          )

          )


          Fiddle here.



          If possible, I wouldn't detect collisions with your model, but create some collision boxes.




          It's also worth mentioning, if You'd want to use a physics engine in your project, Don McCurdys Physics System also enables collision detection. Instead of hitstart, You'd need to listen for collision.






          share|improve this answer

























          • nice,Ngo Kevins is what i am looking for

            – TYC
            Nov 11 '18 at 11:18












          • glad i could help!

            – Piotr Adam Milewski
            Nov 11 '18 at 11:31















          3














          The simplest way to detect collisions would be detecting if the THREE bounding boxes are overlapping



          You can use Ngo Kevins aabb-collider which emits hitstart upon collision. Remember though, the camera does not have it's own geometry:



          <a-camera foo geometry="primitive: box" aabb-collider="objects: a-box"></a-camera>
          <a-box scale="2 2 2" class="box" color="blue" position="0 1.6 -5" ></a-box>


          with foo being a simple event listener for hitstart.



          AFRAME.registerComponent("foo", 
          init: function()
          this.el.addEventListener("hitstart", (e)=>
          // Collision ! increment the score
          )

          )


          Fiddle here.



          If possible, I wouldn't detect collisions with your model, but create some collision boxes.




          It's also worth mentioning, if You'd want to use a physics engine in your project, Don McCurdys Physics System also enables collision detection. Instead of hitstart, You'd need to listen for collision.






          share|improve this answer

























          • nice,Ngo Kevins is what i am looking for

            – TYC
            Nov 11 '18 at 11:18












          • glad i could help!

            – Piotr Adam Milewski
            Nov 11 '18 at 11:31













          3












          3








          3







          The simplest way to detect collisions would be detecting if the THREE bounding boxes are overlapping



          You can use Ngo Kevins aabb-collider which emits hitstart upon collision. Remember though, the camera does not have it's own geometry:



          <a-camera foo geometry="primitive: box" aabb-collider="objects: a-box"></a-camera>
          <a-box scale="2 2 2" class="box" color="blue" position="0 1.6 -5" ></a-box>


          with foo being a simple event listener for hitstart.



          AFRAME.registerComponent("foo", 
          init: function()
          this.el.addEventListener("hitstart", (e)=>
          // Collision ! increment the score
          )

          )


          Fiddle here.



          If possible, I wouldn't detect collisions with your model, but create some collision boxes.




          It's also worth mentioning, if You'd want to use a physics engine in your project, Don McCurdys Physics System also enables collision detection. Instead of hitstart, You'd need to listen for collision.






          share|improve this answer















          The simplest way to detect collisions would be detecting if the THREE bounding boxes are overlapping



          You can use Ngo Kevins aabb-collider which emits hitstart upon collision. Remember though, the camera does not have it's own geometry:



          <a-camera foo geometry="primitive: box" aabb-collider="objects: a-box"></a-camera>
          <a-box scale="2 2 2" class="box" color="blue" position="0 1.6 -5" ></a-box>


          with foo being a simple event listener for hitstart.



          AFRAME.registerComponent("foo", 
          init: function()
          this.el.addEventListener("hitstart", (e)=>
          // Collision ! increment the score
          )

          )


          Fiddle here.



          If possible, I wouldn't detect collisions with your model, but create some collision boxes.




          It's also worth mentioning, if You'd want to use a physics engine in your project, Don McCurdys Physics System also enables collision detection. Instead of hitstart, You'd need to listen for collision.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 '18 at 11:31

























          answered Nov 11 '18 at 10:41









          Piotr Adam MilewskiPiotr Adam Milewski

          5,38021226




          5,38021226












          • nice,Ngo Kevins is what i am looking for

            – TYC
            Nov 11 '18 at 11:18












          • glad i could help!

            – Piotr Adam Milewski
            Nov 11 '18 at 11:31

















          • nice,Ngo Kevins is what i am looking for

            – TYC
            Nov 11 '18 at 11:18












          • glad i could help!

            – Piotr Adam Milewski
            Nov 11 '18 at 11:31
















          nice,Ngo Kevins is what i am looking for

          – TYC
          Nov 11 '18 at 11:18






          nice,Ngo Kevins is what i am looking for

          – TYC
          Nov 11 '18 at 11:18














          glad i could help!

          – Piotr Adam Milewski
          Nov 11 '18 at 11:31





          glad i could help!

          – Piotr Adam Milewski
          Nov 11 '18 at 11:31













          1














          Just do this:



          score++;
          $("#score").attr("value", score);





          share|improve this answer























          • how do i detect the collision or touch with the object, so i can call the function afterward

            – TYC
            Nov 11 '18 at 9:39











          • Look at the above answer

            – Jack Bashford
            Nov 11 '18 at 20:16















          1














          Just do this:



          score++;
          $("#score").attr("value", score);





          share|improve this answer























          • how do i detect the collision or touch with the object, so i can call the function afterward

            – TYC
            Nov 11 '18 at 9:39











          • Look at the above answer

            – Jack Bashford
            Nov 11 '18 at 20:16













          1












          1








          1







          Just do this:



          score++;
          $("#score").attr("value", score);





          share|improve this answer













          Just do this:



          score++;
          $("#score").attr("value", score);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 '18 at 5:00









          Jack BashfordJack Bashford

          6,91031336




          6,91031336












          • how do i detect the collision or touch with the object, so i can call the function afterward

            – TYC
            Nov 11 '18 at 9:39











          • Look at the above answer

            – Jack Bashford
            Nov 11 '18 at 20:16

















          • how do i detect the collision or touch with the object, so i can call the function afterward

            – TYC
            Nov 11 '18 at 9:39











          • Look at the above answer

            – Jack Bashford
            Nov 11 '18 at 20:16
















          how do i detect the collision or touch with the object, so i can call the function afterward

          – TYC
          Nov 11 '18 at 9:39





          how do i detect the collision or touch with the object, so i can call the function afterward

          – TYC
          Nov 11 '18 at 9:39













          Look at the above answer

          – Jack Bashford
          Nov 11 '18 at 20:16





          Look at the above answer

          – Jack Bashford
          Nov 11 '18 at 20:16

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245876%2fa-frame-trigger-javascript-function-on-collide-with-camera%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Edmonton

          Crossroads (UK TV series)