User Tools

Site Tools


dragengine:modules:dragonscript:behavior_touchsensor

This is an old revision of the document!


ECBehaviorTouchSensor

Behavior element behavior adding touch sensor support.

Attaches a TouchSensor resource to the behavior element. By default the collision filter category is set to BaseGameApp.CollisionFilterBit.trigger and the collision filter mask is set to BaseGameApp.CollisionFilterBit.actorAI . This causes actors to trigger this touch sensor unless they have the CollisionFilterBit.trigger bit cleared. Modify the collision filter to allow other elements to trigger this touch sensor.

Upon being touched the touch sensor notifies the listeners.

The TouchSensor resource can be queried for the colliders currently touching it. Furthermore colliders entering and leaving trigger a notification allowing elements to track elements of interest.

If the ECBehaviorCollider behavior is present in the behavior element the TouchSensor is attached to the collider.

The behavior retrieves the owner of colliders inside the touch sensor. If these owners are elements they are stored in a list of elements touched by the owner element. This list is updated when elements enter or leave the touch sensor.

The owner behavior element as well as other ECBehavior subclasses can add a listener to be notified if elements enter and exit the touch sensor.

Instance Counts

This element behavior can be present multiple times in a BehaviorElement. In this case use a unique identifier to distinguish the individual touch sensors.

Element Class Properties

Element class properties have the prefix touchSensor. or touchSensor({id}). if id is not empty.

bone

Bone to attach resource to. If empty string attach to collider.

  • Full name: touchSensor.bone or touchSensor({id}).bone
  • Type: string
  • Default Value: empty string
  • Example (*.deeclass)
    <string name='touchSensor.bone'>attach</string>

collisionFilter

Set collision filter. Filter bits are defined using a list of names mapped to collision filter bit indices using BaseGameApp.getCollisionFilterMap().

  • Full name: collider.collisionFilter
  • Type: string. Format can be category:filter or category where category and filter can be bit1 bit2 … bitN with bit names looked up using the collision filter map. If only category is used filter is the same.
  • Default Value: trigger:actorAI
  • Example (*.deeclass)
    <string name='collider.collisionFilter'>trigger:actorAI particle</string>

orientation

Orientation to attach resource to collider in degrees.

  • Full name: touchSensor.orientation or touchSensor({id}).orientation
  • Type: 3-component float vector
  • Default Value: (0,0,0)
  • Example (*.deeclass)
    <vector name='touchSensor.orientation' x='30' y='0' z='0'/>

position

Position to attach resource to collider.

  • Full name: touchSensor.position or touchSensor({id}).position
  • Type: 3-component float vector
  • Default Value: (0,0,0)
  • Example (*.deeclass)
    <vector name='touchSensor.position' x='0' y='0' z='0.1'/>

shape

Touch sensor shape.

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

shapeFromSize

Set shape to box matching the element size.

  • Full name: touchSensor.shapeFromSize or touchSensor({id}).shapeFromSize
  • Type: boolean
  • Default Value: false
  • Example (*.deeclass)
    <boolean name='touchSensor.shapeFromSize'>true</boolean>

touching

Enable touch sensor.

  • Full name: touchSensor.touching or touchSensor({id}).touching
  • Type: boolean
  • Default Value: true
  • Example (*.deeclass)
    <boolean name='touchSensor.touching'>false</boolean>

Events

elementEntered

Element entered touch sensor.

elementLeft

Element left touch sensor.

elementCanTouch

Element can touch this touch sensor if all listeners return true.

Behavior Tree Actions

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

touchSensor.set

Set one or more touch sensor parameters.

ParameterValueDescription
collisionFilter.filter.setstringSet named collision filter filter mask bit.
collisionFilter.filter.clearstringClear named collision filter filter mask bit.
enabledtrue, falseEnable touch sensor

This is an example of using this action:

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

touchSensor.update

Update touch sensor.

ParameterValueDescription
collisionFilter.filter.setstringSet named collision filter filter mask bit.
collisionFilter.filter.clearstringClear named collision filter filter mask bit.
enabledtrue, falseEnable touch sensor
eventElement.runEventstringRun named event on all ECBehaviorStateMachine instance of event parameter BehaviorElement with the ECBehaviorTouchSensor element as event parameter. Use only inside touchSensor.entered or touchSensor.left event.

This is an example of using this action:

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

touchSensor.check

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

ParameterValueDescription
countintegerCount of touching elements equals integer value
count.notintegerCount of touching elements does not equal integer value
count.lessintegerCount of touching elements is less than integer value
count.greaterintegerCount of touching elements is greater than integer value
eventElement.flags.setstringEvent BehaviorElement has ECBehaviorBTSMFlags with named flag set. Use only inside touchSensor.entered or touchSensor.left event.
eventElement.flags.clearstringEvent BehaviorElement has ECBehaviorBTSMFlags with named flag cleared. Use only inside touchSensor.entered or touchSensor.left event.
playertrue, falsePlayer is touching touch sensor
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='touchSensor.check'>
    <parameter name='player'>true</parameter>
  </action>
  <!-- actions here run only if player is touching touch sensor -->
</sequence>

Behavior Tree Conditions

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

touchSensor.check

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

ParameterValueDescription
touchSensor.countintegerCount of touching elements equals integer value
touchSensor.count.notintegerCount of touching elements does not equal integer value
touchSensor.count.lessintegerCount of touching elements is less than integer value
touchSensor.count.greaterintegerCount of touching elements is greater than integer value
touchSensor.playertrue, falsePlayer is touching touch sensor
touchSensor.eventElement.flags.setstringEvent BehaviorElement has ECBehaviorBTSMFlags with named flag set. Use only inside touchSensor.entered or touchSensor.left event.
touchSensor.eventElement.flags.clearstringEvent BehaviorElement has ECBehaviorBTSMFlags with named flag cleared. Use only inside touchSensor.entered or touchSensor.left event.

This is an example of using this condition:

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

State Machine Actions

State Machine Conditions

State Machine Events

This behavior sends these state machine events. If behavior has non-empty identifier replace touchSensor with touchSensor(id).

touchSensor.entered

Element entered touch sensor. Element is send as event parameter.

touchSensor.left

Element left touch sensor. Element is send as event parameter.

Required Behaviors

This behavior requires no other behaviors.

Optional Behaviors

Persistency

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

API Documentation

ECBehaviorTouchSensor.

Since DragonScript Module Version 1.0

Use Cases

  • Detect touching elements.

Element Class Example

This example defines an element which can detect touching elements.

class MyElement extends BehaviorElementClass
  public var ECBehaviorComponent component
  public var ECBehaviorCollider collider
  public var ECBehaviorTouchSensor touchSensor
  func new()
    component = ECBehaviorComponent.new(this, null)
    collider = ECBehaviorCollider.new(this, component)
    touchSensor = ECBehaviorTouchSensor.new(this, collider)
  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='ECBehaviorTouchSensor'>
    <!-- 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 -->
    <boolean name='.shapeFromSize'>true</boolean>
  </behavior>
 
  <!-- for adding multiple behaviors use unique identifiers -->
  <behavior type='ECBehaviorTouchSensor' id='second'/>
</elementClass>

Live Examples

You could leave a comment if you were logged in.
dragengine/modules/dragonscript/behavior_touchsensor.1791588487.txt.gz · Last modified: by dragonlord