Exciting things ahead for G3N (physics)

As if it wasn’t enough G3N (a great little 3d engine for the Go language), has fantastic things in store for it, however at this stage do be warned the sub title for this post should be “Danger Will Robinson” or maybe “Here be Dragons” as the new experimental features are just that, and the code is very much in flux and extremely likely to change – in deed there are a number of empty “place holder” functions and the code is littered with the comment I often make

// TODO.....!

The experimental features in questions will hopefully be adding “physics” or more accurately a dynamics system to the 3D engine, and while it is still in a very early stage it does show some promise, for example you can induce backspin on a sphere by dropping another sphere on it, and the backspin really does “feel” right, as it causes the sphere to react to the surface its running over.

Its subtleties like the “feel” of a dynamics system that with some dynamics systems need some tweaking to achieve, it may well be well selected default values, but even defaults can be an indicator of the value of a system…

The dynamics system follows a very familiar paradigm and its no surprise that even in this very early stage it follows the same common sense methodology as the rest of the engine.

Unlike some other libraries gravity isn’t some special case in the simulation and there is no particular assumptions are made, if you do need “traditional” gravity its very straight forward

sim := physics.NewSimulation(scene)
grav := physics.NewConstantForceField(math32.NewVector3(0,-9.8,0))
sim.AddForceField(grav)

As the simulation is tightly integrated with the engine, you must pass you root node for your graphics scene to the simulation – you get so much for “free” when a dynamics system breaths life into your graphics…

The ConstantForceField structure is analogous to how a lot of libraries handle their special case of gravity, no matter where you are in the simulation, the force acts on the body in the same manner.  There are a few different force field types for example there’s a black hole type attractor where the forces acting on the bodies point to the same point in the simulation and are effected by distance.

Oh and by the way, that’s all you need to set up your simulation… simple…

geom := geometry.NewSphere(.5, 16, 16, 0, math.Pi*2, 0, math.Pi)
mat := material.NewStandard(math32.NewColor("White"))
sphere := graphic.NewMesh(geom, mat)
sphere.SetPosition(x, y, z)
s.Add(sphere)

sphBody := object.NewBody(sphere)
pSphere := shape.NewSphere(0.5)
sphBody.SetShape(pSphere)
sm.AddBody(sphBody,"sphere1")

The first part of the above code, is fairly standard as far as making a visual shape with G3N is concerned.  When creating a new dynamic body you give it the visual geometry that will represent it visually, it is however quite common to use a primitive shape or at least very much less detailed mesh when detecting collisions, obviously for reasons of speed.  At the moment a body can only have one collision shape attached to it, but as I understand it at some point in the future you’ll be able to hang multiple collision shapes onto a body, very handy as its amazing what you can get away with by just using a box and a few spheres for example…

So just a quick look at what’s hopefully on the horizon for G3N, no where ready for use, and it probably won’t happen very quickly, this kind of stuff is just that little more complex than graphics and can lead to some fiendish bugs, but if you think you have what it takes, I really do encourage you to roll up your sleeves and dive in.  The more PR’s hit the repo the sooner we’ll all have a dynamic 3d engine!

Leave a Reply

Your email address will not be published. Required fields are marked *