Licar/racing.h

83 lines
2.1 KiB
C
Raw Normal View History

2023-09-16 22:52:03 +02:00
/**
racing module: implements the racing physics and logic.
*/
#ifndef _LCR_RACING_H
#define _LCR_RACING_H
2024-09-04 23:26:05 +02:00
typedef int32_t LCR_GameUnit; ///< game spatial units
2023-09-16 22:52:03 +02:00
2024-09-04 23:26:05 +02:00
#define LCR_GAME_UNIT 1024 ///< length of map square in game units
#define LCR_PHYSICS_UNIT 512 ///< length of map square for physics engine
2023-09-16 22:52:03 +02:00
#include "map.h"
#include "tinyphysicsengine.h"
2024-09-04 23:26:05 +02:00
struct
{
TPE_World physicsWorld;
TPE_Body carBody;
} LCR_racing;
TPE_Vec3 LCR_racingEnvironmentFunction(TPE_Vec3 point, TPE_Unit maxDist)
{
/*
return
TPE_envHalfPlane(point,
TPE_vec3(0,0,0),
TPE_vec3(0,TPE_FRACTIONS_PER_UNIT,0));
*/
return TPE_envAABoxInside(point,TPE_vec3(0,0,0),TPE_vec3(
LCR_PHYSICS_UNIT * LCR_MAP_SIZE_BLOCKS,
(LCR_PHYSICS_UNIT * LCR_MAP_SIZE_BLOCKS) / 2,
LCR_PHYSICS_UNIT * LCR_MAP_SIZE_BLOCKS));
}
void LCR_racingInit(void)
{
LCR_log("initializing racing engine");
TPE_worldInit(&(LCR_racing.physicsWorld),
&(LCR_racing.carBody),1,LCR_racingEnvironmentFunction);
}
void _LCR_drawPhysicsDebugPixel(uint16_t x, uint16_t y, uint8_t color)
{
if (x > 1 && x < LCR_EFFECTIVE_RESOLUTION_X - 1 &&
y > 1 && y < LCR_EFFECTIVE_RESOLUTION_Y - 1)
{
uint16_t color = color;
color = (color << 3) | 0xf800;
for (int j = -1; j < 2; ++j)
for (int i = -1; i < 2; ++i)
LCR_drawPixelXYUnsafe(x + i,y + j,color);
}
}
void LCR_physicsDebugDraw(LCR_GameUnit camPos[3], LCR_GameUnit camRot[2],
LCR_GameUnit camFov)
{
TPE_Vec3 cPos, cRot, cView;
cPos.x = (camPos[0] * LCR_PHYSICS_UNIT) / LCR_GAME_UNIT;
cPos.y = (camPos[1] * LCR_PHYSICS_UNIT) / LCR_GAME_UNIT;
cPos.z = (camPos[2] * LCR_PHYSICS_UNIT) / LCR_GAME_UNIT;
cRot.x = (camRot[0] * TPE_FRACTIONS_PER_UNIT) / LCR_GAME_UNIT;
cRot.y = (camRot[1] * TPE_FRACTIONS_PER_UNIT) / LCR_GAME_UNIT;
cRot.z = 0;
cView.x = LCR_EFFECTIVE_RESOLUTION_X;
cView.y = LCR_EFFECTIVE_RESOLUTION_Y;
cView.z = (camFov * TPE_FRACTIONS_PER_UNIT) / LCR_GAME_UNIT;
TPE_worldDebugDraw(&(LCR_racing.physicsWorld),_LCR_drawPhysicsDebugPixel,
cPos,cRot,cView,16,2 * LCR_PHYSICS_UNIT);
}
2023-09-16 22:52:03 +02:00
#endif // guard