Creating my first pinball game in babylonjs

Hi, I’m trying to create a simple pinball game.
But it seems to not be working…sometimes when i hit the ball it launches it away.
I tried implementing a hinge, but couldnt figure out how to set up it correctly…
Can you help me fix that problem? Thanks!

Uh yeah, and collisions, they dont work eigther… tried with different obsers.
I need to find a way to light up the central rod on collide and the bottom box to despawn.

https://playground.babylonjs.com/#0PGS6R

First things first, the scene was throwing an error because you weren’t getting the correct name of the ball.

I recommend getting used to the Inspector: The Inspector | Babylon.js Documentation (babylonjs.com) and also always keeping your console window open while you develop.

Then, you can start adding the colliders. I added the one on the central rod as means of demonstration, works perfectly: Pinball test | Babylon.js Playground (babylonjs.com)

You’ll probably want to make some loops to add the colliders, since most objects are named similarly. Try to work on one piece of a time.

2 Likes

Yeah, I did some changes, as my post was blocked so I just moved on with another approach.
I can add colliders manually, yes. The problem is when using blender exporter. When imported the physics of colliders will work, but it will not be show associated to mesh.physicsImpostor …it will be undefined. So I wasn’t unable to define any collider on it. So the solution was to remove the defined physics from blender and export without…then redefine in javascript. Would be cool if it associated the data correctly.
If you are curiuos the game will be hosted on (still in development): Game

I have problems with proper collision detection. Do you know how to enable continuos collision detection with ammo.js? For now I can make it kinda work by scaling down the linear velocity… :sweat_smile:

What exactly are the problems?

Ball moves to fast…sometimes it goes outside the board, sometimes it gets stuck inside meshes.
Updated sandbox: https://playground.babylonjs.com/#0PGS6R#5
Remove the function on line 150 to see how it goes.
Sorry for messed code, its still a prototype.
Use: Z + X to flippers

Should i make another post about this problem?

image

So, physics speed and “preciseness” (for the lack of a better word) are definitely related concepts. To see this, imagine you’re running a simulation of a ball rolling down a hill, and the speed of that ball is 10 units per frame. And let’s say you have a small obstacle, like, 2 units in size, as example:
ex1

If you run the simulation once per frame, then on frame f=0, the ball is there at the top of the hill, but what happens at frame f=1? Well, it’s going to have moved by 10 units, so its new position is here:
ex2

At that position, there is no collision with the obstacle, so even if to our eyes it’s obvious that the ball should have hit the obstacle, there’s no way to for the physics system to have known it.

There are two solutions to this problem: one is, as you’ve already tried it, reducing the speed. However, that might not be desirable in some cases, so another solution is increasing the amount of times you run the physics calculations for a frame. To see that this works, you can imagine the same situation if we ran the calculations twice each frame. So, on the first calculation, it’s as like we were on frame f=1/2 for purposes of calculation, and the ball’s position would be this:

ex3

In this case, the physics system can detect a colision ocurred, and it will behave as we expect!

To increase the amount of times the calculation is ran, you can use the setTimeStep function: Using A Physics Engine | Babylon.js Documentation (babylonjs.com). By reducing the time step, i.e, the interval between computations, you increase the amount of times this is computated.

3 Likes

I reduced the time step on line 28 and it seems to be working nicely! Pinball by contat.eu | Babylon.js Playground (babylonjs.com)

Pinballs are such fun, I grew up on Space Cadet pinball and Pokémon pinball :grinning_face_with_smiling_eyes:

2 Likes

https://playground.babylonjs.com/#0PGS6R#7

Lines 111 and 112 enable CCD.
You may encounter an issue with sleeping as well, as can happen when the ball gets stuck between two obstacles.

I made a multiplayer pinball prototype some time ago, using BJS as well as a simple 2D CCD collision engine, as AmmoJS might be a bit too complex for a simple game like this.
But if it works, changing it might just be a waste of time

3 Likes

Thanks to both of you, yeah I will try to fine tune those parameters!
I’m trying to run it in VR on an oculus quest (v1) and its a bit laggy for now.
Would be cool to make it work in AR too :crossed_fingers:
Pokemon was very cool, I played it a lot too!
@Raggar multiplayer pinball sounds very coool. Is there any demo left? I would like to experience it :smiley:

1 Like

Unfortunately. I might have some bits and pieces, but the full project is gone. And it evolved into more of a hockey game with pinball features. I used NengiJS for the networking, and used this as a base for the CCD: GitHub - mreinstein/collision-2d: 2d collision routines

2 Likes