The following code is intended to be used to calculate a new location and rotation angle for a ship token by moving its starting point 70 pixels forward then change its starting rotation (facing) by angleOfCurve degrees and then repeat these steps distanceToMove times. Then I update the final position and rotation angle on the token. Each time I try this the distance moved appears correct but not the final location or the final rotation angle/heading of the ship. Sometimes the ship doesn't move at all. The rotation stick on the token is facing forward on the ship image so the direction should be correct. Any ideas why this doesn't seem to work? Thanks --Andy on('chat:message', function(msg) {
var params = msg.content.split(' ');
if(msg.type == 'api' && msg.selected && msg.content.indexOf('!turn') == 0){
var cmd = params.shift().toLowerCase();
var angleOfCurve = parseFloat(params.shift().toLowerCase());
var distanceToMove = parseFloat(params.shift().toLowerCase());
log(angleOfCurve);
log(distanceToMove);
var selectedObjs = msg.selected;
_.each(selectedObjs, function(obj) {
if(obj._type == 'graphic'){
var token = getObj('graphic', obj._id);
var angle = ((parseFloat(token.get('rotation'))||0)-90) * (Math.PI/180);
var rotation = ((parseFloat(token.get('rotation'))||0)-90);
log(rotation);
var x = parseFloat(token.get('left'));
var y = parseFloat(token.get('top'));
var i;
for(i = 1; i <= distanceToMove; i++)
{
x = x + (70 * Math.cos(angle));
y = y + (70 * Math.sin(angle));
rotation = rotation + angleOfCurve;
if(rotation < 0)
{
rotation = 360 + rotation;
}
if(rotation > 360)
{
rotation = rotation - 360;
}
angle = rotation * (Math.PI/180);
};
token.set({
top: y,
left: x,
});
token.set({rotation: rotation});
log(rotation);
};
});
};
});