It seems I found out a solution, even though there is still something bugging me about it. Writing this post helped me clear my head and my syntax to explain my problem. No more help requiered about this specific instance. Sorry I wrote this “dead” post. I didn’t find the need to delete it, if the idea of double weighting could help someone. If it did, I would love to hear a thank you in my inbox
For any curious coder, the solution of the problem below is : “don’t compute vectorial stuff when the coin is too far away to matter”. I added a condition. Instead of
var facingValue = 3 + cosinusOf( Vector( this.pos , currentCoin.pos) , Vector( this.pos , goldBarycenter.pos);
I wrote :
if(currCoinDist<15){
var cosine= cosinusOf( Vector( this.pos , currentCoin.pos) , Vector( this.pos , goldBarycenter.pos);
}else{
cosine=0;
}
var facingValue=3+cosine;
Original post
Hello there,
I’m having a bit of trouble finding out why my code is bringing me a HardExecutionLimit, and I think I’ve narrowed it down to my vectorial functions.
In order to find the best coin to go to, I check the value of the distance/goldValue of the coin and I make a loop throught all the coins to find the minimum.
As I refined this idea, I wanted to add another weight : facingValue, so that the variable to minimize would be
var leastDistance = distance / ( goldValue * facingValue )
if the coin makes you go towards the bulk of the gold on the ground -> good ( facingValue = 4); if it makes you walk away from most of the gold -> bad. (facingValue = 2). It means that a coin that’s making go away from goldBarycenter will appear twice less appealing that a similar coin helping you go toward goldBarycenter.
So it comes down to a simple formula (wikipedia dotProduct) :
var facingValue = 3 + cosinusOf( Vector( this.pos , currentCoin.pos) , Vector( this.pos , goldBarycenter.pos);
with the vectorial functions being :
function Vector(head,tail){
return {x:(head.x-tail.x),y:(head.y-tail.y)};
}
function cosinusOf(vect1,vect2){
var res=dotProd(vect1,vect2)/Math.sqrt(dotProd(vect1,vect1)*dotProd(vect2,vect2));
res;
}
function dotProd(vect1,vect2){
return vect1.x*vect2.x+vect1.y*vect2.y;
}
In order to debug my code :
- Everything runs smoothly if I skip the computation of facingValue, and I set its value to 1
- I get a HardExecutionLimit reach around 30sec in combat when I compute facingValue.
- The problem doesn’t come from the computation of the goldBarycenter, because even if I set it to a fixed position (ex : center of the battlefield ; no computation requiered), I still reach a HardExecutionLimit around 30sec in combat.
I don’t get it. What’s wrong with my dotProd() & cosinusOf() functions ? Few “+” and “x” are really this expensive to compute ? Any tip is very welcomed.
PS : interesting library I’d like to be able to fully understand.