I’m trying to create a simple peg game where you position a ball at the top of the “board” and then when the user is done dragging the ball to where they want they either press Enter to drop the ball or the ball will drop once the user releases the mouse click. I’ve gotten the physics engine and gravity to work on the ball if I set it at loading time, but when I try to set it after the ball is positioned the ball disappears. I’m fairly new to Babylon and have just been following the documentation. Any help would be greatly appreciated. Here’s a link to a playground example: Babylon.js Playground
Hiya N, welcome to the forum.
I did some goofing around… https://www.babylonjs-playground.com/#A4894I#1
Mostly testing inside a
dragBehavior.onDragEndObservable at line 77. I tried all sorts of things… like waiting for onDragEnd before adding impostor to ball… etc. I mostly failed. It seems drag behavior is causing some kind of hell. I just don’t understand what would make ball disappear… with sudden gravity set.
A little note: All whatever.checkCollisions = true things… aren’t needed for physics engine scenes like yours. The .checkCollisions property is for a different kind of collision-testing… designed for freeCamera-to-mesh collisions in first-person shooter games. Common misconception, no problems, doesn’t affect your scene.
Now for something different:
That’s another playground… basic CannonJS ball-drop… using ball.physicsImpostor.setMass() (via gui 2d buttons). it seems to work ok (with no drag behavior activated). So, we know a decent CannonJS physics engine ball-drop IS possible. heh.
To sum, I have made no progress at all, so far. Only testing. Checking setting gravity… vs. setting mass. I’m thinking about that dragBehavior thing… wondering if dragBehavior is contaminating the ball-mesh somehow, before or after impostor add. Hmm. Puzzling.
Thanks for the feedback! I appreciate the attempts at fixing the issue. I’m going to play around with it some more as well and If I make any progress I’ll make sure to post it.
Cool… my pleasure.
I made a whole new “board” (wanted it larger in case physics was acting-up due to original board’s small size). Pegs and side-boards are all parented to plane (position, rotate, or scale plane, and all other parts go along for ride).
SourcePlane is not used at all. Took specular shine off-of the hemi-light and set light groundColor gray to light bottom of board/plane. Plane is now a thin box, actually.
Just a bunch of goofy tweaks. Line 3 - set y gravity immediately, but line 82… set ball with 0-mass.
In line 85 area… inside the onDragEnd event handler… line 90… set mass on ball to 1 (on drag end).
But NO! FAIL… same symptom exactly… all the way. hehe. Oh well, it was a nice re-build… looked good on paper and sounded good in theory… but no. Drag the ball… release the ball… goodbye ball. hmm.
I AM seeing SOME anomalies, though. Release mouse button while drag is still moving - sometimes makes ball fly a bit… usually thru the side-boards for Cannon.
For auto-drop version, Ammo/Oimo/Cannon… I’m seeing no collisions with pegs. hmm.
Ok, I forgot about the problem of putting impostors on things that have parents. Generally speaking, we need to set impostors first, and THEN set parents.
See line 22. In this version, createPlane(), createPegs(), and createBall() do NO parenting or impostor-installing. We do ALL impostors and parenting… in a new func… setParentsAndPhysics()… at lines 154-185.
As you can see, that auto-dropping version (above URL)… is working much better.
Back to testing ball drag…
Here, we start with mass = 0 on the ball, and normal Earth-like gravity. Ball drags fairly well… and onDragEnd (on drop)… (lines 83-96) is working better. Line 86… we just add some mass to the ball when the drag ends… seems to ALMOST work nice. heh.
What you have assembled here… is sometimes called a “compound physics shape”… made-up of many shapes/impostors (parented). BabylonJS has some special code in its physics… that watches-for parenting that is set/activated on physics-active shapes, and performs some special processing of the impostor… when that happens. Primarily, an impostor “reset”.
I should have thought more… before I mixed parenting and physics. My mistake on that part. Now maybe fixed.
I’m not sure WHY the ball-drag is working a little better… but no complaints from me.
Another version… no setMass() used here. Instead, we don’t even add an impostor to ball… until drag-end (line 107). Drag works good… first time.
Why I say “first time”?
In lines 24-43, I have MASSIVE “ball recovery” efforts underway. Line 26 essentially tests “Did ball fall-off board?” If so… what do we do… to “reset” all things and have good dragBehavior again?
Much testing happening there. For some reason, dragging NOT working well… after reset. Needs more work/tests. Fun, though. I hope I am not doing too much work on project, Nick. Sometimes my curiosity takes control of me.
Update: (sigh) (addictive) https://www.babylonjs-playground.com/#A4894I#9
Line 102… onDragEnd… we add the impostor to ball… same as last time.
IF ball falls-off board… line 27 area… I just use 3 code-lines to do the reset… lines 35-37.
- Line 35 Quaternion set to Indentity()… is needed to “re-orient” the dragBehavior drag-axis.
- Line 36 Force ball back to starting position.
- Line 37 Roto-tiller the old impostor… into the soil of our vegetable garden.
Seems to work pretty gooooooood. Yay! Maybe lines 36/37 should swap order.
Hi @Wingnut ,
I took a break from the project for a few days but I’m back on the grind now. I’m super grateful for the amount of work you put into this. Glad to see you got somewhere! I was playing around with the last playground you posted and that’s basically what I was trying to accomplish. I couldn’t however make the ball drop any quicker than the default. Do you know what property does that?
I found the culprit! You had the plane rotated back so when the ball dropped it was actually responding correctly to gravity. Tilt that bad boy back up straight and she’s dropping like it’s hot! Thanks again for your help @Wingnut
Hi again, Nick! Glad to hear you found the solution. Yeah, I had the board at about 45 degrees, I guess… equal amounts of ball mass on peg-tops and plane. I was “thinko” about “plinko”.
I thinko Plinko uses disks instead of balls, though. And it looks like some Plinko boards lean-back, and some don’t. Sorry about that. I should have asked you what the wanted plane-tilt was supposed to be. My fault.
I think you had your original angles set to “10.2”… and in 3d land… that is an odd value to use for rotation in radians (not bad for degrees, though). Generally, 3d land uses derivatives of PI:
- Math.PI/4 = ~.707 radians = 45 degrees = 1/8th orbit.
- Math.PI/2 = ~.1.57 radians = 90 degrees = 1/4 orbit.
- Math.PI = ~3.14 radians = 180 degrees = 1/2 orbit.
- Math.PI * 2 = ~6.28 radians = 360 degrees = 1 full orbit.
We have some conversion helpers:
If you get really rich with your new game, I could use a Rokon motorcycle. I’ve always wanted one. Get two of ‘em, we’ll go ridin’ the woodland trails… see if we can smuck a deer.
Be well, party on.