simplify vector3 code a bit for C client

This commit is contained in:
UnknownShadow200 2018-03-11 15:59:55 +11:00
parent e54475e09f
commit c92b53bc79
7 changed files with 41 additions and 47 deletions

View File

@ -97,7 +97,7 @@ void Block_SetDrawType(BlockID block, UInt8 draw) {
Block_Draw[block] = draw; Block_Draw[block] = draw;
Block_RecalcIsLiquid(block); Block_RecalcIsLiquid(block);
Vector3 zero = Vector3_Zero, one = Vector3_One; Vector3 zero = Vector3_Zero; Vector3 one = Vector3_One;
Block_FullOpaque[block] = draw == DRAW_OPAQUE Block_FullOpaque[block] = draw == DRAW_OPAQUE
&& Vector3_Equals(&Block_MinBB[block], &zero) && Vector3_Equals(&Block_MinBB[block], &zero)
&& Vector3_Equals(&Block_MaxBB[block], &one); && Vector3_Equals(&Block_MaxBB[block], &one);
@ -160,9 +160,9 @@ void Block_ResetProps(BlockID block) {
if (Block_Draw[block] == DRAW_SPRITE) { if (Block_Draw[block] == DRAW_SPRITE) {
Block_MinBB[block] = Vector3_Create3(2.50f / 16.0f, 0.0f, 2.50f / 16.0f); Block_MinBB[block] = Vector3_Create3(2.50f / 16.0f, 0.0f, 2.50f / 16.0f);
Block_MaxBB[block] = Vector3_Create3(13.5f / 16.0f, 1.0f, 13.5f / 16.0f); Block_MaxBB[block] = Vector3_Create3(13.5f / 16.0f, 1.0f, 13.5f / 16.0f);
} else { } else {
Block_MinBB[block] = Vector3_Zero; Vector3 zero = Vector3_Zero; Block_MinBB[block] = zero;
Block_MaxBB[block] = Vector3_One; Vector3 one = Vector3_One; Block_MaxBB[block] = one;
Block_MaxBB[block].Y = DefaultSet_Height(block); Block_MaxBB[block].Y = DefaultSet_Height(block);
} }

View File

@ -134,10 +134,10 @@ void PerspectiveCamera_Init(Camera* cam) {
void FirstPersonCamera_GetView(Matrix* mat) { void FirstPersonCamera_GetView(Matrix* mat) {
Vector3 camPos = Game_CurrentCameraPos; Vector3 camPos = Game_CurrentCameraPos;
Vector3 dir = PerspectiveCamera_GetDirVector(); Vector3 dir = PerspectiveCamera_GetDirVector();
Vector3 targetPos; Vector3 targetPos; Vector3_Add(&targetPos, &camPos, &dir);
Vector3_Add(&targetPos, &camPos, &dir);
Matrix_LookAt(mat, camPos, targetPos, Vector3_UnitY); Vector3 up = Vector3_UnitY;
Matrix_LookAt(mat, camPos, targetPos, up);
Matrix_MulBy(mat, &Camera_TiltM); Matrix_MulBy(mat, &Camera_TiltM);
} }
@ -177,7 +177,8 @@ void ThirdPersonCamera_GetView(Matrix* mat) {
Vector3 targetPos = Entity_GetEyePosition(p); Vector3 targetPos = Entity_GetEyePosition(p);
targetPos.Y += Camera_BobbingVer; targetPos.Y += Camera_BobbingVer;
Matrix_LookAt(mat, cameraPos, targetPos, Vector3_UnitY); Vector3 up = Vector3_UnitY;
Matrix_LookAt(mat, cameraPos, targetPos, up);
Matrix_MulBy(mat, &Camera_TiltM); Matrix_MulBy(mat, &Camera_TiltM);
} }

View File

@ -534,7 +534,7 @@ void ShadowComponent_CalcAlpha(Real32 playerY, ShadowData* data) {
bool ShadowComponent_GetBlocks(Entity* entity, Vector3I* coords, Real32 x, Real32 z, Int32 posY, ShadowData* data) { bool ShadowComponent_GetBlocks(Entity* entity, Vector3I* coords, Real32 x, Real32 z, Int32 posY, ShadowData* data) {
Int32 blockX = Math_Floor(x), blockZ = Math_Floor(z); Int32 blockX = Math_Floor(x), blockZ = Math_Floor(z);
Vector3I p = Vector3I_Create3(blockX, 0, blockZ); Vector3I p = VECTOR3I_CONST(blockX, 0, blockZ);
/* Check we have not processed this particular block already */ /* Check we have not processed this particular block already */
UInt32 i, posCount = 0; UInt32 i, posCount = 0;

View File

@ -1136,8 +1136,8 @@ void BlockModel_RecalcProperties(Entity* p) {
Real32 height; Real32 height;
if (Block_Draw[block] == DRAW_GAS) { if (Block_Draw[block] == DRAW_GAS) {
BlockModel_minBB = Vector3_Zero; Vector3 zero = Vector3_Zero; BlockModel_minBB = zero;
BlockModel_maxBB = Vector3_One; Vector3 one = Vector3_One; BlockModel_maxBB = one;
height = 1.0f; height = 1.0f;
} else { } else {
BlockModel_minBB = Block_MinBB[block]; BlockModel_minBB = Block_MinBB[block];

View File

@ -66,7 +66,7 @@ BlockID Particle_GetBlock(Int32 x, Int32 y, Int32 z) {
bool Particle_TestY(Particle* p, Int32 y, bool topFace, bool throughLiquids) { bool Particle_TestY(Particle* p, Int32 y, bool topFace, bool throughLiquids) {
if (y < 0) { if (y < 0) {
p->NextPos.Y = ENTITY_ADJUSTMENT; p->LastPos.Y = ENTITY_ADJUSTMENT; p->NextPos.Y = ENTITY_ADJUSTMENT; p->LastPos.Y = ENTITY_ADJUSTMENT;
p->Velocity = Vector3_Zero; Vector3 zero = Vector3_Zero; p->Velocity = zero;
p->HitTerrain = true; p->HitTerrain = true;
return false; return false;
} }
@ -82,7 +82,7 @@ bool Particle_TestY(Particle* p, Int32 y, bool topFace, bool throughLiquids) {
Real32 adjust = topFace ? ENTITY_ADJUSTMENT : -ENTITY_ADJUSTMENT; Real32 adjust = topFace ? ENTITY_ADJUSTMENT : -ENTITY_ADJUSTMENT;
p->LastPos.Y = collideY + adjust; p->LastPos.Y = collideY + adjust;
p->NextPos.Y = p->LastPos.Y; p->NextPos.Y = p->LastPos.Y;
p->Velocity = Vector3_Zero; Vector3 zero = Vector3_Zero; p->Velocity = zero;
p->HitTerrain = true; p->HitTerrain = true;
return false; return false;
} }

View File

@ -5,6 +5,7 @@
#include "Player.h" #include "Player.h"
#include "World.h" #include "World.h"
#include "Funcs.h" #include "Funcs.h"
#include "Platform.h"
Real32 PickedPos_dist; Real32 PickedPos_dist;
void PickedPos_TestAxis(PickedPos* pos, Real32 dAxis, Face fAxis) { void PickedPos_TestAxis(PickedPos* pos, Real32 dAxis, Face fAxis) {
@ -16,11 +17,12 @@ void PickedPos_TestAxis(PickedPos* pos, Real32 dAxis, Face fAxis) {
} }
void PickedPos_SetAsValid(PickedPos* pos, RayTracer* t, Vector3 intersect) { void PickedPos_SetAsValid(PickedPos* pos, RayTracer* t, Vector3 intersect) {
Vector3I blockPos = VECTOR3I_CONST(t->X, t->Y, t->Z);
pos->Valid = true;
pos->BlockPos = blockPos;
pos->Block = t->Block;
pos->Intersect = intersect;
pos->Min = t->Min; pos->Max = t->Max; pos->Min = t->Min; pos->Max = t->Max;
pos->BlockPos = Vector3I_Create3(t->X, t->Y, t->Z);
pos->Valid = true;
pos->Block = t->Block;
pos->Intersect = intersect;
PickedPos_dist = MATH_LARGENUM; PickedPos_dist = MATH_LARGENUM;
PickedPos_TestAxis(pos, intersect.X - t->Min.X, FACE_XMIN); PickedPos_TestAxis(pos, intersect.X - t->Min.X, FACE_XMIN);
@ -30,22 +32,24 @@ void PickedPos_SetAsValid(PickedPos* pos, RayTracer* t, Vector3 intersect) {
PickedPos_TestAxis(pos, intersect.Z - t->Min.Z, FACE_ZMIN); PickedPos_TestAxis(pos, intersect.Z - t->Min.Z, FACE_ZMIN);
PickedPos_TestAxis(pos, intersect.Z - t->Max.Z, FACE_ZMAX); PickedPos_TestAxis(pos, intersect.Z - t->Max.Z, FACE_ZMAX);
Vector3I offsets[FACE_COUNT]; switch (pos->ClosestFace) {
offsets[FACE_XMIN] = Vector3I_Create3(-1, 0, 0); case FACE_XMIN: blockPos.X--; break;
offsets[FACE_XMAX] = Vector3I_Create3(+1, 0, 0); case FACE_XMAX: blockPos.X++; break;
offsets[FACE_ZMIN] = Vector3I_Create3(0, 0, -1); case FACE_ZMIN: blockPos.Z--; break;
offsets[FACE_ZMAX] = Vector3I_Create3(0, 0, +1); case FACE_ZMAX: blockPos.Z++; break;
offsets[FACE_YMIN] = Vector3I_Create3(0, -1, 0); case FACE_YMIN: blockPos.Y--; break;
offsets[FACE_YMAX] = Vector3I_Create3(0, +1, 0); case FACE_YMAX: blockPos.Y++; break;
Vector3I_Add(&pos->TranslatedPos, &pos->BlockPos, &offsets[pos->ClosestFace]); }
pos->TranslatedPos = blockPos;
} }
void PickedPos_SetAsInvalid(PickedPos* pos) { void PickedPos_SetAsInvalid(PickedPos* pos) {
pos->Valid = false; Vector3I blockPos = VECTOR3I_CONST(-1, -1, -1);
pos->BlockPos = Vector3I_MinusOne; pos->Valid = false;
pos->TranslatedPos = Vector3I_MinusOne; pos->BlockPos = blockPos;
pos->ClosestFace = (Face)FACE_COUNT; pos->Block = BLOCK_AIR;
pos->Block = BLOCK_AIR; pos->ClosestFace = (Face)FACE_COUNT;
pos->TranslatedPos = blockPos;
} }
Real32 RayTracer_Div(Real32 a, Real32 b) { Real32 RayTracer_Div(Real32 a, Real32 b) {

View File

@ -21,23 +21,12 @@ Real32 Vector3_Length(Vector3* v);
Real32 Vector3_LengthSquared(Vector3* v); Real32 Vector3_LengthSquared(Vector3* v);
#define VECTOR3_CONST(x, y, z) { x, y, z }; #define VECTOR3_CONST(x, y, z) { x, y, z };
#define Vector2_UnitX Vector2_Create2(1.0f, 0.0f) #define VECTOR3I_CONST(x, y, z) { x, y, z };
#define Vector2_UnitY Vector2_Create2(0.0f, 1.0f) #define Vector3_UnitX VECTOR3_CONST(1.0f, 0.0f, 0.0f)
#define Vector2_Zero Vector2_Create2(0.0f, 0.0f) #define Vector3_UnitY VECTOR3_CONST(0.0f, 1.0f, 0.0f)
#define Vector2_One Vector2_Create2(1.0f, 1.0f) #define Vector3_UnitZ VECTOR3_CONST(0.0f, 0.0f, 1.0f)
#define Vector3_Zero VECTOR3_CONST(0.0f, 0.0f, 0.0f)
#define Vector3_UnitX Vector3_Create3(1.0f, 0.0f, 0.0f) #define Vector3_One VECTOR3_CONST(1.0f, 1.0f, 1.0f)
#define Vector3_UnitY Vector3_Create3(0.0f, 1.0f, 0.0f)
#define Vector3_UnitZ Vector3_Create3(0.0f, 0.0f, 1.0f)
#define Vector3_Zero Vector3_Create3(0.0f, 0.0f, 0.0f)
#define Vector3_One Vector3_Create3(1.0f, 1.0f, 1.0f)
#define Vector3I_UnitX Vector3I_Create3(1, 0, 0)
#define Vector3I_UnitY Vector3I_Create3(0, 1, 0)
#define Vector3I_UnitZ Vector3I_Create3(0, 0, 1)
#define Vector3I_Zero Vector3I_Create3(0, 0, 0)
#define Vector3I_One Vector3I_Create3(1, 1, 1)
#define Vector3I_MinusOne Vector3I_Create3(-1, -1, -1)
void Vector3_Add(Vector3* result, Vector3* a, Vector3* b); void Vector3_Add(Vector3* result, Vector3* a, Vector3* b);
void Vector3I_Add(Vector3I* result, Vector3I* a, Vector3I* b); void Vector3I_Add(Vector3I* result, Vector3I* a, Vector3I* b);