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