From fa6632483337a0df11b9ab2edbfa9847faab73b9 Mon Sep 17 00:00:00 2001 From: Miloslav Ciz Date: Thu, 26 Sep 2024 14:56:39 +0200 Subject: [PATCH] Continue block collisions --- assets.h | 8 ++++++++ map.h | 50 ++++++++++++++++++++++++++++++++++++++++++++------ racing.h | 53 ++++++++++++++++++++++++++++++++++------------------- renderer.h | 25 ++++++++++++++----------- 4 files changed, 100 insertions(+), 36 deletions(-) diff --git a/assets.h b/assets.h index 8e9e46b..9161d24 100644 --- a/assets.h +++ b/assets.h @@ -35,6 +35,14 @@ LCR_MAP_BLOCK(LCR_BLOCK_FULL,33,0,33,LCR_BLOCK_MATERIAL_CONCRETE,0), LCR_MAP_BLOCK(LCR_BLOCK_FULL,33,0,34,LCR_BLOCK_MATERIAL_CONCRETE,0), LCR_MAP_BLOCK(LCR_BLOCK_FULL,33,0,35,LCR_BLOCK_MATERIAL_CONCRETE,0), LCR_MAP_BLOCK(LCR_BLOCK_FULL,33,0,36,LCR_BLOCK_MATERIAL_CONCRETE,0), +LCR_MAP_BLOCK(LCR_BLOCK_FULL,33,1,36,LCR_BLOCK_MATERIAL_CONCRETE,0), + +LCR_MAP_BLOCK(LCR_BLOCK_FULL,34,0,33,LCR_BLOCK_MATERIAL_CONCRETE,0), +LCR_MAP_BLOCK(LCR_BLOCK_FULL,34,0,34,LCR_BLOCK_MATERIAL_CONCRETE,0), +LCR_MAP_BLOCK(LCR_BLOCK_FULL,34,0,35,LCR_BLOCK_MATERIAL_CONCRETE,0), +LCR_MAP_BLOCK(LCR_BLOCK_FULL,34,0,36,LCR_BLOCK_MATERIAL_CONCRETE,0), + +LCR_MAP_BLOCK(LCR_BLOCK_RAMP,32,32,32,LCR_BLOCK_MATERIAL_CONCRETE,LCR_BLOCK_TRANSFORM_ROT_180), /* LCR_MAP_BLOCK(LCR_BLOCK_FULL_ACCEL,0,0,0,LCR_BLOCK_MATERIAL_CONCRETE,0), diff --git a/map.h b/map.h index accab5c..c6dec9e 100644 --- a/map.h +++ b/map.h @@ -148,6 +148,20 @@ void LCR_mapBlockGetCoords(const uint8_t block[LCR_BLOCK_SIZE], *z = (block[2] >> 4) | ((block[3] & 0x03) << 4); } +uint8_t LCR_mapBlockOppositeTransform(uint8_t transform) +{ + if (!(transform & LCR_BLOCK_TRANSFORM_FLIP_H)) + { + if ((transform & 0x60) == LCR_BLOCK_TRANSFORM_ROT_90) + return ((transform & (~0x60)) | LCR_BLOCK_TRANSFORM_ROT_270); + + if ((transform & 0x60) == LCR_BLOCK_TRANSFORM_ROT_270) + return ((transform & (~0x60)) | LCR_BLOCK_TRANSFORM_ROT_90); + } + + return transform; +} + uint8_t LCR_mapBlockGetTransform(const uint8_t block[LCR_BLOCK_SIZE]) { return block[3] & 0xf0; @@ -171,6 +185,15 @@ uint32_t LCR_mapBlockCoordsToCoordNumber(uint8_t x, uint8_t y, uint8_t z) return LCR_mapBlockGetCoordNumber(b); } +int LCR_rampHeight4ths(uint8_t rampType) +{ + return + (rampType == LCR_BLOCK_RAMP_14) + + (rampType == LCR_BLOCK_RAMP) * 4 + + (rampType == LCR_BLOCK_RAMP_12 || rampType == LCR_BLOCK_RAMP_34) * 2 + + (rampType == LCR_BLOCK_RAMP_34); +} + uint8_t *LCR_getMapBlockAtCoordNumber(uint32_t coord) { // binary search the block: @@ -424,6 +447,21 @@ void _LCR_addBlockShapeByte(uint8_t *bytes, uint8_t *byteCount, *byteCount += 1; } + + +#define LCR_TRANSFORM_COORDS(trans,cx,cy,cz,maxXZ,maxY)\ + if (trans & LCR_BLOCK_TRANSFORM_FLIP_H) cx = maxXZ - cx;\ + if (trans & 0x20) { /* for both 90 and 270 */ \ + cx ^= cz; cz ^= cx; cx ^= cz; /* swap */ \ + cx = maxXZ - cx; } \ + if (trans & 0x40) { /* for both 180 and 270 */ \ + cx = maxXZ - cx; \ + cz = maxXZ - cz; } \ + if (trans & LCR_BLOCK_TRANSFORM_FLIP_V) \ + cy = maxY - cy; + + + /** Gets a shape of given map block type as a 3D model composed of triangles. The model is returned as an array of byte triplets (triangles), with each byte @@ -521,11 +559,7 @@ void LCR_mapGetBlockShape(uint8_t blockType, uint8_t transform, case LCR_BLOCK_RAMP_14: case LCR_BLOCK_RAMP_34: { - uint8_t top = - (blockType == LCR_BLOCK_RAMP_14) + - (blockType == LCR_BLOCK_RAMP) * 4 + - (blockType == LCR_BLOCK_RAMP_12 || blockType == LCR_BLOCK_RAMP_34) * 2 + - (blockType == LCR_BLOCK_RAMP_34); + uint8_t top = LCR_rampHeight4ths(blockType); ADD(0,0,0) ADD(0,top,6) ADD(0,0,6) // side ADD(6,0,0) ADD(6,0,6) ADD(6,top,6) // side @@ -549,7 +583,10 @@ void LCR_mapGetBlockShape(uint8_t blockType, uint8_t transform, uint8_t x, y, z, tmp; _LCR_decodeMapBlockCoords(bytes[i],&x,&y,&z); - + +LCR_TRANSFORM_COORDS(transform,x,y,z,6,4) + +/* if (transform & LCR_BLOCK_TRANSFORM_FLIP_H) x = 6 - x; @@ -568,6 +605,7 @@ void LCR_mapGetBlockShape(uint8_t blockType, uint8_t transform, if (transform & LCR_BLOCK_TRANSFORM_FLIP_V) y = 4 - y; +*/ bytes[i] = _LCR_encodeMapBlockCoords(x,y,z); } diff --git a/racing.h b/racing.h index 95bd32d..81b225d 100644 --- a/racing.h +++ b/racing.h @@ -61,18 +61,22 @@ TPE_Vec3 _LCR_racingBlockEnvFunc(TPE_Vec3 point, const uint8_t *block) { uint8_t bx, by, bz; LCR_mapBlockGetCoords(block,&bx,&by,&bz); + + TPE_Vec3 blockOffset = TPE_vec3( + (((int) bx) - LCR_MAP_SIZE_BLOCKS / 2) * LCR_PHYSICS_UNIT, + (((int) by) - LCR_MAP_SIZE_BLOCKS / 2) * (LCR_PHYSICS_UNIT / 2), + (((int) bz) - LCR_MAP_SIZE_BLOCKS / 2) * LCR_PHYSICS_UNIT); - TPE_Vec3 center = TPE_vec3( - (((int) bx) - LCR_MAP_SIZE_BLOCKS / 2) * LCR_PHYSICS_UNIT - + LCR_PHYSICS_UNIT / 2, - (((int) by) - LCR_MAP_SIZE_BLOCKS / 2) * (LCR_PHYSICS_UNIT / 2) - + LCR_PHYSICS_UNIT / 4, - (((int) bz) - LCR_MAP_SIZE_BLOCKS / 2) * LCR_PHYSICS_UNIT - + LCR_PHYSICS_UNIT / 2); + point = TPE_vec3Minus(point,blockOffset); // shift to origin - point = TPE_vec3Minus(point,center); // shift to origin + uint8_t transform = + LCR_mapBlockOppositeTransform(LCR_mapBlockGetTransform(block)); -// TODO: transform + LCR_TRANSFORM_COORDS(transform,point.x,point.y,point.z,(LCR_PHYSICS_UNIT / 2), + (LCR_PHYSICS_UNIT / 4)) + + point = TPE_vec3Minus(point,TPE_vec3( + LCR_PHYSICS_UNIT / 2,LCR_PHYSICS_UNIT / 4,LCR_PHYSICS_UNIT / 2)); switch (block[0]) { @@ -121,9 +125,15 @@ TPE_Vec3 _LCR_racingBlockEnvFunc(TPE_Vec3 point, const uint8_t *block) case LCR_BLOCK_RAMP_14: { TPE_Unit sides[6]; - sides[0] = -1 * LCR_PHYSICS_UNIT / 2; sides[1] = -1 * LCR_PHYSICS_UNIT / 2; - sides[2] = LCR_PHYSICS_UNIT / 2; sides[3] = -1 * LCR_PHYSICS_UNIT / 2; - sides[4] = LCR_PHYSICS_UNIT / 2; sides[5] = LCR_PHYSICS_UNIT / 2; + sides[0] = -1 * LCR_PHYSICS_UNIT / 2; + sides[1] = -1 * LCR_PHYSICS_UNIT / 4; + + sides[2] = LCR_PHYSICS_UNIT / 2; + sides[3] = -1 * LCR_PHYSICS_UNIT / 4; + + sides[4] = LCR_PHYSICS_UNIT / 2; + sides[5] = -1 * LCR_PHYSICS_UNIT / 4 + + LCR_rampHeight4ths(block[0]) * (LCR_PHYSICS_UNIT / 8); point = TPE_envAATriPrism(point,TPE_vec3(0,0,0),sides,LCR_PHYSICS_UNIT,2); break; @@ -134,16 +144,17 @@ TPE_Vec3 _LCR_racingBlockEnvFunc(TPE_Vec3 point, const uint8_t *block) break; } -// TODO: untransform + point = TPE_vec3Plus(point, + TPE_vec3(LCR_PHYSICS_UNIT / 2,LCR_PHYSICS_UNIT / 4,LCR_PHYSICS_UNIT / 2)); - point = TPE_vec3Plus(point,center); // shift back + transform = LCR_mapBlockOppositeTransform(transform); + + LCR_TRANSFORM_COORDS(transform,point.x,point.y,point.z,LCR_PHYSICS_UNIT, + (LCR_PHYSICS_UNIT / 2)) + + point = TPE_vec3Plus(point,blockOffset); // shift back return point; - -/* - return TPE_envAABox(point,center,TPE_vec3(LCR_PHYSICS_UNIT / 2, - LCR_PHYSICS_UNIT / 4,LCR_PHYSICS_UNIT / 2)); -*/ } TPE_Vec3 _LCR_racingEnvironmentFunction(TPE_Vec3 point, TPE_Unit maxDist) @@ -154,6 +165,10 @@ TPE_Vec3 _LCR_racingEnvironmentFunction(TPE_Vec3 point, TPE_Unit maxDist) (LCR_PHYSICS_UNIT * LCR_MAP_SIZE_BLOCKS) / 2, LCR_PHYSICS_UNIT * LCR_MAP_SIZE_BLOCKS)),point) + // without this check we might try to get block outside the map + if (_pBest.x == point.x && _pBest.y == point.y && _pBest.z == point.z) + return _pBest; + if (maxDist <= LCR_PHYSICS_UNIT / 4) // considering half of square height { /* Here we only check the 8 closest blocks => relatively fast. */ diff --git a/renderer.h b/renderer.h index d984a90..a1bd69c 100644 --- a/renderer.h +++ b/renderer.h @@ -16,7 +16,8 @@ /// Renderer specific unit, length of one map square. #define LCR_RENDERER_UNIT (S3L_F / 2) - // ^ just S3L_F leaves some tris bugging + // NOTE: ^ S3L_F sometimes makes some triangles bug, S3L_F/2 seems to fix it + // but it's more jerky, maybe try to apply anti-overflow in S3L? #define LCR_RENDERER_CHUNK_RESOLUTION 4 // do not change #define LCR_RENDERER_LOD_BLOCKS 64 // do not change @@ -30,6 +31,10 @@ #define LCR_RENDERER_MODEL_COUNT 10 #define LCR_RENDERER_CAR_SCALE (LCR_RENDERER_UNIT / 4) +/** For some reason the map model is a bit misaligned with physics world, this + kinda hotfixes it -- later try to discover source of this bug. TODO */ +#define _LCR_MAP_MODEL_SCALE 1034 + struct { S3L_Scene scene; @@ -615,15 +620,12 @@ uint8_t _LCR_buildMapModel(void) S3L_model3DInit(LCR_renderer.mapVerts,0,LCR_renderer.mapTris,0, &LCR_renderer.mapModel); -// TEMPORARY FIX: this scales the map so that it better aligns with the -// physics world -- dunno why it's not aligned by default, investigate (if it's -// numerical errors then at least make this a const) -LCR_renderer.mapModel.transform.scale.x = - (S3L_F * 1009) / 1000; -LCR_renderer.mapModel.transform.scale.y = - LCR_renderer.mapModel.transform.scale.x; -LCR_renderer.mapModel.transform.scale.z = - LCR_renderer.mapModel.transform.scale.x; + LCR_renderer.mapModel.transform.scale.x = + (_LCR_MAP_MODEL_SCALE * S3L_F) / 1024; + LCR_renderer.mapModel.transform.scale.y + = LCR_renderer.mapModel.transform.scale.x; + LCR_renderer.mapModel.transform.scale.z + = LCR_renderer.mapModel.transform.scale.x; for (int j = 0; j < LCR_currentMap.blockCount; ++j) { @@ -724,7 +726,8 @@ LCR_renderer.mapModel.transform.scale.z = } } - _LCR_rendererAddMapTri(triIndices[0],triIndices[1],triIndices[2],triData); + _LCR_rendererAddMapTri(triIndices[0],triIndices[1],triIndices[2], + triData); } vi = 0;