Mixing onPointerObservable with ActionManager not working

Hello,
i am not sure if I did misunderstand how the pointer evalutation works but I tried the following to no avail:
https://www.babylonjs-playground.com/#XBPQ23#4
The first click generates a sprite and then a click on the sprite should call a function.
It seems the latter does not get called.

Thanks!

1 Like

Pinging @PirateJC

Pirates are offshore in Quarantine these days … :slight_smile:

1 Like

Hey @EvilTwinsNiceBrother…first this is the best username ever!

Second, terribly sorry about the delay. I’ve honestly been spending a lot of time digging into this and trying to figure out why what you have written isn’t working.

I’ve got good news and bad news.

Bad News first - I honestly cannot figure out why adding an execute code action, added to the sprite action manager is not firing. There are clear examples in the documentation of where this feature works just fine, so It’s a bit of a mystery to me. I’m sure I’m overlooking something stupid. @Deltakosh can likely help us.

The good news though is I was able to get the desired behavior you were looking for via a different method. I’m not sure how helpful this is to you, but check out this playground:

https://www.babylonjs-playground.com/#XBPQ23#5

I’ve done a couple of things here. First I changed the onPointerObservable to onPointerDown to fire your function whenever the mouse is pressed.

Second I store the pointer’s x and y position at the time of the click and run a pick operation and make sure there was a hit. This eliminates the bug you had where you’d draw a sprite even if you clicked off of the mesh.

This narrows it down to either a mesh or sprite having been picked.

Next I run another pick but this time just the pickSprite to see if there was a sprite selected. side note you shouldn’t need to do this second pickSprite operation, because I believe that the first pick should return anything in the scene that was picked…including both the sprites and the mesh, but I could be wrong about that…it may just be the FIRST thing that was picked.

If it was a sprite that was picked, then I display the alert

If it was not a sprite, then I use the original pick’s x,y,z positions to draw the sprite on the mesh as you had before.

So this works to get the desired behavior, but it’s a different method than you were exploring, and again, I see no reason why the route you were going didn’t work, so I’m going to do some more digging and see what I can learn.

Anyone else have any ideas?

Bonus: I also add this line of code:

sprite.useAlphaForPicking = true;

It isolates selection of each sprite to the red dot itself.

Sorry I don’t have a more firm answer. I hope this is helpful in some way.

2 Likes

Looking at the the code, the way it works right now is that the action manager favors meshes over any other type of picked objects. I’m not sure if that’s the intended behavior, but if there is a mesh under/over the sprite, it will pick that mesh first.

1 Like

thanks a lot.
I will try and see if i can incorporate that way of doing things into my code.
good to know that my problem even creates a problem with the experienced babylonians.
i was pulling quite some hairs.

2 Likes

I dug into this quite a bit more and learned some things from the honorable @Deltakosh.

@bghgary is completely right about this. The sprite’s don’t exist in the depth buffer, so that means the pick operation doesn’t understand z-order. Priority is always given to a mesh when multiple things are picked, so the sprite is never actually “picked” in your original method…so the executeCode action never fires. The pick keeps thinking that a mesh is being selected, not the sprite.

So you then need to do a check to see if a sprite was hit first, and then act accordingly. So the method I used in the playground above is probably the best way to go about doing what you’re hoping to do.

Here’s a slightly simplified version with the action manager code stripped out.

https://www.babylonjs-playground.com/#XBPQ23#6

Hope this helps! Thanks for the fun investigative journey!

3 Likes

thanks a lot. greatly appreciated!

2 Likes

This thread has been super useful, thanks for both the question and answers. One follow up question though, why is this problem not existing with OnPointerOverTrigger which always fires?