re3/src/core/Collision.h

252 lines
7.9 KiB
C
Raw Normal View History

2019-05-15 14:52:37 +00:00
#pragma once
#include "templates.h"
#include "Game.h" // for eLevelName
2020-08-03 10:58:37 +00:00
#ifdef VU_COLLISION
#include "VuVector.h"
#endif
2019-05-15 14:52:37 +00:00
2020-04-15 16:19:45 +00:00
// If you spawn many tanks at once, you will see that collisions of two entity exceeds 32.
#if defined(FIX_BUGS) && !defined(SQUEEZE_PERFORMANCE)
2020-04-15 16:19:45 +00:00
#define MAX_COLLISION_POINTS 64
#else
#define MAX_COLLISION_POINTS 32
#endif
2020-07-27 13:38:12 +00:00
struct CompressedVector
{
#ifdef COMPRESSED_COL_VECTORS
int16 x, y, z;
CVector Get(void) const { return CVector(x, y, z)/128.0f; };
void Set(float x, float y, float z) { this->x = x*128.0f; this->y = y*128.0f; this->z = z*128.0f; };
2020-08-03 10:58:37 +00:00
#ifdef GTA_PS2
void Unpack(uint128 &qword) const {
__asm__ volatile (
"lh $8, 0(%1)\n"
"lh $9, 2(%1)\n"
"lh $10, 4(%1)\n"
"pextlw $10, $8\n"
"pextlw $2, $9, $10\n"
"sq $2, %0\n"
: "=m" (qword)
: "r" (this)
: "$8", "$9", "$10", "$2"
);
}
#else
void Unpack(int32 *qword) const {
qword[0] = x;
qword[1] = y;
qword[2] = z;
qword[3] = 0; // junk
}
#endif
2020-07-27 13:38:12 +00:00
#else
float x, y, z;
CVector Get(void) const { return CVector(x, y, z); };
void Set(float x, float y, float z) { this->x = x; this->y = y; this->z = z; };
#endif
};
2019-05-15 14:52:37 +00:00
struct CColSphere
{
2020-08-03 10:58:37 +00:00
// NB: this has to be compatible with a CVuVector
2019-05-15 14:52:37 +00:00
CVector center;
float radius;
uint8 surface;
uint8 piece;
void Set(float radius, const CVector &center, uint8 surf, uint8 piece);
void Set(float radius, const CVector &center) { this->center = center; this->radius = radius; }
};
struct CColBox
{
CVector min;
CVector max;
uint8 surface;
uint8 piece;
void Set(const CVector &min, const CVector &max, uint8 surf, uint8 piece);
CVector GetSize(void) { return max - min; }
2019-05-15 14:52:37 +00:00
};
struct CColLine
{
2020-08-03 10:58:37 +00:00
// NB: this has to be compatible with two CVuVectors
2019-05-15 14:52:37 +00:00
CVector p0;
int pad0;
CVector p1;
int pad1;
CColLine(void) { };
CColLine(const CVector &p0, const CVector &p1) { this->p0 = p0; this->p1 = p1; };
void Set(const CVector &p0, const CVector &p1);
};
struct CColTriangle
{
uint16 a;
uint16 b;
uint16 c;
uint8 surface;
2020-07-27 13:38:12 +00:00
void Set(const CompressedVector *v, int a, int b, int c, uint8 surf, uint8 piece);
2019-05-15 14:52:37 +00:00
};
struct CColTrianglePlane
{
2020-08-03 10:58:37 +00:00
#ifdef VU_COLLISION
CompressedVector normal;
int16 dist;
void Set(const CVector &va, const CVector &vb, const CVector &vc);
void Set(const CompressedVector *v, CColTriangle &tri) { Set(v[tri.a].Get(), v[tri.b].Get(), v[tri.c].Get()); }
void GetNormal(CVector &n) const { n.x = normal.x/4096.0f; n.y = normal.y/4096.0f; n.z = normal.z/4096.0f; }
float CalcPoint(const CVector &v) const { CVector n; GetNormal(n); return DotProduct(n, v) - dist/128.0f; };
#ifdef GTA_PS2
void Unpack(uint128 &qword) const {
__asm__ volatile (
"lh $8, 0(%1)\n"
"lh $9, 2(%1)\n"
"lh $10, 4(%1)\n"
"lh $11, 6(%1)\n"
"pextlw $10, $8\n"
"pextlw $11, $9\n"
"pextlw $2, $11, $10\n"
"sq $2, %0\n"
: "=m" (qword)
: "r" (this)
: "$8", "$9", "$10", "$11", "$2"
);
}
#else
void Unpack(int32 *qword) const {
qword[0] = normal.x;
qword[1] = normal.y;
qword[2] = normal.z;
qword[3] = dist;
}
#endif
#else
2019-05-15 14:52:37 +00:00
CVector normal;
float dist;
uint8 dir;
2020-07-27 13:38:12 +00:00
void Set(const CVector &va, const CVector &vb, const CVector &vc);
void Set(const CompressedVector *v, CColTriangle &tri) { Set(v[tri.a].Get(), v[tri.b].Get(), v[tri.c].Get()); }
2019-05-15 14:52:37 +00:00
void GetNormal(CVector &n) const { n = normal; }
float CalcPoint(const CVector &v) const { return DotProduct(normal, v) - dist; };
2020-08-03 10:58:37 +00:00
#endif
2019-05-15 14:52:37 +00:00
};
struct CColPoint
{
CVector point;
int pad1;
// the surface normal on the surface of point
CVector normal;
int pad2;
uint8 surfaceA;
uint8 pieceA;
uint8 surfaceB;
uint8 pieceB;
float depth;
2020-08-03 10:58:37 +00:00
void Set(float depth, uint8 surfA, uint8 pieceA, uint8 surfB, uint8 pieceB) {
this->depth = depth;
this->surfaceA = surfA;
this->pieceA = pieceA;
this->surfaceB = surfB;
this->pieceB = pieceB;
}
void Set(uint8 surfA, uint8 pieceA, uint8 surfB, uint8 pieceB) {
this->surfaceA = surfA;
this->pieceA = pieceA;
this->surfaceB = surfB;
this->pieceB = pieceB;
}
2019-05-15 14:52:37 +00:00
};
struct CStoredCollPoly
{
2020-08-03 10:58:37 +00:00
#ifdef VU_COLLISION
CVuVector verts[3];
#else
2019-05-15 14:52:37 +00:00
CVector verts[3];
2020-08-03 10:58:37 +00:00
#endif
2019-05-15 14:52:37 +00:00
bool valid;
};
struct CColModel
{
CColSphere boundingSphere;
CColBox boundingBox;
int16 numSpheres;
int16 numLines;
int16 numBoxes;
int16 numTriangles;
int32 level;
2020-07-27 13:38:12 +00:00
bool ownsCollisionVolumes; // missing on PS2
2019-05-15 14:52:37 +00:00
CColSphere *spheres;
CColLine *lines;
CColBox *boxes;
2020-07-27 13:38:12 +00:00
CompressedVector *vertices;
2019-05-15 14:52:37 +00:00
CColTriangle *triangles;
CColTrianglePlane *trianglePlanes;
CColModel(void);
~CColModel(void);
void RemoveCollisionVolumes(void);
void CalculateTrianglePlanes(void);
void RemoveTrianglePlanes(void);
CLink<CColModel*> *GetLinkPtr(void);
void SetLinkPtr(CLink<CColModel*>*);
void GetTrianglePoint(CVector &v, int i) const;
2019-06-24 22:42:23 +00:00
CColModel& operator=(const CColModel& other);
2019-05-15 14:52:37 +00:00
};
class CCollision
{
public:
2020-04-15 16:19:45 +00:00
static eLevelName ms_collisionInMemory;
static CLinkList<CColModel*> ms_colModelCache;
2019-05-15 14:52:37 +00:00
static void Init(void);
2019-06-28 17:23:28 +00:00
static void Shutdown(void);
2019-05-15 14:52:37 +00:00
static void Update(void);
static void LoadCollisionWhenINeedIt(bool changeLevel);
2019-06-24 22:42:23 +00:00
static void SortOutCollisionAfterLoad(void);
2019-06-28 17:23:28 +00:00
static void LoadCollisionScreen(eLevelName level);
2019-05-15 14:52:37 +00:00
static void DrawColModel(const CMatrix &mat, const CColModel &colModel);
static void DrawColModel_Coloured(const CMatrix &mat, const CColModel &colModel, int32 id);
static void CalculateTrianglePlanes(CColModel *model);
// all these return true if there's a collision
static bool TestSphereSphere(const CColSphere &s1, const CColSphere &s2);
static bool TestSphereBox(const CColSphere &sph, const CColBox &box);
static bool TestLineBox(const CColLine &line, const CColBox &box);
static bool TestVerticalLineBox(const CColLine &line, const CColBox &box);
2020-07-27 13:38:12 +00:00
static bool TestLineTriangle(const CColLine &line, const CompressedVector *verts, const CColTriangle &tri, const CColTrianglePlane &plane);
2019-05-15 14:52:37 +00:00
static bool TestLineSphere(const CColLine &line, const CColSphere &sph);
2020-07-27 13:38:12 +00:00
static bool TestSphereTriangle(const CColSphere &sphere, const CompressedVector *verts, const CColTriangle &tri, const CColTrianglePlane &plane);
2019-05-28 06:39:36 +00:00
static bool TestLineOfSight(const CColLine &line, const CMatrix &matrix, CColModel &model, bool ignoreSeeThrough);
2019-05-15 14:52:37 +00:00
static bool ProcessSphereSphere(const CColSphere &s1, const CColSphere &s2, CColPoint &point, float &mindistsq);
static bool ProcessSphereBox(const CColSphere &sph, const CColBox &box, CColPoint &point, float &mindistsq);
static bool ProcessLineBox(const CColLine &line, const CColBox &box, CColPoint &point, float &mindist);
2020-07-27 13:38:12 +00:00
static bool ProcessVerticalLineTriangle(const CColLine &line, const CompressedVector *verts, const CColTriangle &tri, const CColTrianglePlane &plane, CColPoint &point, float &mindist, CStoredCollPoly *poly);
static bool ProcessLineTriangle(const CColLine &line , const CompressedVector *verts, const CColTriangle &tri, const CColTrianglePlane &plane, CColPoint &point, float &mindist);
2019-05-15 14:52:37 +00:00
static bool ProcessLineSphere(const CColLine &line, const CColSphere &sphere, CColPoint &point, float &mindist);
2020-07-27 13:38:12 +00:00
static bool ProcessSphereTriangle(const CColSphere &sph, const CompressedVector *verts, const CColTriangle &tri, const CColTrianglePlane &plane, CColPoint &point, float &mindistsq);
2019-05-28 06:39:36 +00:00
static bool ProcessLineOfSight(const CColLine &line, const CMatrix &matrix, CColModel &model, CColPoint &point, float &mindist, bool ignoreSeeThrough);
static bool ProcessVerticalLine(const CColLine &line, const CMatrix &matrix, CColModel &model, CColPoint &point, float &mindist, bool ignoreSeeThrough, CStoredCollPoly *poly);
2019-07-09 07:57:44 +00:00
static int32 ProcessColModels(const CMatrix &matrixA, CColModel &modelA, const CMatrix &matrixB, CColModel &modelB, CColPoint *spherepoints, CColPoint *linepoints, float *linedists);
2019-09-14 17:58:09 +00:00
static bool IsStoredPolyStillValidVerticalLine(const CVector &pos, float z, CColPoint &point, CStoredCollPoly *poly);
2019-05-15 14:52:37 +00:00
static float DistToLine(const CVector *l0, const CVector *l1, const CVector *point);
static float DistToLine(const CVector *l0, const CVector *l1, const CVector *point, CVector &closest);
};