Marine systems simulation
NewMOC.h
1
9#pragma once
10#include <Ogre.h>
11#include <vector>
12#include <utility>
13#include <unordered_map>
14
15 namespace Collision {
16
17
18 // return struct for the function check_ray_collision (all the data about collision)
19 // collided - weather or not there was a collision and the data in the struct is valid (if false ignore other fields).
20 // position - will contain the collision position
21 // entity - the collided entity
22 // closest_distance - will contain the closest distance we collided with
24 {
25 bool collided;
26 Ogre::Vector3 position;
27 Ogre::Entity* entity;
28 float closest_distance;
29 };
30
31 // different collision types
32 enum ECollisionType
33 {
34 COLLISION_ACCURATE, // will check accurate intersection with all verticles of the entity (for complex shapes that need accurate collision)
35 COLLISION_BOX, // will only check intersection with the bounding box (good for walls and crates-like objects)
36 COLLISION_SPHERE, // will only check interection with the object sphere (good for characters)
37 };
38
39 // holds vertices data of a mesh
40 struct SMeshData
41 {
42 unsigned int ref_count; // how many entities we have with that mesh registered to the collision tools (when 0, this data is deleted)
43 size_t vertex_count; // vertices count
44 Ogre::Vector3* vertices; // vertices
45 size_t index_count; // indices count
46 Ogre::uint32* indices; // indices
47
48 SMeshData() : ref_count(0), vertex_count(0), vertices(nullptr), index_count(0), indices(nullptr)
49 {
50 }
51
52 // delete the inner data
53 void delete_data()
54 {
55 delete[] vertices;
56 delete[] indices;
57 }
58 };
59
60 // data about an entity registered to the collision tools
62 {
63 Ogre::Entity* Entity; // the entity to check
64 ECollisionType CollisionType; // the prefered collision type for this entity
65 bool IsStatic; // is this entity part of a static geometry
66
67 // Data used only for static entities
69 {
70 Ogre::Sphere Sphere; // used only for static objects with sphere collision
71 Ogre::AxisAlignedBox Box; // used only for static objects with box or accurate collision
72 Ogre::Matrix4 Mat; // transformation matrix
73 } *StaticData;
74
75 // delete the static data if have it
76 void remove_static_data()
77 {
78 if (StaticData)
79 delete StaticData;
80 StaticData = nullptr;
81 }
82 };
83
84 // collision detection manager for a specific scene
86
87 private:
88 std::vector<SCollidableEntity> m_Entities; // the entities that are registered for collision checks
89 std::unordered_map<const Ogre::Mesh*, SMeshData> m_MeshesData; // data about meshes we need for accurate collision
90
91 public:
92
95
96 // register a dynamic entity for collision detection
97 void register_entity(Ogre::Entity* Entity, ECollisionType CollisionType = COLLISION_ACCURATE);
98
99 // register a static entity for collision detection
100 void register_static_entity(Ogre::Entity* Entity, const Ogre::Vector3& position, const Ogre::Quaternion orientation, const Ogre::Vector3 scale, ECollisionType CollisionType = COLLISION_ACCURATE);
101
102 // unregister an entity from collision detection (make sure to call this when the entity is deleted!)
103 void remove_entity(Ogre::Entity* Entity);
104
105
106 // check ray collision. check out "SCheckCollisionAnswer" for info about return values.
107 // ray - collision ray to check
108 // queryMask - ogre's query mask (you can set for every entity
109 // ignore - will ignore the entity who has the address of 'ignore'. use this if you want to prevent a character from colliding with itself..
110 // maxDistance - beyond this distance we'll ignore entities
111 // stopOnFirstPositive - if true, will stop on first positive collision (instead of nearest)
112 SCheckCollisionAnswer check_ray_collision(const Ogre::Ray &ray, const Ogre::uint32 queryMask = 0xFFFFFFFF, void* ignore = nullptr, Ogre::Real maxDistance = 0xffff, bool stopOnFirstPositive = false);
113
114 // check ray collision. check out "SCheckCollisionAnswer" for info about return values.
115 // fromPoint - ray starting point
116 // toPoint - ray ending point
117 // collisionRadius - ray 'radius'
118 // rayHeightLevel - will add this factor to the yof the ray.
119 // queryMask - ogre's query mask (you can set for every entity
120 // ignore - will ignore the entity who has the address of 'ignore'. use this if you want to prevent a character from colliding with itself..
121 // stopOnFirstPositive - if true, will stop on first positive collision (instead of nearest)
122 SCheckCollisionAnswer check_ray_collision(const Ogre::Vector3& fromPoint, const Ogre::Vector3& toPoint, const float collisionRadius = 1.0f,
123 const float rayHeightLevel = 0.0f, const Ogre::uint32 queryMask = 0xFFFFFFFF, void* ignore = nullptr, bool stopOnFirstPositive = false);
124
125
126 private:
127
128 // do a simple ray query and return a list of results sorted by distance
129 // NOTE!!! this function only do simple query! it does not do accurate checks or anything, either box collision or sphere collision.
130 // all the accurate checks and range limit is done in one of the 'check_ray_collision' functions
131 // stopOnFirstPositive - if true, will stop on first positive bounding box or sphere collision (not relevant for accurate collisions)
132 typedef std::pair<const SCollidableEntity*, Ogre::Real> RayQueryEntry;
133 std::list<RayQueryEntry> get_basic_ray_query_entities_list(const Ogre::Ray &ray, const Ogre::uint32 queryMask = 0xFFFFFFFF,
134 void* ignore = nullptr, Ogre::Real maxDistance = 0xffff, bool stopOnFirstPositive = false);
135
136 // comparing function to arranage the result list of get_basic_ray_query_entities_list
137 friend bool compare_query_distance (const CollisionTools::RayQueryEntry& first, const CollisionTools::RayQueryEntry& second);
138
139 // add mesh data reference to m_MeshesData map.
140 // if first mesh of this type, create all its data, if already exist just increase the reference
141 void add_mesh_data(const Ogre::Mesh* mesh);
142
143 // remove reference from mesh data. if ref count is 0, data is released
144 void remove_mesh_data(const Ogre::Mesh* mesh);
145
146 // get all the needed information of a mesh
147 // we use this function to create the mesh data hash table for accurate collision
148 void get_mesh_info(const Ogre::Mesh* mesh,
149 size_t &vertex_count,
150 Ogre::Vector3* &vertices,
151 size_t &index_count,
152 Ogre::uint32* &indices);
153
154 };
155 };
Definition: NewMOC.h:85
Definition: NewMOC.h:15
Definition: NewMOC.h:24
Definition: NewMOC.h:62
Definition: NewMOC.h:41