Right, so what does the script do exactly?
cons = P.AddEntitySingleHinge( e, "TopCentreW" )
This function is the easiest to use as it hides everything, pretty much, under the covers and only exposes what you need to get the job done (which is what all the best software should do!).
In the library there is a list of named hinges, you specify the hinge by name and the library function does the hard work of creating the appropriate gobbledy-gook to pass into GG.
The list at present contains the following choices:
'TopLeft', 'TopRight', 'TopFront', 'TopBack', 'TopCentreW', 'TopCentreL',
'BottomLeft', 'BottomRight', 'BottomFront', 'BottomBack', 'BottomCentreW', 'BottomCentreL',
'LeftBack', 'LeftFront', 'LeftCentre', 'FrontCentre', 'BackCentre',
'RightBack', 'RightFront', 'RightCentre',
'CentreVertical', 'CentreWidth', 'CentreLength', 'CentreFront', 'CentreBack'
Phew! The first part of the name defines where on the entity the hinge is attached and the rest of the name defines the axis, so for example 'TopLeft' means that the hinge lies along the top left hand edge of the model.
The best way to see what they all do is to simply try them out so to make that even easier lets have a simple script to enable us to do that:
local P = require "scriptbank\\physlib"
function door_hinge_init_name( e, name )
P.AddEntitySingleHinge( e, name )
end
function door_hinge_main( e )
PromptLocal( e, GetEntityName( e ) )
end
Simply attach that script to your entity, clone it a bunch of times then name the entities with the name of a hinge from the list above to see what each one does.
The next function I'll briefly describe is from the first script again:
P.RemoveEntityConstraintsIfDead( e )
This one does exactly what it's name suggests, it checks to see if the entities health is zero and if so removes any constraints that have been set for that entity, as soon as the constraint have been removed then normal Physics take over and the entity will fall due to gravity etc.
(Note that the AddEntitySingleHinge function returns a value (I've assigned it to 'cons' in the first script), this is an identifier of the constraint in the Bullet Engine that can then be used by other functions later to alter the constraint in some way. For example if you set a hinge constraint with angular limits then later you can alter those limits by passing the identifier to specify which constraint you are modifying, this is me future proofing btw I haven't added support for this fully yet but when I do you will find the functions to do it in the library.)
The next function to cover is easier to explain because it basically works in exactly the same way as the hinge but the result is different:
P.AddEntitySingleJoint( e, jointName, spacing )
The difference between a hinge and a joint is simply that the hinge constrains motion around a specified axis, i.e. like a door or wheel motion, whereas a joint constrains to a single point with rotation in all axis, i.e. like a hanging light.
This function uses the same name list as the hinge to define the location of the constraint.
Note the optional 'spacing' parameter, this can also be specified for hinges and I'll explain this in more detail later.
In fact the optional parameter list for single hinges is:
swingAng : defines the angle through which the hinge can operate
offset : defines the centre of the swing angle as a percentage
spacing : adds an offset to the hinge location, this is a list of the form { w, h, l } where:
w = width (or x offset)
h = height (or y offset)
l = length (or z offset)
Now you may be wondering why I use w, h & l if I really mean x, y & z, well it's because x, y, z is used to define a position in world space width, height and length are dimensions relative to the model origin.
I'll explain these optional parameters when we use them in anger.
Next post I'll introduce the 'Double' versions of these functions and we'll start to put together a fun example.
Been there, done that, got all the T-Shirts!