User Tools

Site Tools


dragengine:modules:dragonscript:behavior_collider

ECBehaviorCollider

Behavior element behavior adding collider support.

Colliders provide physical presence to elements allowing them to collide with other colliders, attaching them to other colliders and moving the element through the game world.

This element behavior is often required by other element behaviors to be present to function correctly.

Since this collider repositions the element upon changing position and orientation this element behavior can be present only once in a BehaviorElement. Trying to add more than one instance results in an exception thrown.

If you need additional colliders for example to sense objects using collision detection then you need to use other ECBehavior providing the desired behavior. These allow to be added multiple times.

If the ECBehaviorComponent behavior is present in the behavior element before this behavior is added a ColliderComponent is created. The component is used for collision detection and updated automatically by the collider. This is required if you intend to use per-bone collisions matching animation state or physical simulations like rag-dolls.

If the ECBehaviorComponent behavior is added after this behavior then a ColliderVolume is created. The rig assigned to the component is only used for animation purpose. The ECBehaviorComponent will then statically attach to the collider.

Hence these two use cases are possible depending on the order the behaviors are added: Shape Collision, Component Collision. See Examples

See also:

Instance Counts

This behavior can only be addec once on an element.

Element Class Properties

Element class properties have the prefix collider..

physicsType

Set physics type.

  • Full name: collider.physicsType
  • Type: enumeration
  • Allowed Values:

    ValueDescription
    noneNo response. Collider is not moving and will not move by collisions.
    dynamicDynamic collision response. Collider reacts to impacts using physical simulation.
    kinematicKinematic collision response. Game scripts define the collision response.
  • Default Value: none
  • Example (*.deeclass)
    <string name='collider.physicsType'>dynamic</string>

localGravity

Set local gravity or null to use the world gravity.

  • Full name: collider.localGravity
  • Type: 3-component float vector
  • Default Value: null
  • Example (*.deeclass)
    <vector name='collider.localGravity' x='0' y='-0.5' z='0'/>

weight

Set weight in kg.

  • Full name: collider.weight
  • Type: float
  • Default Value: 1
  • Restriction: At least 0
  • Example (*.deeclass)
    <float name='collider.weight'>1.5</float>

enabled

Set enable collider.

  • Full name: collider.enabled
  • Type: boolean
  • Default Value: true
  • Example (*.deeclass)
    <boolean name='collider.enabled'>false</boolean>

shape

Set shape if no component is used.

  • Full name: collider.shape
  • Type: string (shape format). See “Shape List Encoding” in CodecPropertyString.
  • Default Value: empty string
  • Example (*.deeclass)
    <string name='collider.shape'>box:position,0,0.5,0:extends,2,1,0.5</string>

Events

This behavior has no events.

Required Behaviors

This behavior requires no other behaviors.

Optional Behaviors

Persistency

This behavior does support element class to be persistable (setPersistable). Saves these states:

  • Element position
  • Element orientation
  • Element scaling
  • Collider linear velocity
  • Collider angular velocity
  • If collider response type is dynamic and colider is ColliderRig or ColliderComponent writes these states of all dynamic bones:
    • Bone position
    • Bone orientation
    • Bone linear velocity
    • Bone angular velocity

API Documentation

ECBehaviorCollider.

Since DragonScript Module Version 1.0

Use Cases

  • Add physical presence to element in game world.

Element Class Example

Use Case 1: Shape Collision. Component is only visual. For collision only static collision shape is used. The component is attached statically to the collider and does not collide. If collider is dynamic physics simulation will be done using collision shape only.

class MyElementClass extends BehaviorElementClass
  public var ECBehaviorCollider collider
  public var ECBehaviorComponent component
  func new()
    collider = ECBehaviorCollider.new(this, null)
    // assign collision shape or collision rig to the collider
    component = ECBehaviorComponent.new(this, collider)
  end
end

Use Case 2: Component Collision. Component is used for collision detection. If collider is dynamic component bones will be updated by the collider automatically.

class MyElementClass extends BehaviorElementClass
  public var ECBehaviorCollider collider
  public var ECBehaviorComponent component
  func new()
    component = ECBehaviorComponent.new(this, null)
    collider = ECBehaviorCollider.new(this, component)
  end
end

Behavior Factory

Using element class supporting adding behaviors the behavior can be added like this:

Use Case 1: Shape Collision

<?xml version='1.0' encoding='UTF-8'?>
<elementClass name='MyClass' class='GenericBehaviorElement'>
  <behavior type='ECBehaviorCollider'>
    <!-- no behavior has been added before so no component behavior will be used -->
 
    <!-- set element properties. omit property prefix if used inside behavior tag -->
    <string name='.physicsType'>dynamic</string>
  </behavior>
 
  <!-- adding the component will detect the previously added collider and use it -->
  <behavior type='ECBehaviorComponent'/>
</elementClass>

Use Case 2: Component Collision.

<?xml version='1.0' encoding='UTF-8'?>
<elementClass name='MyClass' class='GenericBehaviorElement'>
  <!-- no collider is present yet so component will not use any collider -->
  <behavior type='ECBehaviorComponent'/>
 
  <behavior type='ECBehaviorCollider'>
    <!-- optional: by default the previously added component is detected and used.
                   to use a different component add one with a different id and
                   use the id here -->
    <string name='component'>second</string>
 
    <!-- set element properties. omit property prefix if used inside behavior tag -->
    <string name='.physicsType'>dynamic</string>
  </behavior>
</elementClass>

Live Examples

You could leave a comment if you were logged in.
dragengine/modules/dragonscript/behavior_collider.txt · Last modified: 2025/03/12 19:35 by dragonlord