Add follow camera

This commit is contained in:
Miloslav Ciz 2024-09-06 00:58:32 +02:00
parent 46c8a4eade
commit e3d0b3219f
4 changed files with 164 additions and 27 deletions

View file

@ -5,29 +5,29 @@
#ifndef _LCR_RACING_H
#define _LCR_RACING_H
typedef int32_t LCR_GameUnit; ///< game spatial units
typedef int32_t LCR_GameUnit; ///< abstract game unit
#define LCR_GAME_UNIT 1024 ///< length of map square in game units
#define LCR_GAME_UNIT 1024 ///< length of map square in LCR_GameUnits
#define LCR_RACING_INPUT_FORW 0x01
#define LCR_RACING_INPUT_RIGHT 0x02
#define LCR_RACING_INPUT_BACK 0x04
#define LCR_RACING_INPUT_LEFT 0x08
#define LCR_PHYSICS_UNIT 512 ///< length of map square for physics engine
#define LCR_PHYSICS_UNIT 1024 ///< length of map square for physics engine
#include "map.h"
#include "tinyphysicsengine.h"
#define LCR_CAR_JOINTS 5
#define LCR_CAR_CONNECTIONS 8
#define LCR_CAR_CONNECTIONS 10
struct
{
TPE_World physicsWorld;
TPE_Body carBody;
TPE_Joint carJoints[5];
TPE_Connection carConnections[8];
TPE_Joint carJoints[LCR_CAR_JOINTS];
TPE_Connection carConnections[LCR_CAR_CONNECTIONS];
} LCR_racing;
TPE_Vec3 LCR_racingEnvironmentFunction(TPE_Vec3 point, TPE_Unit maxDist)
@ -43,13 +43,13 @@ void LCR_racingInit(void)
LCR_log("initializing racing engine");
// make the car body:
TPE_makeCenterRect(LCR_racing.carJoints,
TPE_makeCenterRectFull(LCR_racing.carJoints,
LCR_racing.carConnections,
LCR_GAME_UNIT / 3,
(LCR_GAME_UNIT) / 2,
LCR_GAME_UNIT / 8);
LCR_PHYSICS_UNIT / 2,
(LCR_PHYSICS_UNIT * 3) / 4,
LCR_PHYSICS_UNIT / 8);
LCR_racing.carJoints[4].position.y += LCR_PHYSICS_UNIT / 3;
LCR_racing.carJoints[4].position.y += LCR_PHYSICS_UNIT / 8;
LCR_racing.carJoints[4].sizeDivided *= 3;
LCR_racing.carJoints[4].sizeDivided /= 2;
@ -60,6 +60,8 @@ void LCR_racingInit(void)
TPE_worldInit(&(LCR_racing.physicsWorld),
&(LCR_racing.carBody),1,LCR_racingEnvironmentFunction);
TPE_bodyDeactivate(&(LCR_racing.carBody));
}
void LCR_racingGetCarTransform(LCR_GameUnit position[3],
@ -77,6 +79,12 @@ void LCR_racingGetCarTransform(LCR_GameUnit position[3],
position[1] = AVERAGE(y);
position[2] = AVERAGE(z);
#undef AVERAGE
TPE_Vec3 rot = TPE_bodyGetRotation(&(LCR_racing.carBody),0,2,1);
rotation[0] = (rot.x * LCR_GAME_UNIT) / TPE_FRACTIONS_PER_UNIT;
rotation[1] = (rot.y * LCR_GAME_UNIT) / TPE_FRACTIONS_PER_UNIT;
rotation[2] = (rot.z * LCR_GAME_UNIT) / TPE_FRACTIONS_PER_UNIT;
}
void _LCR_drawPhysicsDebugPixel(uint16_t x, uint16_t y, uint8_t color)
@ -100,20 +108,24 @@ void LCR_racingStep(unsigned int input)
if (input)
{
if (input & LCR_RACING_INPUT_FORW)
vel.z = LCR_GAME_UNIT / 32;
vel.z = LCR_PHYSICS_UNIT / 32;
if (input & LCR_RACING_INPUT_BACK)
vel.y = LCR_GAME_UNIT / 32;
vel.y = LCR_PHYSICS_UNIT / 32;
if (input & LCR_RACING_INPUT_RIGHT)
vel.x = LCR_GAME_UNIT / 32;
vel.x = LCR_PHYSICS_UNIT / 32;
if (input & LCR_RACING_INPUT_LEFT)
vel.x = -1 * LCR_GAME_UNIT / 32;
vel.x = -1 * LCR_PHYSICS_UNIT / 32;
TPE_bodyAccelerate(&(LCR_racing.carBody),vel);
}
TPE_bodyApplyGravity(&(LCR_racing.carBody),
TPE_FRACTIONS_PER_UNIT / 32
);
TPE_worldStep(&(LCR_racing.physicsWorld));
}