PDA

View Full Version : Angles between vectors for InSim


MadCatX
5th January 2012, 21:26
Hi,

I'm not sure if anyone has done anything like this already, but I think it could be useful.

This source library contains a few simple algorithms for vector maths. It can create a 2D vector using X,Y components or an angle off the X axis and do some elementary calculations.

The attached example shows a dumb program which calculates an angle between car A's heading and car B's position.

The code is C++ but it should not be a problem to rewrite it to C# or PHP and include it in popular InSim libraries.

DarkTimes
9th January 2012, 11:00
I did a quick and dirty port to C#. It could probably be improved and made more C#-ish, but it'll do I guess.

It should be noted that if you're going to do lots of vector math in C# I'd recommend getting the XNA Framework library (http://msdn.microsoft.com/en-us/library/bb203940.aspx), as it has tons of stuff for handling vectors.

MadCatX
9th January 2012, 19:58
The XNA seems to have quite rich API for some vector maths, however, I had something more LFS specific in mind. I can imagine tons of situations where you'd want to know an angle between two or more objects, their distance and such things. I was even thinking about some generic algorithm to determine if an object is within a given area of arbitrary shape and other "extras".

It will depend on how much free time I'll have, but I guess especially cruise mods programmers would be happy for that.

DarkTimes
9th January 2012, 20:25
Yeah, something more LFS related could be useful.

I only brought up the XNA Framework as for C# developers it does contain a lot of useful objects and methods for dealing with 2D and 3D maths. For instance if you wanted to find the distance between two points with XNA it's a matter of doing this...

Vector2 a = new Vector2(164, 476);
Vector2 b = new Vector2(435, 676);
float distance = Vector2.Distance(a, b);

There are also lots of helpers for things like collision detection and a very complete maths library.