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

Token Rotation Based on Target

Looking for an API or other method that would automatically rotate a token to face a @{target} selection.
1653305828
Oosh
Sheet Author
API Scripter
See if this does what you're looking for. It only takes one selected target and one target click, run it with the macro: !faceme --s @{selected|token_id} --t @{target|token_id} /* globals getObj, on */ on('ready', () => { on('chat:message', (msg) => { if (msg.type === 'api' && /^!faceme/i.test(msg.content)) { const selectedId = (msg.content.match(/--s(elected)?\s*(-[A-Za-z0-9_-]{19})/)||[])[2], targetId = (msg.content.match(/--t(arget)?\s*(-[A-Za-z0-9_-]{19})/)||[])[2], selectedToken = selectedId ? getObj('graphic', selectedId) : null, targetToken = targetId ? getObj('graphic', targetId) : null; if (!targetToken || !selectedToken) return; const targetPos = { x: parseInt(targetToken.get('left'))||0, y: parseInt(targetToken.get('top'))||0 }; if (selectedToken.get('_subtype') === 'token') { const selectedPos = { x: parseInt(selectedToken.get('left'))||0, y: parseInt(selectedToken.get('top'))||0, }; const delta = { x: targetPos.x - selectedPos.x, y: selectedPos.y - targetPos.y, }; const angleFromY = Math.atan(delta.x/delta.y), degreesFromY = angleFromY*(180/Math.PI), degreesFromZero = delta.y > 0 ? angleFromY >= 0 ? degreesFromY : 360 + degreesFromY : delta.y < 0 ? 180 + degreesFromY : 180 - degreesFromY; // console.info(`Rads from Y: ${angleFromY}, degrees from Y: ${degreesFromY}, degrees from 0: ${degreesFromZero}`); selectedToken.set('rotation', degreesFromZero); } } }); });
1653317589
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Heh, combine this with a bullseye lantern and you have spotlights!
1653353066
Oosh
Sheet Author
API Scripter
Combine that with an automated tint & rotation, and you have a disco!
Thanks. Works great. Just need to make sure my tokens begin oriented properly.
1653361746
Victor B.
Pro
Sheet Author
API Scripter
Is this for flanking rules?  I've been debating playing them.  This might provide a way to make that happen
1653366539
Oosh
Sheet Author
API Scripter
Peacekeeper B said: Thanks. Works great. Just need to make sure my tokens begin oriented properly. Hrmm... what do you mean? The rotation is set to absolute degrees, and isn't relative to the current angle of rotation at all. Is something not working properly? The selected token should face the target token no matter what the initial rotation is. Or do you mean the artwork for the token? I can chuck an offset option in there if you like, it's probably easier than rotating a heap of token art. Also, does the script need to be able to rotate more than one selected token at once, or would that not really get used? They would all end up facing the same target, of course.
Just the art facing. The api treats the top as the facing but must too down tokens face the other way.
1653807508
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Peacekeeper B said: Just the art facing. The api treats the top as the facing but must too down tokens face the other way. Yeah, that was a convention that started early in roll20's history. The app was designed with the handle/front up, but everyone designing top down tokens drew them facing down. It's too late to really change either now without invalidating a whole lot of work and doubtless angering entire camps of people.
1653839220

Edited 1653839304
Oosh
Sheet Author
API Scripter
So.... an offest setting for a token useful? Or not needed? I'm thinking something like "!faceme --offset 90" ... this would set the token to add 90 degrees to the calculation and offset the entire thing, so the handle would be pointing the "wrong" (offset) angle, but the artwork would look right. If that's not useful I won't bother, i don't think there's any way to fix the artwork via API alone. edit - a permanent setting for the token, not something that needs to be done each time. I think burying it in the imgsrc querystring should work, haven't tested it yet though
I think the offset would be a great addition and useful for those who use it. Especially if it could pull an attribute from a character sheet. @{selected|token_facing} So when a player/GM assigns a token they can assign a proper value to represent what they think or want to be the facing.  I want to use this as part of a macro (preferably a power card) so it works with a one click attack. Oosh said: So.... an offest setting for a token useful? Or not needed? I'm thinking something like "!faceme --offset 90" ... this would set the token to add 90 degrees to the calculation and offset the entire thing, so the handle would be pointing the "wrong" (offset) angle, but the artwork would look right. If that's not useful I won't bother, i don't think there's any way to fix the artwork via API alone. edit - a permanent setting for the token, not something that needs to be done each time. I think burying it in the imgsrc querystring should work, haven't tested it yet though
1653882382

Edited 1653882402
Victor B.
Pro
Sheet Author
API Scripter
If you are doing flanking, token alignment won't do anything otherwise cause confusion.  Trust me on this.  Flanking rules are cool, but don't pan out 
1653946870

Edited 1653947057
My main intent is so tokens face their targets and any special effect/fx is not coming out of the token's ass. Secondly, facing allows me to adjudicate flanking better. And yes it flanking rules are cool. Trust me, they pan out. Victor B. said: If you are doing flanking, token alignment won't do anything otherwise cause confusion.  Trust me on this.  Flanking rules are cool, but don't pan out