Creatin a shotgun spread pattern (babylon.js)










0















Hello I'm new to babylonjs and I've been trying to make a constant shotgun spread pattern in babylon.js for a couple of days.



For the moment my approach consists of having a couple of lines ready to be displayed in an array :



 let sgPellets = [
BABYLON.Mesh.CreateLines("lines", [
shotgunPosition,
meshFound.pickedPoint
], this.Player.game.scene),
BABYLON.Mesh.CreateLines("lines", [
shotgunPosition,
meshFound.pickedPoint
], this.Player.game.scene),
]


where shotgunPosition is the absolute position of the shotgun mesh in the world and meshFound.pickedPoint is the position of where I'm aiming at (on a mesh).



So I can draw a line (the first of the array in this case) between the shotgun mesh and the mesh I click on.



Now for the second line, I want it to rotate a bit on the y axis from the first (which is exactly where I'm aiming at) so:



 for(let k=0; k<sgPellets.length; k++)
sgPellets[k].setPivotPoint(shotgunPosition);
sgPellets[k].isPickable = false;
switch(k)
case 1:
sgPellets[k].rotation.y += 0.1
break




It does work well when I look on the sides enter image description here



but not when I look up or down.enter image description here



I think I'm missing something on the math side.
I've also tried using other rotation methods, for the same kind of result.



Does anyone have any hints on how to make the angle constant between the two lines ? Thank you.










share|improve this question


























    0















    Hello I'm new to babylonjs and I've been trying to make a constant shotgun spread pattern in babylon.js for a couple of days.



    For the moment my approach consists of having a couple of lines ready to be displayed in an array :



     let sgPellets = [
    BABYLON.Mesh.CreateLines("lines", [
    shotgunPosition,
    meshFound.pickedPoint
    ], this.Player.game.scene),
    BABYLON.Mesh.CreateLines("lines", [
    shotgunPosition,
    meshFound.pickedPoint
    ], this.Player.game.scene),
    ]


    where shotgunPosition is the absolute position of the shotgun mesh in the world and meshFound.pickedPoint is the position of where I'm aiming at (on a mesh).



    So I can draw a line (the first of the array in this case) between the shotgun mesh and the mesh I click on.



    Now for the second line, I want it to rotate a bit on the y axis from the first (which is exactly where I'm aiming at) so:



     for(let k=0; k<sgPellets.length; k++)
    sgPellets[k].setPivotPoint(shotgunPosition);
    sgPellets[k].isPickable = false;
    switch(k)
    case 1:
    sgPellets[k].rotation.y += 0.1
    break




    It does work well when I look on the sides enter image description here



    but not when I look up or down.enter image description here



    I think I'm missing something on the math side.
    I've also tried using other rotation methods, for the same kind of result.



    Does anyone have any hints on how to make the angle constant between the two lines ? Thank you.










    share|improve this question
























      0












      0








      0


      0






      Hello I'm new to babylonjs and I've been trying to make a constant shotgun spread pattern in babylon.js for a couple of days.



      For the moment my approach consists of having a couple of lines ready to be displayed in an array :



       let sgPellets = [
      BABYLON.Mesh.CreateLines("lines", [
      shotgunPosition,
      meshFound.pickedPoint
      ], this.Player.game.scene),
      BABYLON.Mesh.CreateLines("lines", [
      shotgunPosition,
      meshFound.pickedPoint
      ], this.Player.game.scene),
      ]


      where shotgunPosition is the absolute position of the shotgun mesh in the world and meshFound.pickedPoint is the position of where I'm aiming at (on a mesh).



      So I can draw a line (the first of the array in this case) between the shotgun mesh and the mesh I click on.



      Now for the second line, I want it to rotate a bit on the y axis from the first (which is exactly where I'm aiming at) so:



       for(let k=0; k<sgPellets.length; k++)
      sgPellets[k].setPivotPoint(shotgunPosition);
      sgPellets[k].isPickable = false;
      switch(k)
      case 1:
      sgPellets[k].rotation.y += 0.1
      break




      It does work well when I look on the sides enter image description here



      but not when I look up or down.enter image description here



      I think I'm missing something on the math side.
      I've also tried using other rotation methods, for the same kind of result.



      Does anyone have any hints on how to make the angle constant between the two lines ? Thank you.










      share|improve this question














      Hello I'm new to babylonjs and I've been trying to make a constant shotgun spread pattern in babylon.js for a couple of days.



      For the moment my approach consists of having a couple of lines ready to be displayed in an array :



       let sgPellets = [
      BABYLON.Mesh.CreateLines("lines", [
      shotgunPosition,
      meshFound.pickedPoint
      ], this.Player.game.scene),
      BABYLON.Mesh.CreateLines("lines", [
      shotgunPosition,
      meshFound.pickedPoint
      ], this.Player.game.scene),
      ]


      where shotgunPosition is the absolute position of the shotgun mesh in the world and meshFound.pickedPoint is the position of where I'm aiming at (on a mesh).



      So I can draw a line (the first of the array in this case) between the shotgun mesh and the mesh I click on.



      Now for the second line, I want it to rotate a bit on the y axis from the first (which is exactly where I'm aiming at) so:



       for(let k=0; k<sgPellets.length; k++)
      sgPellets[k].setPivotPoint(shotgunPosition);
      sgPellets[k].isPickable = false;
      switch(k)
      case 1:
      sgPellets[k].rotation.y += 0.1
      break




      It does work well when I look on the sides enter image description here



      but not when I look up or down.enter image description here



      I think I'm missing something on the math side.
      I've also tried using other rotation methods, for the same kind of result.



      Does anyone have any hints on how to make the angle constant between the two lines ? Thank you.







      javascript 3d babylonjs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 1:57









      AngelAngel

      244




      244






















          2 Answers
          2






          active

          oldest

          votes


















          1














          Thank you for you're answer ! You guessed it, I am using scene.pick. I've tested you're approach and it works pretty well, I was also able to create a cone between the barrel and the point picked in the scene. But i wasn't really satisfied. Though I think you're method wouldn't work at making a consistant pattern wherever the picked point is, so I ended up doing something else. I've created a second "fake" camera which rotate and position exactly where the real one is thanks to :



          fakeCamera.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(camera.rotation.y, camera.rotation.x, camera.rotation.z)



          This way I could add a .rotationQuaternion to the fake one with position and converted rotation from the real one (I couldn't add a quaternion for the real camera, It would messed up the controls). In the end I was able to position each rays according to the pickedPoint in the scene and the rotationQuaternion value of the fake camera.






          share|improve this answer























          • Cool, I'm glad you found a solution to your problem and yeah my solution wouldn't work if you wanted to create a consistent pattern every time.

            – Mike
            Nov 21 '18 at 0:54











          • Btw, since you've found your answer you should accept your answer

            – Mike
            Nov 24 '18 at 1:07


















          0














          I'm guessing you're using scene.pick to get meshFound.pickedPoint (if you are not then ignore this answer) and the function takes in a x and y coordinate of a point on the screen then turns that point into a point in 3d space.



          I'm guessing your doing something like:



          var width = scene.getEngine().getRenderWidth();
          var height = scene.getEngine().getRenderHeight();
          var meshFound = scene.pick((width / 2), (height / 2), null, false, camera);


          And that will get the same point in the middle of the screen every time, but this is what I'm suggesting:



          var weaponAccuracy = 200;
          var numShotgunPelletsPerBullet = 10;
          var width = scene.getEngine().getRenderWidth();
          var height = scene.getEngine().getRenderHeight();
          for (var i = 0; i < numShotgunPelletsPerBullet; i++)
          var accuracyX = Math.round((Math.random() * weaponAccuracy) - (weaponAccuracy / 2));
          var accuracyY = Math.round((Math.random() * weaponAccuracy) - (weaponAccuracy / 2));

          var meshFound = scene.pick((width / 2) + accuracyX, (height / 2) + accuracyY, null, false, camera);
          console.log(meshFound.pickedPoint);



          This will pick a random point in a 200 by 200px square around the middle of the screen 10 times.



          If you want to change the accuracy increase or decrease weaponAccuracy.



          And if you want to change the number of pellets in each bullet change numShotgunPelletsPerBullet.






          share|improve this answer






















            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%2f53272683%2fcreatin-a-shotgun-spread-pattern-babylon-js%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









            1














            Thank you for you're answer ! You guessed it, I am using scene.pick. I've tested you're approach and it works pretty well, I was also able to create a cone between the barrel and the point picked in the scene. But i wasn't really satisfied. Though I think you're method wouldn't work at making a consistant pattern wherever the picked point is, so I ended up doing something else. I've created a second "fake" camera which rotate and position exactly where the real one is thanks to :



            fakeCamera.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(camera.rotation.y, camera.rotation.x, camera.rotation.z)



            This way I could add a .rotationQuaternion to the fake one with position and converted rotation from the real one (I couldn't add a quaternion for the real camera, It would messed up the controls). In the end I was able to position each rays according to the pickedPoint in the scene and the rotationQuaternion value of the fake camera.






            share|improve this answer























            • Cool, I'm glad you found a solution to your problem and yeah my solution wouldn't work if you wanted to create a consistent pattern every time.

              – Mike
              Nov 21 '18 at 0:54











            • Btw, since you've found your answer you should accept your answer

              – Mike
              Nov 24 '18 at 1:07















            1














            Thank you for you're answer ! You guessed it, I am using scene.pick. I've tested you're approach and it works pretty well, I was also able to create a cone between the barrel and the point picked in the scene. But i wasn't really satisfied. Though I think you're method wouldn't work at making a consistant pattern wherever the picked point is, so I ended up doing something else. I've created a second "fake" camera which rotate and position exactly where the real one is thanks to :



            fakeCamera.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(camera.rotation.y, camera.rotation.x, camera.rotation.z)



            This way I could add a .rotationQuaternion to the fake one with position and converted rotation from the real one (I couldn't add a quaternion for the real camera, It would messed up the controls). In the end I was able to position each rays according to the pickedPoint in the scene and the rotationQuaternion value of the fake camera.






            share|improve this answer























            • Cool, I'm glad you found a solution to your problem and yeah my solution wouldn't work if you wanted to create a consistent pattern every time.

              – Mike
              Nov 21 '18 at 0:54











            • Btw, since you've found your answer you should accept your answer

              – Mike
              Nov 24 '18 at 1:07













            1












            1








            1







            Thank you for you're answer ! You guessed it, I am using scene.pick. I've tested you're approach and it works pretty well, I was also able to create a cone between the barrel and the point picked in the scene. But i wasn't really satisfied. Though I think you're method wouldn't work at making a consistant pattern wherever the picked point is, so I ended up doing something else. I've created a second "fake" camera which rotate and position exactly where the real one is thanks to :



            fakeCamera.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(camera.rotation.y, camera.rotation.x, camera.rotation.z)



            This way I could add a .rotationQuaternion to the fake one with position and converted rotation from the real one (I couldn't add a quaternion for the real camera, It would messed up the controls). In the end I was able to position each rays according to the pickedPoint in the scene and the rotationQuaternion value of the fake camera.






            share|improve this answer













            Thank you for you're answer ! You guessed it, I am using scene.pick. I've tested you're approach and it works pretty well, I was also able to create a cone between the barrel and the point picked in the scene. But i wasn't really satisfied. Though I think you're method wouldn't work at making a consistant pattern wherever the picked point is, so I ended up doing something else. I've created a second "fake" camera which rotate and position exactly where the real one is thanks to :



            fakeCamera.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(camera.rotation.y, camera.rotation.x, camera.rotation.z)



            This way I could add a .rotationQuaternion to the fake one with position and converted rotation from the real one (I couldn't add a quaternion for the real camera, It would messed up the controls). In the end I was able to position each rays according to the pickedPoint in the scene and the rotationQuaternion value of the fake camera.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 9:53









            AngelAngel

            244




            244












            • Cool, I'm glad you found a solution to your problem and yeah my solution wouldn't work if you wanted to create a consistent pattern every time.

              – Mike
              Nov 21 '18 at 0:54











            • Btw, since you've found your answer you should accept your answer

              – Mike
              Nov 24 '18 at 1:07

















            • Cool, I'm glad you found a solution to your problem and yeah my solution wouldn't work if you wanted to create a consistent pattern every time.

              – Mike
              Nov 21 '18 at 0:54











            • Btw, since you've found your answer you should accept your answer

              – Mike
              Nov 24 '18 at 1:07
















            Cool, I'm glad you found a solution to your problem and yeah my solution wouldn't work if you wanted to create a consistent pattern every time.

            – Mike
            Nov 21 '18 at 0:54





            Cool, I'm glad you found a solution to your problem and yeah my solution wouldn't work if you wanted to create a consistent pattern every time.

            – Mike
            Nov 21 '18 at 0:54













            Btw, since you've found your answer you should accept your answer

            – Mike
            Nov 24 '18 at 1:07





            Btw, since you've found your answer you should accept your answer

            – Mike
            Nov 24 '18 at 1:07













            0














            I'm guessing you're using scene.pick to get meshFound.pickedPoint (if you are not then ignore this answer) and the function takes in a x and y coordinate of a point on the screen then turns that point into a point in 3d space.



            I'm guessing your doing something like:



            var width = scene.getEngine().getRenderWidth();
            var height = scene.getEngine().getRenderHeight();
            var meshFound = scene.pick((width / 2), (height / 2), null, false, camera);


            And that will get the same point in the middle of the screen every time, but this is what I'm suggesting:



            var weaponAccuracy = 200;
            var numShotgunPelletsPerBullet = 10;
            var width = scene.getEngine().getRenderWidth();
            var height = scene.getEngine().getRenderHeight();
            for (var i = 0; i < numShotgunPelletsPerBullet; i++)
            var accuracyX = Math.round((Math.random() * weaponAccuracy) - (weaponAccuracy / 2));
            var accuracyY = Math.round((Math.random() * weaponAccuracy) - (weaponAccuracy / 2));

            var meshFound = scene.pick((width / 2) + accuracyX, (height / 2) + accuracyY, null, false, camera);
            console.log(meshFound.pickedPoint);



            This will pick a random point in a 200 by 200px square around the middle of the screen 10 times.



            If you want to change the accuracy increase or decrease weaponAccuracy.



            And if you want to change the number of pellets in each bullet change numShotgunPelletsPerBullet.






            share|improve this answer



























              0














              I'm guessing you're using scene.pick to get meshFound.pickedPoint (if you are not then ignore this answer) and the function takes in a x and y coordinate of a point on the screen then turns that point into a point in 3d space.



              I'm guessing your doing something like:



              var width = scene.getEngine().getRenderWidth();
              var height = scene.getEngine().getRenderHeight();
              var meshFound = scene.pick((width / 2), (height / 2), null, false, camera);


              And that will get the same point in the middle of the screen every time, but this is what I'm suggesting:



              var weaponAccuracy = 200;
              var numShotgunPelletsPerBullet = 10;
              var width = scene.getEngine().getRenderWidth();
              var height = scene.getEngine().getRenderHeight();
              for (var i = 0; i < numShotgunPelletsPerBullet; i++)
              var accuracyX = Math.round((Math.random() * weaponAccuracy) - (weaponAccuracy / 2));
              var accuracyY = Math.round((Math.random() * weaponAccuracy) - (weaponAccuracy / 2));

              var meshFound = scene.pick((width / 2) + accuracyX, (height / 2) + accuracyY, null, false, camera);
              console.log(meshFound.pickedPoint);



              This will pick a random point in a 200 by 200px square around the middle of the screen 10 times.



              If you want to change the accuracy increase or decrease weaponAccuracy.



              And if you want to change the number of pellets in each bullet change numShotgunPelletsPerBullet.






              share|improve this answer

























                0












                0








                0







                I'm guessing you're using scene.pick to get meshFound.pickedPoint (if you are not then ignore this answer) and the function takes in a x and y coordinate of a point on the screen then turns that point into a point in 3d space.



                I'm guessing your doing something like:



                var width = scene.getEngine().getRenderWidth();
                var height = scene.getEngine().getRenderHeight();
                var meshFound = scene.pick((width / 2), (height / 2), null, false, camera);


                And that will get the same point in the middle of the screen every time, but this is what I'm suggesting:



                var weaponAccuracy = 200;
                var numShotgunPelletsPerBullet = 10;
                var width = scene.getEngine().getRenderWidth();
                var height = scene.getEngine().getRenderHeight();
                for (var i = 0; i < numShotgunPelletsPerBullet; i++)
                var accuracyX = Math.round((Math.random() * weaponAccuracy) - (weaponAccuracy / 2));
                var accuracyY = Math.round((Math.random() * weaponAccuracy) - (weaponAccuracy / 2));

                var meshFound = scene.pick((width / 2) + accuracyX, (height / 2) + accuracyY, null, false, camera);
                console.log(meshFound.pickedPoint);



                This will pick a random point in a 200 by 200px square around the middle of the screen 10 times.



                If you want to change the accuracy increase or decrease weaponAccuracy.



                And if you want to change the number of pellets in each bullet change numShotgunPelletsPerBullet.






                share|improve this answer













                I'm guessing you're using scene.pick to get meshFound.pickedPoint (if you are not then ignore this answer) and the function takes in a x and y coordinate of a point on the screen then turns that point into a point in 3d space.



                I'm guessing your doing something like:



                var width = scene.getEngine().getRenderWidth();
                var height = scene.getEngine().getRenderHeight();
                var meshFound = scene.pick((width / 2), (height / 2), null, false, camera);


                And that will get the same point in the middle of the screen every time, but this is what I'm suggesting:



                var weaponAccuracy = 200;
                var numShotgunPelletsPerBullet = 10;
                var width = scene.getEngine().getRenderWidth();
                var height = scene.getEngine().getRenderHeight();
                for (var i = 0; i < numShotgunPelletsPerBullet; i++)
                var accuracyX = Math.round((Math.random() * weaponAccuracy) - (weaponAccuracy / 2));
                var accuracyY = Math.round((Math.random() * weaponAccuracy) - (weaponAccuracy / 2));

                var meshFound = scene.pick((width / 2) + accuracyX, (height / 2) + accuracyY, null, false, camera);
                console.log(meshFound.pickedPoint);



                This will pick a random point in a 200 by 200px square around the middle of the screen 10 times.



                If you want to change the accuracy increase or decrease weaponAccuracy.



                And if you want to change the number of pellets in each bullet change numShotgunPelletsPerBullet.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 4:31









                MikeMike

                1,4091923




                1,4091923



























                    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%2f53272683%2fcreatin-a-shotgun-spread-pattern-babylon-js%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

                    Kleinkühnau

                    Makov (Slowakei)

                    Deutsches Schauspielhaus