1 | /*! |
---|
2 | * @file collision.h |
---|
3 | * Basic collision detection |
---|
4 | */ |
---|
5 | |
---|
6 | #ifndef _COLLISION_H |
---|
7 | #define _COLLISION_H |
---|
8 | |
---|
9 | #include "vector.h" |
---|
10 | #include <stdlib.h> |
---|
11 | #include <stdio.h> |
---|
12 | #include <string.h> |
---|
13 | |
---|
14 | //! Tree structure used by the CollisionCluster |
---|
15 | typedef struct CCTree |
---|
16 | { |
---|
17 | unsigned long n; |
---|
18 | union |
---|
19 | { |
---|
20 | struct CCTree** b; |
---|
21 | unsigned long ID; |
---|
22 | } data; |
---|
23 | float r; |
---|
24 | Vector m; |
---|
25 | } CCTree; |
---|
26 | |
---|
27 | //! Basic collision detection class |
---|
28 | /** |
---|
29 | This class implements a more or less efficient collision system based on nested hitzones. |
---|
30 | Instead of using a brute force approach (try if any hitzone intersects with any other hitzone) |
---|
31 | this features a tree of spheric hitzones. Only some of them are actually the solid groundstones |
---|
32 | the collision model bases on, the others serve to group them into sets of spheres that are only |
---|
33 | checked for collision when the assigned top level sphere has registered a collision, preventing |
---|
34 | unnessessary checks (like between every sphere of two collision clusters at the other end of the world) |
---|
35 | from being performed. |
---|
36 | The CollisionCluster features collision detection between multiple CollisionClusters as well as |
---|
37 | traceing a collision between a line of defined length and a cluster. In both cases the base spheres |
---|
38 | that have intersected are marked with a flag in an unsigned long for hitlocation queries. In the case |
---|
39 | of a trace, the exact point of interception is returned as well. |
---|
40 | */ |
---|
41 | class CollisionCluster { |
---|
42 | |
---|
43 | CCTree* root; |
---|
44 | |
---|
45 | |
---|
46 | public: |
---|
47 | CollisionCluster (float r, Vector m); // simple cluster |
---|
48 | CollisionCluster (char* filename); // get cluster from file |
---|
49 | ~CollisionCluster (); |
---|
50 | |
---|
51 | int store (char* filename); |
---|
52 | |
---|
53 | friend bool ccTreeTrace( const Placement* p, CCTree* t, unsigned long* hitflags, const Line* trace, Vector* impactpoint); |
---|
54 | friend bool ccTreeIterate(const Placement* pa, CCTree* ta, unsigned long* ahitflags, const Placement* pb, CCTree* tb, unsigned long* bhitflags); |
---|
55 | friend bool checkTrace (const Placement* pa, const CollisionCluster* a, unsigned long* ahitflags, const Line* trace, Vector* impactpoint); |
---|
56 | friend bool checkCollision (const Placement* pa, const CollisionCluster* a, unsigned long* ahitflags, const Placement* pb, const CollisionCluster* b, unsigned long* bhitflags); |
---|
57 | }; |
---|
58 | |
---|
59 | bool sphereSphereCollision( Vector m1, float r1, Vector m2, float r2); |
---|
60 | bool traceSphereCollision( Vector m, float r, const Line* l, Vector* impactpoint); |
---|
61 | |
---|
62 | void setflag( unsigned long* flags, unsigned long ID); |
---|
63 | |
---|
64 | void freeCCTree( CCTree* tree); |
---|
65 | CCTree* loadCCTree (FILE* stream); |
---|
66 | int saveCCTree (CCTree* tree, FILE* stream); |
---|
67 | |
---|
68 | #endif /* _COLLISION_H */ |
---|