Character Vault
Any Concept / Any System
Your System Come To Life
Roll20 for Android
Streamlined for your Tablet
Roll20 for iPad
Streamlined for your Tablet

Personal tools

Script:Collision Detection

From Roll20 Wiki

Revision as of 22:14, 8 January 2015 by Brian (Talk | contribs)

Jump to: navigation, search
API ScriptAuthor: Brian
Version: 2.0
Last Modified: 2015-01-08
Code: Collision Detection
Dependencies: None
Conflicts: None

This script will watch for collisions between tokens and a subset of the paths on the player page. When an event occurs, some configurable behavior will be applied.


There are three configuration variables near the top of the script. You may alter them to customize the script functionality:

  • config.pathColor – The script only considers paths of a specific color, allowing you to also use paths of other colors which your players will not collide with. By default, this is fuchsia; the color is specified as a hexadecimal web color, which you can see when selecting a color from the drawing interface. A path's fill color is ignored.
  • config.layer – The script will only look at paths on the specified layer (map, objects, gmlayer, or walls). You can also set this value to "all" and paths on every layer will be considered.
  • config.behavior – There are currently three different behaviors dictating how the script will act when a collision event occurs:
    • behaviors.DONT_MOVE – return the token to its starting position prior to the movement
    • behaviors.WARN_PLAYER – sends a message to the token's controller warning that the token isn't supposed to be there
    • behaviors.STOP_AT_WALL – the token will be moved 1 pixel away from the wall it collided with

You can combine multiple behaviors by using the bitwise OR operator, "|". Example: to use DONT_MOVE and WARN_PLAYER, the behavior should be set to behaviors.DONT_MOVE | behaviors.WARN_PLAYER. STOP_AT_WALL overrides DONT_MOVE if both are set.

Usage Notes

Currently, this script only considers polygons and polylines as "walls" to collide with, which means no freehand drawings or circles/ovals. Additionally, the math in the script does not handle paths which have been resized or rotated.

Tokens which can only be moved by the GM (no player is assigned to control it, and the token isn't linked to a character sheet which is assigned to any player) do not collide with walls. This will let the GM move things around at will. However, if the GM is assigned as the controlling player for a token, or if the token is linked to a character sheet which has the GM assigned as the controlling player, the token will collide with the walls.

The script will break if you go "warp speed" by holding down an arrow key to move, and the token passes through multiple walls before the script catches up. (If you drag a token through multiple walls, the script will collide at the first one.)

In most cases, the dynamic lighting will not update before the token's position is reset to the correct side of the wall (assuming a relevant behavior is set), meaning the player won't see what's on the other side (if the wall is on the DL layer or there's a similarly-positioned wall on the DL layer). However, sometimes the DL will update first, and the player will catch a glimpse of the other side.