Add FramePerfection's Better Wall Collision with rounded corners

This commit is contained in:
Arceveti committed 2021-09-17 19:26:13 -07:00
1 parent 97f6fa6959
commit 2927db262c
16 files changed
+369 -220

No files matched your search

+208 -95
View File
@@ -5,6 +5,8 @@
#include "game/level_update.h"
#include "game/mario.h"
#include "game/object_list_processor.h"
// #include "game/rendering_graph_node.h"
#include "math_util.h"
#include "surface_collision.h"
#include "surface_load.h"
#include "game/puppyprint.h"
@@ -17,19 +19,33 @@
* Iterate through the list of walls until all walls are checked and
* have given their wall push.
*/
static s32 find_wall_collisions_from_list(struct SurfaceNode *surfaceNode,
struct WallCollisionData *data) {
static s32 find_wall_collisions_from_list(struct SurfaceNode *surfaceNode, struct WallCollisionData *data) {
const f32 corner_threshold = -0.9f;
register struct Surface *surf;
register f32 offset;
register f32 radius = data->radius;
register f32 x = data->x;
register f32 y = data->y + data->offsetY;
register f32 z = data->z;
register f32 px, pz;
register f32 w1, w2, w3;
register f32 y1, y2, y3;
register f32 v0x, v0y, v0z;
register f32 v1x, v1y, v1z;
register f32 v2x, v2y, v2z;
register f32 d00, d01, d11, d20, d21;
register f32 invDenom;
register f32 v, w;
register f32 margin_radius = (radius - 1.0f);
s32 numCols = 0;
// #if EXTENDED_BOUNDS_MODE
// const float down_scale = (1.0f / gWorldScale);
// radius *= down_scale;
// x *= down_scale;
// y *= down_scale;
// z *= down_scale;
// margin_radius *= down_scale;
// #endif
// Max collision radius = 200
if (radius > 200.0f) {
radius = 200.0f;
@@ -45,70 +61,12 @@ static s32 find_wall_collisions_from_list(struct SurfaceNode *surfaceNode,
continue;
}
offset = surf->normal.x * x + surf->normal.y * y + surf->normal.z * z + surf->originOffset;
offset = (surf->normal.x * x) + (surf->normal.y * y) + (surf->normal.z * z) + surf->originOffset;
if (offset < -radius || offset > radius) {
continue;
}
px = x;
pz = z;
//! (Quantum Tunneling) Due to issues with the vertices walls choose and
// the fact they are floating point, certain floating point positions
// along the seam of two walls may collide with neither wall or both walls.
if (surf->flags & SURFACE_FLAG_X_PROJECTION) {
w1 = -surf->vertex1[2]; w2 = -surf->vertex2[2]; w3 = -surf->vertex3[2];
y1 = surf->vertex1[1]; y2 = surf->vertex2[1]; y3 = surf->vertex3[1];
if (surf->normal.x > 0.0f) {
if ((y1 - y) * (w2 - w1) - (w1 - -pz) * (y2 - y1) > 0.0f) {
continue;
}
if ((y2 - y) * (w3 - w2) - (w2 - -pz) * (y3 - y2) > 0.0f) {
continue;
}
if ((y3 - y) * (w1 - w3) - (w3 - -pz) * (y1 - y3) > 0.0f) {
continue;
}
} else {
if ((y1 - y) * (w2 - w1) - (w1 - -pz) * (y2 - y1) < 0.0f) {
continue;
}
if ((y2 - y) * (w3 - w2) - (w2 - -pz) * (y3 - y2) < 0.0f) {
continue;
}
if ((y3 - y) * (w1 - w3) - (w3 - -pz) * (y1 - y3) < 0.0f) {
continue;
}
}
} else {
w1 = surf->vertex1[0]; w2 = surf->vertex2[0]; w3 = surf->vertex3[0];
y1 = surf->vertex1[1]; y2 = surf->vertex2[1]; y3 = surf->vertex3[1];
if (surf->normal.z > 0.0f) {
if ((y1 - y) * (w2 - w1) - (w1 - px) * (y2 - y1) > 0.0f) {
continue;
}
if ((y2 - y) * (w3 - w2) - (w2 - px) * (y3 - y2) > 0.0f) {
continue;
}
if ((y3 - y) * (w1 - w3) - (w3 - px) * (y1 - y3) > 0.0f) {
continue;
}
} else {
if ((y1 - y) * (w2 - w1) - (w1 - px) * (y2 - y1) < 0.0f) {
continue;
}
if ((y2 - y) * (w3 - w2) - (w2 - px) * (y3 - y2) < 0.0f) {
continue;
}
if ((y3 - y) * (w1 - w3) - (w3 - px) * (y1 - y3) < 0.0f) {
continue;
}
}
}
// Determine if checking for the camera or not.
if (gCheckingSurfaceCollisionsForCamera) {
if (surf->flags & SURFACE_FLAG_NO_CAM_COLLISION || surf->type == SURFACE_NEW_WATER || surf->type == SURFACE_NEW_WATER_BOTTOM) {
@@ -136,21 +94,140 @@ static s32 find_wall_collisions_from_list(struct SurfaceNode *surfaceNode,
}
}
//! (Wall Overlaps) Because this doesn't update the x and z local variables,
// multiple walls can push mario more than is required.
data->x += surf->normal.x * (radius - offset);
data->z += surf->normal.z * (radius - offset);
v0x = (f32)(surf->vertex2[0] - surf->vertex1[0]);
v0y = (f32)(surf->vertex2[1] - surf->vertex1[1]);
v0z = (f32)(surf->vertex2[2] - surf->vertex1[2]);
v1x = (f32)(surf->vertex3[0] - surf->vertex1[0]);
v1y = (f32)(surf->vertex3[1] - surf->vertex1[1]);
v1z = (f32)(surf->vertex3[2] - surf->vertex1[2]);
v2x = x - (f32)surf->vertex1[0];
v2y = y - (f32)surf->vertex1[1];
v2z = z - (f32)surf->vertex1[2];
// Face
d00 = (v0x * v0x) + (v0y * v0y) + (v0z * v0z);
d01 = (v0x * v1x) + (v0y * v1y) + (v0z * v1z);
d11 = (v1x * v1x) + (v1y * v1y) + (v1z * v1z);
d20 = (v2x * v0x) + (v2y * v0y) + (v2z * v0z);
d21 = (v2x * v1x) + (v2y * v1y) + (v2z * v1z);
invDenom = 1.0f / ((d00 * d11) - (d01 * d01));
v = ((d11 * d20) - (d01 * d21)) * invDenom;
if (v < 0.0f || v > 1.0f) {
goto edge_1_2;
}
w = (d00 * d21 - d01 * d20) * invDenom;
if (w < 0.0f || w > 1.0f || v + w > 1.0f) {
goto edge_1_2;
}
x += surf->normal.x * (radius - offset);
z += surf->normal.z * (radius - offset);
goto hasCollision;
edge_1_2:
if (offset < 0) continue;
// Edge 1-2
if (v0y != 0.0f) {
v = (v2y / v0y);
if (v < 0.0f || v > 1.0f) {
goto edge_1_3;
}
d00 = v0x * v - v2x;
d01 = v0z * v - v2z;
invDenom = sqrtf(sqr(d00) + sqr(d01));
offset = invDenom - margin_radius;
if (offset > 0.0f) {
goto edge_1_3;
}
invDenom = offset / invDenom;
x += (d00 *= invDenom);
z += (d01 *= invDenom);
margin_radius += 0.01f;
if ((d00 * surf->normal.x) + (d01 * surf->normal.z) < (corner_threshold * offset)) {
continue;
} else {
goto hasCollision;
}
}
edge_1_3:
// Edge 1-3
if (v1y != 0.0f) {
v = (v2y / v1y);
if (v < 0.0f || v > 1.0f) {
goto edge_2_3;
}
d00 = v1x * v - v2x;
d01 = v1z * v - v2z;
invDenom = sqrtf(sqr(d00) + sqr(d01));
offset = invDenom - margin_radius;
if (offset > 0.0f) {
goto edge_2_3;
}
invDenom = offset / invDenom;
x += (d00 *= invDenom);
z += (d01 *= invDenom);
margin_radius += 0.01f;
if ((d00 * surf->normal.x) + (d01 * surf->normal.z) < (corner_threshold * offset)) {
continue;
} else {
goto hasCollision;
}
}
edge_2_3:
// Edge 2-3
v1x = (f32)(surf->vertex3[0] - surf->vertex2[0]);
v1y = (f32)(surf->vertex3[1] - surf->vertex2[1]);
v1z = (f32)(surf->vertex3[2] - surf->vertex2[2]);
v2x = x - (f32)surf->vertex2[0];
v2y = y - (f32)surf->vertex2[1];
v2z = z - (f32)surf->vertex2[2];
if (v1y != 0.0f) {
v = (v2y / v1y);
if (v < 0.0f || v > 1.0f) continue;
d00 = v1x * v - v2x;
d01 = v1z * v - v2z;
invDenom = sqrtf(sqr(d00) + sqr(d01));
offset = invDenom - margin_radius;
if (offset > 0.0f) continue;
invDenom = offset / invDenom;
x += (d00 *= invDenom);
z += (d01 *= invDenom);
margin_radius += 0.01f;
if ((d00 * surf->normal.x) + (d01 * surf->normal.z) < (corner_threshold * offset)) {
continue;
} else {
goto hasCollision;
}
} else {
continue;
}
hasCollision:
//! (Unreferenced Walls) Since this only returns the first four walls,
// this can lead to wall interaction being missed. Typically unreferenced walls
// come from only using one wall, however.
if (data->numWalls < 4) {
if (data->numWalls < MAX_REFEREMCED_WALLS) {
data->walls[data->numWalls++] = surf;
}
numCols++;
}
// #if EXTENDED_BOUNDS_MODE
// x *= gWorldScale;
// y *= gWorldScale;
// z *= gWorldScale;
// #endif
data->x = x;
data->z = z;
return numCols;
}
@@ -224,17 +301,44 @@ s32 find_wall_collisions(struct WallCollisionData *colData) {
return numCollisions;
}
/**
* Collides with walls and returns the most recent wall.
*/
void resolve_and_return_wall_collisions(Vec3f pos, f32 offset, f32 radius, struct WallCollisionData *collisionData) {
collisionData->x = pos[0];
collisionData->y = pos[1];
collisionData->z = pos[2];
collisionData->radius = radius;
collisionData->offsetY = offset;
find_wall_collisions(collisionData);
pos[0] = collisionData->x;
pos[1] = collisionData->y;
pos[2] = collisionData->z;
}
/**************************************************
* CEILINGS *
**************************************************/
void add_ceil_margin(s32 *x, s32 *z, Vec3s target1, Vec3s target2, f32 margin) {
register f32 diff_x, diff_z, invDenom;
diff_x = target1[0] - *x + target2[0] - *x;
diff_z = target1[2] - *z + target2[2] - *z;
invDenom = margin / sqrtf(sqr(diff_x) + sqr(diff_z));
*x += diff_x * invDenom;
*z += diff_z * invDenom;
}
/**
* Iterate through the list of ceilings and find the first ceiling over a given point.
*/
static struct Surface *find_ceil_from_list(struct SurfaceNode *surfaceNode, s32 x, s32 y, s32 z, f32 *pheight) {
register struct Surface *surf;
register s32 x1, z1, x2, z2, x3, z3;
s32 x1, z1, x2, z2, x3, z3;
f32 nx, ny, nz, oo, height;
const f32 margin = 1.5f;
struct Surface *ceil = NULL;
*pheight = CELL_HEIGHT_LIMIT;
// Stay in this loop until out of ceilings.
@@ -245,8 +349,14 @@ static struct Surface *find_ceil_from_list(struct SurfaceNode *surfaceNode, s32
continue;
x1 = surf->vertex1[0];
z1 = surf->vertex1[2];
if (surf->type != SURFACE_HANGABLE) {
add_ceil_margin(&x1, &z1, surf->vertex2, surf->vertex3, margin);
}
z2 = surf->vertex2[2];
x2 = surf->vertex2[0];
if (surf->type != SURFACE_HANGABLE) {
add_ceil_margin(&x2, &z2, surf->vertex3, surf->vertex1, margin);
}
// Checking if point is in bounds of the triangle laterally.
if ((z1 - z) * (x2 - x1) - (x1 - x) * (z2 - z1) > 0) {
continue;
@@ -254,6 +364,9 @@ static struct Surface *find_ceil_from_list(struct SurfaceNode *surfaceNode, s32
// Slight optimization by checking these later.
x3 = surf->vertex3[0];
z3 = surf->vertex3[2];
if (surf->type != SURFACE_HANGABLE) {
add_ceil_margin(&x3, &z3, surf->vertex1, surf->vertex2, margin);
}
if ((z2 - z) * (x3 - x2) - (x2 - x) * (z3 - z2) > 0) {
continue;
}
@@ -270,31 +383,31 @@ static struct Surface *find_ceil_from_list(struct SurfaceNode *surfaceNode, s32
else if (surf->type == SURFACE_CAMERA_BOUNDARY || surf->type == SURFACE_NEW_WATER || surf->type == SURFACE_NEW_WATER_BOTTOM) {
continue;
}
nx = surf->normal.x;
ny = surf->normal.y;
nz = surf->normal.z;
oo = surf->originOffset;
// If a wall, ignore it. Likely a remnant, should never occur.
if (ny == 0.0f) {
continue;
}
// Find the ceil height at the specific point.
height = -(x * nx + nz * z + oo) / ny;
if (height > *pheight) {
continue;
}
// Checks for ceiling interaction
if (y > height) {
continue;
}
if (y >= surf->upperY) {
continue;
}
*pheight = height;
ceil = surf;
if (height == y) {
break;
}
nx = surf->normal.x;
ny = surf->normal.y;
nz = surf->normal.z;
oo = surf->originOffset;
// If a wall, ignore it. Likely a remnant, should never occur.
if (ny == 0.0f) {
continue;
}
// Find the ceil height at the specific point.
height = -(x * nx + nz * z + oo) / ny;
if (height > *pheight) {
continue;
}
// Checks for ceiling interaction
if (y > height) {
continue;
}
if (y >= surf->upperY) {
continue;
}
*pheight = height;
ceil = surf;
if (height == y) {
break;
}
}
return ceil;
}
+2 -2
View File
@@ -7,7 +7,6 @@
#include "engine/extended_bounds.h"
#define CELL_HEIGHT_LIMIT 20000
#define FLOOR_LOWER_LIMIT -11000
#define FLOOR_LOWER_LIMIT_MISC (FLOOR_LOWER_LIMIT + 1000)
@@ -22,11 +21,12 @@ struct WallCollisionData
/*0x10*/ f32 radius;
/*0x14*/ s16 unused;
/*0x16*/ s16 numWalls;
/*0x18*/ struct Surface *walls[4];
/*0x18*/ struct Surface *walls[MAX_REFEREMCED_WALLS];
};
s32 f32_find_wall_collision(f32 *xPtr, f32 *yPtr, f32 *zPtr, f32 offsetY, f32 radius);
s32 find_wall_collisions(struct WallCollisionData *colData);
void resolve_and_return_wall_collisions(Vec3f pos, f32 offset, f32 radius, struct WallCollisionData *collisionData);
f32 find_ceil(f32 posX, f32 posY, f32 posZ, struct Surface **pceil);
f32 find_floor_height(f32 x, f32 y, f32 z);
f32 find_floor(f32 xPos, f32 yPos, f32 zPos, struct Surface **pfloor);
-4
View File
@@ -130,10 +130,6 @@ static void add_surface_to_cell(s32 dynamic, s32 cellX, s32 cellZ, struct Surfac
} else {
listIndex = SPATIAL_PARTITION_WALLS;
sortDir = 0; // insertion order
if (surface->normal.x < -0.707 || surface->normal.x > 0.707) {
surface->flags |= SURFACE_FLAG_X_PROJECTION;
}
}
//! (Surface Cucking) Surfaces are sorted by the height of their first