A-Frame Trigger javascript function on collide with camera











up vote
3
down vote

favorite
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




























    up vote
    3
    down vote

    favorite
    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


























      up vote
      3
      down vote

      favorite
      2









      up vote
      3
      down vote

      favorite
      2






      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 6 hours ago









      Jack Bashford

      3,44631033




      3,44631033










      asked Nov 11 at 4:36









      TYC

      7618




      7618
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          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 at 11:18












          • glad i could help!
            – Piotr Adam Milewski
            Nov 11 at 11:31


















          up vote
          1
          down vote













          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 at 9:39










          • Look at the above answer
            – Jack Bashford
            Nov 11 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',
          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








          up vote
          3
          down vote



          accepted










          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 at 11:18












          • glad i could help!
            – Piotr Adam Milewski
            Nov 11 at 11:31















          up vote
          3
          down vote



          accepted










          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 at 11:18












          • glad i could help!
            – Piotr Adam Milewski
            Nov 11 at 11:31













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          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 at 11:31

























          answered Nov 11 at 10:41









          Piotr Adam Milewski

          5,07221025




          5,07221025












          • nice,Ngo Kevins is what i am looking for
            – TYC
            Nov 11 at 11:18












          • glad i could help!
            – Piotr Adam Milewski
            Nov 11 at 11:31


















          • nice,Ngo Kevins is what i am looking for
            – TYC
            Nov 11 at 11:18












          • glad i could help!
            – Piotr Adam Milewski
            Nov 11 at 11:31
















          nice,Ngo Kevins is what i am looking for
          – TYC
          Nov 11 at 11:18






          nice,Ngo Kevins is what i am looking for
          – TYC
          Nov 11 at 11:18














          glad i could help!
          – Piotr Adam Milewski
          Nov 11 at 11:31




          glad i could help!
          – Piotr Adam Milewski
          Nov 11 at 11:31












          up vote
          1
          down vote













          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 at 9:39










          • Look at the above answer
            – Jack Bashford
            Nov 11 at 20:16















          up vote
          1
          down vote













          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 at 9:39










          • Look at the above answer
            – Jack Bashford
            Nov 11 at 20:16













          up vote
          1
          down vote










          up vote
          1
          down vote









          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 at 5:00









          Jack Bashford

          3,44631033




          3,44631033












          • how do i detect the collision or touch with the object, so i can call the function afterward
            – TYC
            Nov 11 at 9:39










          • Look at the above answer
            – Jack Bashford
            Nov 11 at 20:16


















          • how do i detect the collision or touch with the object, so i can call the function afterward
            – TYC
            Nov 11 at 9:39










          • Look at the above answer
            – Jack Bashford
            Nov 11 at 20:16
















          how do i detect the collision or touch with the object, so i can call the function afterward
          – TYC
          Nov 11 at 9:39




          how do i detect the collision or touch with the object, so i can call the function afterward
          – TYC
          Nov 11 at 9:39












          Look at the above answer
          – Jack Bashford
          Nov 11 at 20:16




          Look at the above answer
          – Jack Bashford
          Nov 11 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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)