Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[Help] Collisions

May 04 (8 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Hi all,

I'm looking at designing a script that will rely on collision detection, but I want the last collision detected in a movement, not the first. I've been thinking about using Stephen's TokenCollisions script, but I'm not entirely sure how to access it's objects, utilize them, or if there might not be a better collision detector system (there seem to be several) out there.

Thanks,

Scott
May 04 (8 years ago)

Edited May 04 (8 years ago)
Ada L.
Marketplace Creator
Sheet Author
API Scripter
That is a very old version of my TokenCollisions script there. You can get the newest version here: https://github.com/Roll20/roll20-api-scripts/tree/...

With this script, you can very easily get the last collision during a token's movement. All you would need to do is this:

var collisions = TokenCollisions.getCollisions(token, otherTokens);
var lastCollision = _.last(collisions);

My script only detects collisions between tokens though. It also makes the assumption that each token takes up a circular area with a diameter equal to its width.

If you're instead wanting to detect collisions with dynamic lighting walls you might want to check out Brian Shields' Collision Detection script: https://github.com/Roll20/roll20-api-scripts/tree/...
May 05 (8 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Ah, thank you Stephen, that was very helpful. I had been looking at the one-click install version of the script, and just didn't realize the forum post linked to a different one.
May 05 (8 years ago)

Edited May 05 (8 years ago)
Ada L.
Marketplace Creator
Sheet Author
API Scripter
No problem. Perhaps I should update the One-Click descriptions to include documentation for the functions exposed by TokenCollisions and some of my other utility scripts, just as a convenience for anyone who wants to use them for their own scripts in the future.
May 12 (8 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Stephen L. said:
Update:
Token Collisions 1.3 is now unofficially available, with support for rectangular collisions (including rotated rectangles).
This will allow collisions with rectangular traps.

It's not merged yet, but if you guys want to play with it, you can replace whichever version of Token Collisions you have with the one on my 1.3 branch here: https://github.com/Cazra/roll20-api-scripts/tree/T...

To set a trap as rectangular using Token Collisions 1.3, just set the trap's Aura1 to be a square.
Hi Stephen,

I'm getting an error using the new Token Collisions 1.3.
TypeError: other.get is not a function TypeError: other.get is not a function at apiscript.js:175:24 at /home/node/d20-api-server/node_modules/underscore/underscore.js:380:19 at Function._.map._.collect (/home/node/d20-api-server/node_modules/underscore/underscore.js:172:24) at Function._.sortBy (/home/node/d20-api-server/node_modules/underscore/underscore.js:376:22) at _.(anonymous function) [as sortBy] (/home/node/d20-api-server/node_modules/underscore/underscore.js:1496:34) at _getCollisionsInWaypoint (apiscript.js:167:11) at apiscript.js:141:20 at Function._.map._.collect (/home/node/d20-api-server/node_modules/underscore/underscore.js:172:24) at _.(anonymous function) [as map] (/home/node/d20-api-server/node_modules/underscore/underscore.js:1496:34) at Object.getCollisions (apiscript.js:140:12)
I think it's from this codes segment (lines 55-87):

function _getCollisionsInWaypoint(token, others, waypoint) {
       var startPt = waypoint[0];
       var endPt = waypoint[1];
       var numCollisions = 0;
       return _.chain(others)
         // Get the list of tokens that actually collide, sorted by the distance
         // from the starting point at which they occur.
         .sortBy(function(other) {
           var dist;
           <strong>if(token.get('aura1_square'))
              if(other.get('aura1_square'))
                dist = _testRectsCollision(token, other, waypoint);
              else
                dist = _testRectCircleCollision(token, other, waypoint);
           else
              if(other.get('aura1_square'))
                dist = _testCircleRectCollision(token, other, waypoint);
              else
                dist = _testCirclesCollision(token, other, waypoint);
</strong>

           if(dist !== undefined)
             numCollisions++;
           return dist;
         })
         // Other tokens with undefined collision distance will be sorted to the high
         // end of the list. So, we'll just drop them.
         .first(numCollisions)
         .value();
     }
But other than that I'm stymied. I'm trying to call Token Collisions with the following code:
   getDestinationCollisions = function(token) {
        var pageId,
        destinations,
        collisions,
        lastCollision;
        pageId = token.get('_pageid');
        destinations = getDestinations(pageId);
        collisions = TokenCollisions.getCollisions(token, destinations);
        lastCollision = _.last(collisions);
   },
    
    getDestinations = function(pageId){
        return findObjs({
            pageid: pageId,
            type: 'graphic',
            layer: /*'map' ||*/ 'gmlayer',
            statusmarkers: 'aura'
        })[0];
    },
If I comment out the "collisions =" and "lastCollisions =" lines and log all the variables I get:
"pageId"
"-Jo6Dd7nZqSelv3fMvdG"
"token"
{"_id":"-Jp2cpJ5LRb6rewwjAJg","_pageid":"-Jo6Dd7nZqSelv3fMvdG","left":3395,"top":1085,"width":70,"height":70,"rotation":0,"layer":"objects","isdrawing":false,"flipv":false,"fliph":false,"imgsrc":"https://s3.amazonaws.com/files.d20.io/images/7039467/f7qYLQ3M1plP_Fg1wFQkcg/thumb.png?1420690169","name":"","gmnotes":"","controlledby":"","bar1_value":"","bar1_max":"","bar1_link":"","bar2_value":"","bar2_max":"","bar2_link":"","bar3_value":"","bar3_max":"","bar3_link":"","represents":"","aura1_radius":"","aura1_color":"#FFFF99","aura1_square":false,"aura2_radius":"","aura2_color":"#59E594","aura2_square":false,"tint_color":"transparent","statusmarkers":"","showname":false,"showplayers_name":false,"showplayers_bar1":false,"showplayers_bar2":false,"showplayers_bar3":false,"showplayers_aura1":false,"showplayers_aura2":false,"playersedit_name":true,"playersedit_bar1":true,"playersedit_bar2":true,"playersedit_bar3":true,"playersedit_aura1":true,"playersedit_aura2":true,"light_radius":"","light_dimradius":"","light_otherplayers":false,"light_hassight":false,"light_angle":"","light_losangle":"","light_multiplier":1,"sides":"","currentSide":0,"lastmove":"3395,1155","_type":"graphic","_subtype":"token","_cardid":""}
"destinations"
{"_id":"-KHXcUKIYyHbJGZIf8do","_pageid":"-Jo6Dd7nZqSelv3fMvdG","left":3395,"top":1050,"width":210,"height":140,"rotation":0,"layer":"gmlayer","isdrawing":false,"flipv":false,"fliph":false,"imgsrc":"https://s3.amazonaws.com/files.d20.io/images/17909715/MSbM_vNTea-EnPc74F_DFA/thumb.png?1459987051","name":"Scrapwall","gmnotes":"","controlledby":"","bar1_value":"","bar1_max":"","bar1_link":"","bar2_value":"","bar2_max":"","bar2_link":"","bar3_value":"","bar3_max":"","bar3_link":"","represents":"","aura1_radius":"","aura1_color":"#FFFF99","aura1_square":true,"aura2_radius":"","aura2_color":"#59E594","aura2_square":false,"tint_color":"transparent","statusmarkers":"aura","showname":false,"showplayers_name":false,"showplayers_bar1":false,"showplayers_bar2":false,"showplayers_bar3":false,"showplayers_aura1":false,"showplayers_aura2":false,"playersedit_name":true,"playersedit_bar1":true,"playersedit_bar2":true,"playersedit_bar3":true,"playersedit_aura1":true,"playersedit_aura2":true,"light_radius":"","light_dimradius":"","light_otherplayers":false,"light_hassight":false,"light_angle":"","light_losangle":"","light_multiplier":1,"sides":"","currentSide":0,"lastmove":"3325,1085","_type":"graphic","_subtype":"token","_cardid":""}
So I'm reasonably sure I'm passing valid objects to TokenCollisions. Any ideas?

Thanks,

Scott
May 12 (8 years ago)

Edited May 12 (8 years ago)
Ada L.
Marketplace Creator
Sheet Author
API Scripter
I actually saw your post first in the traps thread and replied to it there. 

The problem is that you're passing in a single token for others when TokenCollisions.getCollisions expects a list of tokens for others. You can fix this by removing the [0] in getDestinations.