Pioneer
Loading...
Searching...
No Matches
CollisionSpace.h
Go to the documentation of this file.
1// Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#ifndef _COLLISION_SPACE
5#define _COLLISION_SPACE
6
7#include "../Aabb.h"
8#include "../vector3.h"
9
10#include <memory>
11#include <vector>
12
13class Geom;
14class SingleBVHTree;
15
16struct isect_t;
17struct CollisionContact;
18
19struct Sphere {
21 double radius;
22 void *userData;
23};
24
25/*
26 * Collision spaces have a bunch of geoms and at most one sphere (for a planet).
27 */
29public:
32 void AddGeom(Geom *);
33 void RemoveGeom(Geom *);
34 void AddStaticGeom(Geom *);
35 void RemoveStaticGeom(Geom *);
36 void TraceRay(const vector3d &start, const vector3d &dir, double len, CollisionContact *c, const Geom *ignore = nullptr);
37 void Collide(void (*callback)(CollisionContact *));
38 void SetSphere(const vector3d &pos, double radius, void *user_data)
39 {
40 sphere.pos = pos;
41 sphere.radius = radius;
42 sphere.userData = user_data;
43 }
44 void FlagRebuildObjectTrees() { m_needStaticGeomRebuild = true; }
45 void RebuildObjectTrees();
46
47 const SingleBVHTree *GetDynamicTree() const { return m_dynamicObjectTree.get(); }
48 const SingleBVHTree *GetStaticTree() const { return m_staticObjectTree.get(); }
49
50 // Geoms with the same handle will not be collision tested against each other
51 // should be used for geoms that are part of the same body
52 // could also be used for autopiloted groups and LRCs near stations
53 // zero means ungrouped. assumes that wraparound => no old crap left
54 static int GetGroupHandle()
55 {
56 if (!s_nextHandle) s_nextHandle++;
57 return s_nextHandle++;
58 }
59
60private:
61 using Intersection = std::pair<uint32_t, uint32_t>;
62
63 void CollideRaySphere(const vector3d &start, const vector3d &dir, isect_t *isect);
64 uint32_t SortEnabledGeoms(std::vector<Geom *> &geoms);
65 void RebuildBVHTree(SingleBVHTree *tree, uint32_t numEnabled, const std::vector<Geom *> &geoms, std::vector<AABBd> &aabbs);
66
67 void CollideGeom(Geom *a, Geom *b, void (*callback)(CollisionContact *));
68 void CollidePlanet(void (*callback)(CollisionContact *));
69 void TraceRayGeom(Geom *g, const vector3d &start, const vector3d &dir, double len, CollisionContact *c);
70
71 std::unique_ptr<SingleBVHTree> m_staticObjectTree;
72 std::unique_ptr<SingleBVHTree> m_dynamicObjectTree;
73
74 std::vector<Geom *> m_staticGeoms;
75 std::vector<Geom *> m_geoms;
76
77 uint32_t m_enabledStaticGeoms;
78 uint32_t m_enabledDynGeoms;
79
80 std::vector<AABBd> m_geomAabbs;
81 Sphere sphere;
82
83 bool m_needStaticGeomRebuild;
84 bool m_duringCollision;
85
86 static int s_nextHandle;
87};
88
89#endif /* _COLLISION_SPACE */
Definition: CollisionSpace.h:28
void RebuildObjectTrees()
Definition: CollisionSpace.cpp:210
void RemoveStaticGeom(Geom *)
Definition: CollisionSpace.cpp:62
CollisionSpace()
Definition: CollisionSpace.cpp:16
void FlagRebuildObjectTrees()
Definition: CollisionSpace.h:44
void AddStaticGeom(Geom *)
Definition: CollisionSpace.cpp:53
const SingleBVHTree * GetStaticTree() const
Definition: CollisionSpace.h:48
void TraceRay(const vector3d &start, const vector3d &dir, double len, CollisionContact *c, const Geom *ignore=nullptr)
Definition: CollisionSpace.cpp:109
void SetSphere(const vector3d &pos, double radius, void *user_data)
Definition: CollisionSpace.h:38
~CollisionSpace()
Definition: CollisionSpace.cpp:27
void AddGeom(Geom *)
Definition: CollisionSpace.cpp:31
void Collide(void(*callback)(CollisionContact *))
Definition: CollisionSpace.cpp:284
static int GetGroupHandle()
Definition: CollisionSpace.h:54
const SingleBVHTree * GetDynamicTree() const
Definition: CollisionSpace.h:47
void RemoveGeom(Geom *)
Definition: CollisionSpace.cpp:39
Definition: Geom.h:16
Definition: BVHTree.h:70
Definition: CollisionContact.h:9
Definition: CollisionSpace.h:19
double radius
Definition: CollisionSpace.h:21
void * userData
Definition: CollisionSpace.h:22
vector3d pos
Definition: CollisionSpace.h:20
Definition: GeomTree.h:17