discontinued 2023 july 29th . by ninekorn

Started by ninekorn, January 24, 2018, 10:04:39 PM

ninekorn

discontinued 2023 july 29th . by ninekorn

ai_enabled

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.

ninekorn

discontinued 2023 july 29th . by ninekorn

ninekorn

discontinued 2023 july 29th . by ninekorn

ai_enabled

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!

ninekorn

discontinued 2023 july 29th . by ninekorn

ai_enabled

>> 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.

ninekorn

discontinued 2023 july 29th . by ninekorn

Lurler

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! :)

ninekorn

discontinued 2023 july 29th . by ninekorn