Jump to content

Help on making a shotgun spread pattern ?


Recommended Posts

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", [
            ], this.Player.game.scene),
            BABYLON.Mesh.CreateLines("lines", [
            ], 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].isPickable = false;
                case 1:
                    sgPellets[k].rotation.y += 0.1

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

It is surely because you cant extand indifinitly on the axis. But then, what's the workaround ?

I think I'm missing something on the math side

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

Link to comment
Share on other sites

Hello HyHy, and welcome to the BJS forum.  What an interesting project you have!  Cool!

I don't have any answers to your issues, but I want to show you a neat system that our friend @jerome built.  (at least I THINK he built it)  ;)


It's called the Solid Particle System, or SPS.  It's useful-for-you features are:

  - SPS is a very performance-optimized system
  - Particles/Pellets have basic collision-detect
  - Pellets have auto-calculated gravitational force, or disable gravity
  - Pellets have adjustable frustum/spread
  - Pellets have adjustable muzzle velocity
  - SPS systems allow a manual emit-count... a "pulse" of particles, and then go-idle, as wanted  (a shot)
  - SPS pellets have easily-adjustable pellets-per-shot (pellet cluster-density)
  - SPS pellets can have multiple and odd shapes, in case you want to shoot rock-salt instead of BB's
  - Pellets can be deleted (recycled) upon collision
  - Pellets can fall to the ground or just disappear, at a certain distance, after they have missed the target/collision

In my opinion, the SPS system is PERFECT for your "upland game" game/sim.  I think you should give it some thought/consideration.

Stay tuned... others will comment soon, I think.  Meantime, SPS experimenters - let's create a basic SPS shotgun for/with HyHy... to see how well he/she likes it.

(okay, okay, I would like to see/play-with an SPS shotgun, too.  Sounds like fun!!!  Let's see, we have an "explode-the-mesh" thing laying around here, too, yes?)

Link to comment
Share on other sites

@HyHy Im not sure I understand what you want (A playground with the current behavior would be helpful) but for a shotgun spread I would try this https://playground.babylonjs.com/#TG814J here the spread is a set of rays pointing in the z direction, if wanting to show the spread in a certain orientation (eg. from camera) you can rotate the direction by the camera's rotationQuaternion. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...