AtomicTorch Studio Forums

VoidExpanse => Help section => Topic started by: ninekorn on January 24, 2018, 10:04:39 PM

Title: discontinued 2023 july 29th . by ninekorn
Post by: ninekorn on January 24, 2018, 10:04:39 PM
discontinued 2023 july 29th . by ninekorn
Title: Re: Math.sqrt alternative for calculating the length of a vector2d?
Post by: ai_enabled on January 24, 2018, 10:17:20 PM
Hi nine,

There are two functions to calculate the dot product between two 2D vectors A and B:

// first approach uses vector lengths and so requires expensive sqrt calculation
var dot = a.Length * b.Length * Math.Cos(angle); // it also uses angle between A and B vectors

// second approach is much simpler
var dot = a.X * b.X + a.Y * b.Y;


So you can simply use the second approach to calculate the dot product.
If you need to get the angle between them, you can use this function:

var angle = Math.Acos(dot / (a.Length * b.Length));


Regards!

UPD. Oops, I was wrong :-), corrected the post.
So you actually need vector lengths (and so sqrt) to calculate the angle between vectors with the dot product (IF the dot product alone is not enough for your case).
I think this is a premature optimization. Even thousands of sqrt calculations per second are not that slow on modern CPUs. Don't optimize until you actually can feel that it's too slow.
Title: Re: Math.sqrt alternative for calculating the length of a vector2d?
Post by: ninekorn on January 24, 2018, 10:50:30 PM
discontinued 2023 july 29th . by ninekorn
Title: Re: Math.sqrt alternative for calculating the length of a vector2d?
Post by: ninekorn on January 26, 2018, 02:26:16 AM
discontinued 2023 july 29th . by ninekorn
Title: Re: Math.sqrt alternative for calculating the length of a vector2d?
Post by: ai_enabled on January 26, 2018, 03:09:10 AM
atan2 is the way we're usually using to get the angle between two vectors.
Actually, atan2 call is a little bit slower than sqrt on modern CPUs.
Anyway, it's not something to worry about until you really perceive the issue with the performance.

Regards!
Title: Re: Math.sqrt alternative for calculating the length of a vector2d?
Post by: ninekorn on January 26, 2018, 03:24:03 AM
discontinued 2023 july 29th . by ninekorn
Title: Re: Math.sqrt alternative for calculating the length of a vector2d?
Post by: ai_enabled on January 26, 2018, 03:26:25 AM
>> It's a tiny lag spike just when the AI's pathfind activates

Does it happen for every new drone AI or just a single time? Sounds like a script loading/compilation lag, in that case you can't do much.
Title: Re: Math.sqrt alternative for calculating the length of a vector2d?
Post by: ninekorn on January 26, 2018, 09:14:05 AM
discontinued 2023 july 29th . by ninekorn
Title: Re: Math.sqrt alternative for calculating the length of a vector2d?
Post by: Lurler on January 28, 2018, 09:30:03 PM
Sorry to interrupt your conversation, but I'm just amazed at seeing this kind of mods being developed for VoidExpanse :)
Please keep up the good work and I really hope to see this released when you finish everything you have planned! :)
Title: Re: Math.sqrt alternative for calculating the length of a vector2d?
Post by: ninekorn on January 31, 2018, 10:07:57 PM
discontinued 2023 july 29th . by ninekorn