User Tools

Site Tools


dragengine:modules:dragonscript:behavior_avoidcollision

ECBehaviorAvoidCollision

Behavior adding support to actors to avoid collisions with ECBehaviorAvoidedByActor.

Behavior attaches to ECBehaviorActorMover to modify walking path while moving.

To use this behavior add it to the element class and make sure to call setShapesSphere() to set up the collision test shapes. The best time to do this is either in BehaviorElement.init() or inside BaseActorAction if you need to change them per action.

See also:

Instance Counts

This behavior can be used only once on an element.

Element Class Properties

Element class properties have the prefix avoidCollision. .

radius

Set avoid radius in meters.

  • Full name: avoidCollision.radius
  • Type: float
  • Default Value: 0.4
  • Restriction: At least 0
  • Example (*.deeclass)
    <float name='avoidCollision.radius'>0.6</float>

Events

This behavior has no events.

Behavior Tree Actions

This behavior adds these behavior tree actions if behavior tree is present.

avoidCollision.set

Set one or more force field parameters.

ParameterValueDescription
waitBehindObstaclestrue, falseWait behind obstacles instead of moving around them
enabledtrue, falseEnable avoid collision

This is an example of using this action:

<action name='avoidCollision.set'>
  <parameter name='enabled'>true</parameter>
</action>

avoidCollision.stop

Stop avoiding collision.

This is an example of using this action:

<action name='avoidCollision.stop'/>

avoidCollision.check

Check one or more force field parameters. Action succeeds if all parameter value matches their respective force field parameter otherwise action fails. This action is typically used as first action in a sequence to run the sequence only if a force field parameter matches (or not).

ParameterValueDescription
waitBehindObstaclestrue, falseWait behind obstacles instead of moving around them
enabledtrue, falseAvoid collision is enabled or not
avoidingtrue, falseIn progress of avoiding a collision
avoiding.time.lessfloatRamining time avoiding collision is less than value seconds
avoiding.time.greaterfloatRamining time avoiding collision is greater than value seconds
wait If present action returns BTResult.running instead of BTResult.failed to wait until the checks are all fulfilled

This is an example of using this action:

<sequence>
  <action name='avoidCollision.check'>
    <parameter name='avoiding'>true</parameter>
  </action>
  <!-- actions here run only if actor is avoiding a collision -->
</sequence>

Behavior Tree Conditions

This behavior adds these behavior tree conditions if behavior tree is present. If behavior has non-empty identifier replace avoidCollision with avoidCollision(id).

avoidCollision.check

Check one or more force field parameters. Conditions returns true if all parameter value match their respective force field parameter. This condition is typically used to run an action or sequence of actions as long as force field conditions are true.

ParameterValueDescription
avoidCollision.waitBehindObstaclestrue, falseWait behind obstacles instead of moving around them
avoidCollision.enabledtrue, falseAvoid collision is enabled or not
avoidCollision.avoidingtrue, falseIn progress of avoiding a collision
avoidCollision.avoiding.time.lessfloatRamining time avoiding collision is less than value seconds
avoidCollision.avoiding.time.greaterfloatRamining time avoiding collision is greater than value seconds

This is an example of using this condition:

<action name='myAction' id='doing something'>
  <parameter name='avoidCollision.avoiding'>true</parameter>
  <condition>avoidCollision.check</condition>
</action>

State Machine Actions

State Machine Conditions

State Machine Events

This behavior sends these state machine events.

avoidCollision.avoid

Avoid collision.

avoidCollision.stop

Stop avoiding collision.

Required Behaviors

Optional Behaviors

Persistency

This behavior does support element class to be persistable (setPersistable).

API Documentation

ECBehaviorAvoidCollision.

Since DragonScript Module Version 1.0

Use Cases

Element Class Example

This example defines an element which can avoid collisions.

class MyElement extends BehaviorElementClass
  public var ECBehaviorComponent component
  public var ECBehaviorCollider collider
  public var ECBehaviorColliderAI colliderAI
  public var ECBehaviorLocomotion locomotion
  public var ECBehaviorNavigator navigator
  public var ECBehaviorProjectToGround projectToGround
  public var ECBehaviorRideOn rideOn
  public var ECBehaviorActorMover actorMover
  public var ECBehaviorAvoidCollision avoidCollision
  func new()
    component = ECBehaviorComponent.new(this, null)
    collider = ECBehaviorCollider.new(this, component)
    colliderAI = ECBehaviorColliderAI.new(this, collider)
    locomotion = ECBehaviorLocomotion.new(this, colliderAI)
    navigator = ECBehaviorNavigator.new(this)
    projectToGround = ECBehaviorProjectToGround.new(this, colliderAI)
    rideOn = ECBehaviorRideOn.new(this, locomotion, projectToGround)
    actorMover = ECBehaviorActorMover.new(this, locomotion, navigator, rideOn)
    actorMover.setConversationActor(conversationActor)
    avoidCollision = ECBehaviorAvoidCollision.new(this, actorMover, rideOn)
  end
end

Behavior Factory

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

<?xml version='1.0' encoding='UTF-8'?>
<elementClass name='MyClass' class='GenericBehaviorElement'>
  <behavior type='ECBehaviorComponent'/>
  <behavior type='ECBehaviorCollider'/>
  <behavior type='ECBehaviorColliderAI'/>
  <behavior type='ECBehaviorLocomotion'/>
  <behavior type='ECBehaviorNavigator'/>
  <behavior type='ECBehaviorProjectToGround'/>
  <behavior type='ECBehaviorRideOn'/>
 
  <behavior type='ECBehaviorAvoidCollision'>
    <!-- optional: set collision filter. default value '3:0 1 2 3' which means
                   category BaseGameApp.CollisionFilterBit.actorAI
                   filter BaseGameApp.CollisionFilterBit.geometry,
                          BaseGameApp.CollisionFilterBit.dynamic,
                          BaseGameApp.CollisionFilterBit.actorAI.
                   format is '', 'category' or 'category:filter' where category and filter
                   are a list of bits to set. -->
    <string name='collisionFilter'>3:0 1 2 3</string>
 
    <!-- optional: use behavior tree with id instead of empty string -->
    <string name='behaviorTree'>second</string>
 
    <!-- optional: use state machine with id instead of empty string -->
    <string name='stateMachine'>second</string>
 
    <!-- set element properties. omit property prefix if used inside behavior tag -->
    <float name='.radius'>0.6</float>
  </behavior>
</elementClass>

Live Examples

You could leave a comment if you were logged in.
dragengine/modules/dragonscript/behavior_avoidcollision.txt · Last modified: 2025/05/04 23:16 by dragonlord