-
Notifications
You must be signed in to change notification settings - Fork 3
/
collide.h
185 lines (161 loc) · 6.78 KB
/
collide.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifndef COLLIDE_H
#define COLLIDE_H
#include "vector.h"
#include "march.h"
#include "PxPhysicsVersion.h"
#include "PxPhysics.h"
#include "extensions/PxDefaultStreams.h"
#include "extensions/PxDefaultAllocator.h"
#include "extensions/PxDefaultErrorCallback.h"
#include "geometry/PxTriangleMeshGeometry.h"
#include "cooking/PxTriangleMeshDesc.h"
#include "cooking/PxCooking.h"
#include "extensions/PxTriangleMeshExt.h"
#include "PxQueryReport.h"
#include "geometry/PxGeometryQuery.h"
#include <list>
#include <map>
#include <set>
#include <deque>
#include <algorithm>
#include <iostream>
using namespace physx;
class Physicer;
class Object;
class GeometrySet;
class PhysicsGeometry {
public:
PhysicsGeometry(PxTriangleMesh *triangleMesh, PxConvexMesh *convexMesh, PxGeometry *geometry);
~PhysicsGeometry();
PxTriangleMesh *triangleMesh;
PxConvexMesh *convexMesh;
PxGeometry *geometry;
};
class PhysicsObject {
public:
PhysicsObject(unsigned int objectId, Vec objectPosition, Quat objectQuaternion, std::shared_ptr<PhysicsGeometry> physicsGeometry, Vec position, Quat quaternion, Physicer *physicer);
~PhysicsObject();
unsigned int objectId;
Vec objectPosition;
Quat objectQuaternion;
std::shared_ptr<PhysicsGeometry> physicsGeometry;
Vec position;
Quat quaternion;
Physicer *physicer;
};
class Physicer {
public:
Physicer();
PxDefaultAllocator *gAllocator = nullptr;
PxDefaultErrorCallback *gErrorCallback = nullptr;
PxFoundation *gFoundation = nullptr;
PxPhysics *physics = nullptr;
PxCooking *cooking = nullptr;
};
/* void doInitPhysx() {
gAllocator = new PxDefaultAllocator();
gErrorCallback = new PxDefaultErrorCallback();
gFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, *gAllocator, *gErrorCallback);
PxTolerancesScale tolerancesScale;
physics = PxCreatePhysics(PX_PHYSICS_VERSION, *gFoundation, tolerancesScale);
PxCookingParams cookingParams(tolerancesScale);
// cookingParams.midphaseDesc = PxMeshMidPhase::eBVH34;
cooking = PxCreateCooking(PX_PHYSICS_VERSION, *gFoundation, cookingParams);
} */
std::shared_ptr<PhysicsGeometry> doMakeBakedGeometryRaw(Physicer *physicer, PxDefaultMemoryOutputStream *writeStream);
std::shared_ptr<PhysicsGeometry> doMakeBakedConvexGeometryRaw(Physicer *physicer, PxDefaultMemoryOutputStream *writeStream);
std::shared_ptr<PhysicsObject> doMakeBakedGeometry(Physicer *physicer, PxDefaultMemoryOutputStream *writeStream, unsigned int objectId, float *meshPosition, float *meshQuaternion);
std::shared_ptr<PhysicsObject> doMakeBakedConvexGeometry(Physicer *physicer, PxDefaultMemoryOutputStream *writeStream, unsigned int objectId, float *meshPosition, float *meshQuaternion);
std::shared_ptr<PhysicsObject> doMakeGeometry(Physicer *physicer, PxGeometry *geometry, unsigned int objectId, float *meshPosition, float *meshQuaternion);
std::shared_ptr<PhysicsObject> doMakeBoxGeometry(Physicer *physicer, unsigned int objectId, float *objectPosition, float *objectQuaternion, float *position, float *quaternion, float w, float h, float d);
std::shared_ptr<PhysicsObject> doMakeCapsuleGeometry(Physicer *physicer, unsigned int objectId, float *objectPosition, float *objectQuaternion, float *position, float *quaternion, float radius, float halfHeight);
PxDefaultMemoryOutputStream *doBakeGeometry(Physicer *physicer, float *positions, unsigned int *indices, unsigned int numPositions, unsigned int numIndices);
PxDefaultMemoryOutputStream *doBakeConvexGeometry(Physicer *physicer, float *positions, unsigned int *indices, unsigned int numPositions, unsigned int numIndices);
/* void doUnregisterGeometry(PhysicsGeometry * geometrySpec) {
{
std::lock_guard<std::mutex> lock(gPhysicsMutex);
for (std::set<PhysicsGeometry *> *geometrySpecSet : geometrySpecSets) {
geometrySpecSet->erase(PhysicsGeometry);
}
}
delete geometrySpec;
} */
class Shape {
public:
Vec position;
Quat quaternion;
Vec scale;
};
extern std::map<std::string, Shape> PHYSICS_SHAPES;
void doLandPhysics(Tracker *tracker, Subparcel *subparcel, float *landPositions, unsigned int numLandPositions);
void doObjectPhysics(Tracker *tracker, GeometrySet *geometrySet, Subparcel *subparcel);
void doThingPhysics(Tracker *tracker, GeometrySet *geometrySet, Subparcel *subparcel);
void doRaycast(Tracker *tracker, float *origin, float *direction, float *meshPosition, float *meshQuaternion, unsigned int &hit, float *position, float *normal, float &distance, unsigned int &objectId, unsigned int &faceIndex, Vec &outPosition, Quat &outQuaternion);
void doCollide(Tracker *tracker, float radius, float halfHeight, float *position, float *quaternion, float *meshPosition, float *meshQuaternion, unsigned int maxIter, unsigned int &hit, float *direction, unsigned int &grounded);
extern int PEEK_FACE_INDICES[];
/* (() => {
const directionsLookup = {
1: 'FRONT',
2: 'BACK',
3: 'LEFT',
4: 'RIGHT',
5: 'TOP',
6: 'BOTTOM',
};
const indexDirections = [];
const PEEK_FACE_INDICES = Array((6<<4|6)+1);
for (let i = 0; i < (6<<4|6)+1; i++) {
PEEK_FACE_INDICES[i] = 0xFF;
}
let peekIndex = 0;
for (let i = 1; i <= 6; i++) {
for (let j = 1; j <= 6; j++) {
if (i != j) {
const otherEntry = PEEK_FACE_INDICES[j << 4 | i];
PEEK_FACE_INDICES[i << 4 | j] = otherEntry != 0xFF ? otherEntry : (() => {
const newIndex = peekIndex++;
indexDirections[newIndex] = directionsLookup[i] + ' -> ' + directionsLookup[j];
return newIndex;
})();
}
}
}
x = PEEK_FACE_INDICES;
y = indexDirections;
})(); */
class PeekDirection {
public:
Vec normal;
int inormal[3];
PEEK_FACES face;
};
extern PeekDirection PEEK_DIRECTIONS[6];
/* class GroupSet {
public:
GroupSet(int x, int y, int z, int index, const Sphere &boundingSphere, unsigned char *peeks, Group *groups, unsigned int numGroups);
int x;
int y;
int z;
int index;
Sphere boundingSphere;
unsigned char peeks[15];
std::vector<Group> groups;
};
class Culler {
public:
Culler();
std::vector<GroupSet *> groupSets;
}; */
class CullResult {
public:
CullResult(unsigned int start, unsigned int count, unsigned int materialIndex);
unsigned int start;
unsigned int count;
unsigned int materialIndex;
};
/* Culler *doMakeCuller();
GroupSet *doRegisterGroupSet(Culler *culler, int x, int y, int z, float r, unsigned char *peeks, Group *groups, unsigned int numGroups);
void doUnregisterGroupSet(Culler *culler, GroupSet *groupSet);
void doCull(Culler *culler, float *positionData, float *matrixData, float slabRadius, CullResult *cullResults, unsigned int &numCullResults); */
void doTickCull(Tracker *tracker, float *positionData, float *matrixData, CullResult *landCullResults, unsigned int &numLandCullResults, CullResult *vegetationCullResults, unsigned int &numVegetationCullResults, CullResult *thingCullResults, unsigned int &numThingCullResults);
#endif