Custom wire physics using GreasedLines

Hello all ! :slight_smile:

I’m currently in the process of porting some custom projects and classes from Python3 (Numpy + OpenCV) toward BabylonJS (in Javascript TypeScript :grin: ). During my work I came across this funny test, so I share :

Screencast from 23-10-2024 18:06:22

  • Render β†’ GreasedLine (@roland :wink: ) + Spheres
  • Physics β†’ Custom dummy computation (physics itself is about 10 lines of code in total)

If you want to give a test :

// This is the custom physics engine
const game = new Game(scene);
// Init a list of points. Pin list is indices of points to be pinned (still movable with mouse)
let pts = [];
for(let i=0; i<25; i++){
    pts.push(new BABYLON.Vector3(-5+i, 8, 0));
}
const pin = [0, 12];
// Create line with pinned points
game.createLine(pts, pin);
// Create a bar constraint between two points
game.bars.push(new Bar(game.joints[6], game.joints[14]));

The constraints bar have a length automatically computed from distance between point at the time you create the constraint. To use another length, just give the length param :

game.bars.push(new Bar(game.joints[6], game.joints[14], 2.0));

Hope you’ll have fun :stuck_out_tongue:
++
Tricotou

8 Likes

Oh by the way, it works in 3D :stuck_out_tongue:

Screencast from 23-10-2024 18:51:47
I’m pretty happy since the original engine was written in 2D, and porting to BABYLON.Vector3 had it working in 3D at the first try ! :smiley:

Modifications compared to previous PG :

  • Picking plane set to billboard mode (in order to drag in screen space… I guess there is a better way !) + placed at mesh picked on pick down
  • Camera detached from canvas on picking in order to rotate

Can’t wait to give a try to some actual games, with this trick :grin: (Wires, Bridges, whatever)

5 Likes

that is nice

2 Likes

New test with a net :grin:

Screencast from 24-10-2024 12:47:53

Compute for +400 joints is about 0.1 ms :slight_smile:

9 Likes

This is so NEAT! Please tweet about it so I can retweet :smiley:

3 Likes

Thanks :heart_eyes: ! Here you go :

I added instantiation of the spheres, I forgot to do it in the first version, resulting in 400 spheres + their individuals materials :joy:

3 Likes

So smooth! :smiling_face_with_three_hearts:

1 Like