Move a bullet quickly without it skipping through meshes

Hey crew!

Quick question here - is there a way to move a bullet inside onBeforeRenderObservable very quickly through the scene without it skipping meshes it should intersect with?

Right now i move the bullet by 10 units, but obviously if something is smaller than 10 units, there is a high chance the bullet will just jump behind it and the intersection wont be detected.

I was thinking that each bullet could have a ray and check 10 points in front of it, if there is a mesh, then adjust properly, but I am afraid that is going to be performance heavy.

Any other ideas?

Thanks a lot!

Use a multi-pick ray:

https://doc.babylonjs.com/babylon101/raycasts#multi-pick

Yes, that is one solution, although I would not use multi pick, since I only care about the first mesh.

I am wondering if there are other solutions that do not require ray casting.

I’d just use a ray. Don’t worry about performance until it is an issue.

2 Likes

You might want to create a predicate function that will only choose meshes that can be shot.

https://doc.babylonjs.com/babylon101/raycasts#predicate-function

Yeah I already have all that in place, hence why I don’t need to use multi pick etc.

Thanks!

Hi guys.

I did some experiments… using Vector3’s static Distance() method, which is a ray-caster, I believe. Notice 4 AbstractMesh overloads at top.

https://www.babylonjs-playground.com/#19OS76#9

Click screen to fire. Goofy, huh? Likely somewhat limited, but, what the heck… gets us thinkin’, maybe. We should be able to shove nearly anything into the boxes array, and if the bullet comes-within 1 unit of it, it will execute target/box.impactAction(). Then impactAction executes target/box.resetTimer(). Then resetTimer executes target/box.resetAction(). What a mess, but sort-of fun. :slight_smile:

I have no idea if I am on-topic, though. I was scared during the whole time I was coding it. heh. Using that Distance() thing… is sort-of like “Working without an ActionManager”. :slight_smile:

Impact-particles, you say? oookay. https://www.babylonjs-playground.com/#19OS76#10

1 Like

I always love how you think outside of the box wing :slight_smile:

But… (there’s the but)… try setting the bullet speed to 2 in your scenario. None of the boxes will light up, because the bullet “jumps over” them.

Anyway, I think I fixed it yesterday with the ray cast for each bullet, it seems like the cleanest solution with the least code.

1 Like

:slight_smile: nod. Yeah, one would think… that if BJS/JS allows “magnum load” bullets, they would provide magnum-load IF/THEN polling, eh? But nooo. :slight_smile:

I DID find a 24-pack of magnum-load If/Then’s for-sale on Ebay, but they were for a SparcStation and coded in ADA. heh.

I wonder what would happen… if everything was upscaled 10x. 10 times bigger targets, 10 times more gap between them, etc.

Nah, that probably wouldn’t work. Really, we just need 10 times faster JS, huh? Darn. hmm.

1 Like