[INFO/LIB] Detect objects in a cone / Get FPS
Posted: Wed, 23. Apr 08, 20:09
(Get FPS: see below)
For a long time I've been looking for a way to do this but nothing was even remotely fast and effective enough.
- A string of increasingly larger find-bubbles? Not a real cone, lots of work to remove all the duplicates, lots of overlap with the bubbles and thus a lot of CPU time wasted.
- Full blown trigonometry for every object in the sector. Hahaha! Sure pal.
What I finally came up with (for my RADAR sensor cone thingy) takes no more than 1 Find and 1 very short loop as a filter.
There is no range limit and every object is computed exactly once.
This calculation uses 2 points to define the origin and direction of the cone.
In my case it was
1. the ship
2. a point 5000m behind it.
This means the cone extends to the front.
2. was easy to find with UniTrader's lib.ut.pos.from.2.grades.and.rad.
00 ObjArray = Find blah
01 Point = Punkt behind ship 5000
02
03 index = size of array
04 Filter:
05 While index
06 dec index
07 Obj = ObjArray (index)
08 Dist.Ship = distance between Ship and Obj
09 Dist.Point = distance between Point and Obj
10 skip if not Dist.Ship + 4700 > Dist.Point
11 remove element from array ObjArray at index index
12 end
The number 4700 determines the angle of the cone.
The full list is found here.
4860 : 30°
4710 : 45°
4630 : 50°
4380 : 65°
4300 : 80°
3860 : 90°
2520 : 135°
With no more than 3 instructions (lines 5-7) I can determine if the object is in the cone or not.
Compare that to doing full trigonometry in 3D for every single object when the ship itself can be pointing any angle to begin with...
Note that if you reverse the formula you can get the angle value. How far your ship needs to turn to align itself with object X...
[ external image ][ external image ] . . . same function, other resolution.
At long ranges (005 - 600 km) the cone looks pretty and linear but a close-up on the zero-crossing reveals that it is really a parabola.
Using the "4700" angle, the base of the parabola is 145 m behind the ship.
If you need higher accuracy, displace the 2 points of reference.
You will have to experiment a little with different angles... or find a radically different approach. (tell me if you do =)
Which way you filter the result is completely up to you. You could get ships that are neither in front nor behind your ship within an angle of x°...
I can imagine several uses for navigation or combat scripts but it certainly works for my case. =)
For a long time I've been looking for a way to do this but nothing was even remotely fast and effective enough.
- A string of increasingly larger find-bubbles? Not a real cone, lots of work to remove all the duplicates, lots of overlap with the bubbles and thus a lot of CPU time wasted.
- Full blown trigonometry for every object in the sector. Hahaha! Sure pal.
What I finally came up with (for my RADAR sensor cone thingy) takes no more than 1 Find and 1 very short loop as a filter.
There is no range limit and every object is computed exactly once.
This calculation uses 2 points to define the origin and direction of the cone.
In my case it was
1. the ship
2. a point 5000m behind it.
This means the cone extends to the front.
2. was easy to find with UniTrader's lib.ut.pos.from.2.grades.and.rad.
00 ObjArray = Find blah
01 Point = Punkt behind ship 5000
02
03 index = size of array
04 Filter:
05 While index
06 dec index
07 Obj = ObjArray (index)
08 Dist.Ship = distance between Ship and Obj
09 Dist.Point = distance between Point and Obj
10 skip if not Dist.Ship + 4700 > Dist.Point
11 remove element from array ObjArray at index index
12 end
The number 4700 determines the angle of the cone.
The full list is found here.
4860 : 30°
4710 : 45°
4630 : 50°
4380 : 65°
4300 : 80°
3860 : 90°
2520 : 135°
With no more than 3 instructions (lines 5-7) I can determine if the object is in the cone or not.
Compare that to doing full trigonometry in 3D for every single object when the ship itself can be pointing any angle to begin with...
Note that if you reverse the formula you can get the angle value. How far your ship needs to turn to align itself with object X...
[ external image ][ external image ] . . . same function, other resolution.
At long ranges (005 - 600 km) the cone looks pretty and linear but a close-up on the zero-crossing reveals that it is really a parabola.
Using the "4700" angle, the base of the parabola is 145 m behind the ship.
If you need higher accuracy, displace the 2 points of reference.
You will have to experiment a little with different angles... or find a radically different approach. (tell me if you do =)
Which way you filter the result is completely up to you. You could get ships that are neither in front nor behind your ship within an angle of x°...
I can imagine several uses for navigation or combat scripts but it certainly works for my case. =)