nape.symbolic.SymbolicConstraint

type
class
extends
nape.constraint.UserConstraint
requires
nape-symbolic module.
Class provides run-time definition of constraints using a simplified DSL.

These constraints are much easier to write and experiment with than a hand-written UserConstraint as only the constraint itself needs to be defined at a high level using the mathematical DSL.

For a handful of constraints, the performance will be perfectly fine and this class provides a debug() method to print internal information that would be required to more easily translate a given constraint into a hand-written and optimised UserConstraint.

Syntax: The input to this class is a constraint definitino which follows this pseudo-BNF grammar:
# this is a comment
# body parameter declarations
body name [, name]*

# variable decalarations
scalar|vector name [= default-literal]? [-> time-derivative-expression]?

# constraint/variable limits (optional; default 0's)
# The type of min/max expressions must match variable/constraint type
# with limits imposed component-wise for vector values.
# Limits on variables will be checked during constraint verification at runtime.
limit expression|constraint min-expression max-expression

# expressions may be composed of:
# a basic value:
#    literal-scalar # eg: 1.5
#    inf  # infinity
#    eps  # epsilon value (very near 0).

# a vector/matrix/block expression:
#    [ e1 e2 ] # this is a vector, expressions must be scalar typed.
#    [ e1 e2 ; e3 e4 ] # this is a matrix, expressions must be scalar typed.
#    { e1 e2 } # this is a block vector, expressions may be of any type.

# a let expression:
#    let name = e1 in e2

# Composed of operations between expressions:
#    e1 -+/* e2
#    e1 dot e2 # dot product
#    e1 cross e2 # cross product
#    e1 outer e2 # outer product
#    unit expr # unit of expression (sign of scalar, unit vector)
#    | expr | # magnitude of expression (magnitude of scalar, length of vector)
#    [ expr ] # perpendicular to a vector
#    relative name expr # transformation of given vector expression from local
#                       # to relative coordinates given a variable name representing
#                       # an angle.

# A constraint definition:
#    For a 1-dimensional constraint, this expression should be of scalar type.
#    And for 2 and greater dimensional constraints, a block-vector of scalar types.
constraint expr
Each body parameter will automatically define a set of special variables for that body:
body.position   # position of body (vector)
body.velocity   # velocity of body (vector)
body.rotation   # rotation of body (scalar)
body.angularVel # angular velocity of body (scalar)
As an example, we can re-create Nape's DistanceJoint with:
var symbolicDistanceJoint = new SymbolicConstraint("
    body body1, body2
    vector anchor1, anchor2
    scalar jointMin, jointMax

    # declare that jointMin <= jointMax, and jointMin, jointMax >= 0
    limit jointMin 0 jointMax

    # declare constraint as the distance between transformed anchors
    constraint
       let r1 = relative body1.rotation anchor1 in
       let r2 = relative body2.rotation anchor2 in
       | (body2.position + r2) - (body1.position + r1) |

    # limit distance to the joint min/max.
    limit constraint jointMin jointMax
");
And use it like:
symbolicDistanceJoint.space = space;
symbolicDistanceJoint.setBody("body1", somebody);
... etc
For more examples visit the github repo at: https://github.com/deltaluca/nape-symbolic

SymbolicConstraint's make use internally of the UserConstraint API, and so like other UserConstraint's can be made stiff, or soft and elastic with no extra work.

Constructor

function new(constraint:String)
+ Construct a new SymbolicConstraint

Instance Properties hide inherited show inherited

var active : Bool = true

+ Whether this constraint is active or not.

var breakUnderError : Bool = false

+ Whether constraint will break once maxError is reached.

var breakUnderForce : Bool = false

+ Whether constraint will break once maxForce is reached.

readonly var cbTypes : nape.callbacks.CbTypeList = [CbType.ANY_CONSTRAINT]

+ Set of CbTypes for this constraints for callbacks.

var compound : Null<nape.phys.Compound> = null

+ Compound this Constraints belong to.

var damping : Float = 1

+ Damping ratio of elastic properties of constraint.

var debugDraw : Bool = true

+ Set to disable debug drawing/

var frequency : Float = 10

+ Frequency of elastic properties of constraint.

var ignore : Bool = false

+ Whether interactions between related Bodys will be ignored.

readonly var isSleeping : Bool

+ Whether this constraint is sleeping or not.

var maxError : Float = infinity

+ The maximum amount of error this constraint is allowed to use.

var maxForce : Float = infinity

+ The maximum amount of force this constraint is allowed to use.

var removeOnBreak : Bool = true

+ Whether constraint will be removed when it breaks.

var space : Null<nape.space.Space> = null

+ Space this constraint is inside of.

var stiff : Bool = true

+ Whether constraint is stiff, or elastic.

readonly var userData : Dynamic<Dynamic> = {}

+ Dynamic object for user to store additional data.

Instance Methods hide inherited show inherited

__bindVec2():nape.geom.Vec2

+ Create a Vec2 property for user-constraint.

__broken():Void

+ Internal extra steps in breaking constraint
+ Internal copying of user defined constraint.

__draw(debug:nape.util.Debug):Void

+ Internal debug drawing of constraint.

__invalidate():Void

+ Internal method to invalidate constraint on property changes

__registerBody(oldBody:Null<nape.phys.Body>, newBody:Null<nape.phys.Body>):Null<nape.phys.Body>

+ Internal method to register Body's with constraint.
+ inheritDoc

copy():Constraint

+ Produce copy of constraint.

function debug():String

+ Print internal information about symbolic constraint.

function getBody(name:String):Null<nape.phys.Body>

+ Access Body type parameter of constraint.

function getScalar(name:String):Float

+ Access Scalar type parameter of constraint.

function getVector(name:String):nape.geom.Vec2

+ Access Vector type parameter of constraint.
+ inheritDoc

function setBody(name:String, body:Null<nape.phys.Body>):Null<nape.phys.Body>

+ Set Body type parameter of constraint.

function setScalar(name:String, value:Float):Void

+ Set Scalar type parameter of constraint.

function setVector(name:String, value:nape.geom.Vec2):nape.geom.Vec2

+ Set Vector type parameter of constraint.

visitBodies(lambda:nape.phys.Body -> Void):Void

+ inheritDoc