Touches - theRAPTLab/gsgo GitHub Wiki

See !93 !89 !106 !116


[[TOC]]


TLDR;

If you want to use touches, you need to use the Physics feature:

  1. Define Costume and set the default sprite BEFORE using Physics
  2. Use Physics on both the source and target blueprints you want to monitor.
  3. Use Touches on the blueprint that will be running the touch test, and register the agents you want to monitor. (You don't need to use Touches on the target blueprint).
  4. Physics will automatically define the physics body bounds based on the Costume size.
  5. If you want to change the size of the character, set size / scale via Physics so the system can maintain both the visual size and the physics body size.
  6. Touches are processed based on the Physics body size. So you can potentially have a physics body and sprite costume size that are independent of each other.
  7. In general, don't use the following agent props directly:
    • scale
    • scaleY

This is a simpler and more efficient version of Touches that adds support for different types of touches.

  • There are three types of touches:

    1. c2c -- Center to Center
    2. c2b -- Center to Bounds
    3. b2b -- Bounds to Bounds
  • These correspond to new conditions tests:

    1. c2c --> centerTouchesCenter
    2. c2b --> centerTouches
    3. b2b --> touches
  • Each condition test also has a 'firstTouches' and 'lastTouches' variant.

  • It hooks into SIM/AGENTS_UPDATE phase instead of using a rxjs interval timer REVIEW: We're using AGENTS_UPDATE so it doesn't run during PRERUN We might need a new phase or something that only runs when the simulation is running and NOT during PRERUN. It also needs to run BEFORE CONDITIONS_UPDATE, since the conditions tests rely on the calculations.

  • Most calculations are done during the update loop.

  • The touch information is saved into agent. -- agent.lastTouch -- touch info from the previous frame -- agent.isTouching -- touch info from the current frame

  • lastTouch/isTouching are dictionaries, keyed to the target id whose values are the frame number of the detected touch types. e.g.:

    agent.lastTouched[501] = {
      c2c: 1534920,
      c2b: undefined,
      b2b: undefined
    }
    
  • There no feature properties. The data is stored in agent.

  • There are essentially no methods other than 'monitor'. Use conditions tests.

To Use

The key steps:

  1. You have to register the type of touch you want to monitor.
  2. Use the corresponding condition test

Example:

        useFeature Physics
        useFeature Touches
        featCall Touches monitor Algae b2b

        when Fish touches Algae [[ ... ]]

Dependencies:

  • Physics Feature

Example

# PROGRAM DEFINE
useFeature Costume
featCall Costume setCostume 'fish.json' 0

useFeature Physics
useFeature Touches
featCall Touches monitorTouchesWith Algae

# PROGRAM UPDATE
when Fish touches Algae [[
  every 1 runAtStart [[
    prop Fish.energyLevel add 10
    prop Algae.energyLevel sub 10
    featCall Fish.Costume setGlow 0.5
  ]]
]]


Principles

  • touching physics body boundaries vs touching centers

firstTouches condition

See Conditions

lastTouches condition

See Conditions


getTouchingAgent method

Syntax

featCall Touches getTouchingAgent <touchType>

will return the first agent that passes the <touchType> type test relative to this agent.

TouchTypes include:

  • c2c -- center to center
  • c2b -- center to bounds
  • b2b -- bounds to bounds (any edge touching another edge)
  • binb -- bounds in bounds (first agent inside second agent)

This is primarily used by Vision's m_IsTargetColorVisible call to get the background agent of the target.

⚠️ **GitHub.com Fallback** ⚠️