Skip to content

Commit

Permalink
inline vector stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Jan 6, 2019
1 parent 91c9df5 commit 401429b
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 98 deletions.
17 changes: 10 additions & 7 deletions src/Block.c
Expand Up @@ -201,13 +201,16 @@ void Block_SetCollide(BlockID block, CollideType collide) {
}

void Block_SetDrawType(BlockID block, DrawType draw) {
Vector3 zero = Vector3_Zero();
Vector3 one = Vector3_One();

if (draw == DRAW_OPAQUE && Blocks.Collide[block] != COLLIDE_SOLID) draw = DRAW_TRANSPARENT;
Blocks.Draw[block] = draw;
Block_RecalcIsLiquid(block);

Blocks.FullOpaque[block] = draw == DRAW_OPAQUE
&& Vector3_Equals(&Blocks.MinBB[block], &Vector3_Zero)
&& Vector3_Equals(&Blocks.MaxBB[block], &Vector3_One);
&& Vector3_Equals(&Blocks.MinBB[block], &zero)
&& Vector3_Equals(&Blocks.MaxBB[block], &one);
}


Expand Down Expand Up @@ -250,11 +253,11 @@ void Block_ResetProps(BlockID block) {

Blocks.Draw[block] = DefaultSet_Draw(block);
if (Blocks.Draw[block] == DRAW_SPRITE) {
Blocks.MinBB[block] = Vector3_Create3(2.50f / 16.0f, 0.0f, 2.50f / 16.0f);
Blocks.MaxBB[block] = Vector3_Create3(13.5f / 16.0f, 1.0f, 13.5f / 16.0f);
Blocks.MinBB[block] = Vector3_Create3(2.50f/16.0f, 0.0f, 2.50f/16.0f);
Blocks.MaxBB[block] = Vector3_Create3(13.5f/16.0f, 1.0f, 13.5f/16.0f);
} else {
Blocks.MinBB[block] = Vector3_Zero;
Blocks.MaxBB[block] = Vector3_One;
Blocks.MinBB[block] = Vector3_Zero();
Blocks.MaxBB[block] = Vector3_One();
Blocks.MaxBB[block].Y = DefaultSet_Height(block);
}

Expand Down Expand Up @@ -414,7 +417,7 @@ void Block_RecalculateBB(BlockID block) {
TextureLoc texLoc = Block_GetTex(block, FACE_XMAX);
int x = Atlas2D_TileX(texLoc), y = Atlas2D_TileY(texLoc);

static Vector3 centre = { 0.5f, 0.0f, 0.5f };
Vector3 centre = { 0.5f, 0.0f, 0.5f };
float minX = 0, minY = 0, maxX = 1, maxY = 1;
Vector3 minRaw, maxRaw;

Expand Down
12 changes: 6 additions & 6 deletions src/Entity.c
Expand Up @@ -812,7 +812,7 @@ static void LocalPlayer_HandleInput(float* xMoving, float* zMoving) {
hacks->FlyingDown = KeyBind_IsPressed(KEYBIND_FLY_DOWN);

if (hacks->WOMStyleHacks && hacks->Enabled && hacks->CanNoclip) {
if (hacks->Noclip) p->Base.Velocity = Vector3_Zero;
if (hacks->Noclip) p->Base.Velocity = Vector3_Zero();
hacks->Noclip = KeyBind_IsPressed(KEYBIND_NOCLIP);
}
}
Expand Down Expand Up @@ -842,7 +842,7 @@ static void LocalPlayer_Tick(struct Entity* e, double delta) {

/* Immediate stop in noclip mode */
if (!hacks->NoclipSlide && (hacks->Noclip && xMoving == 0 && zMoving == 0)) {
e->Velocity = Vector3_Zero;
e->Velocity = Vector3_Zero();
}

PhysicsComp_UpdateVelocityState(&p->Physics);
Expand Down Expand Up @@ -912,16 +912,16 @@ static void LocalPlayer_Init(void) {
static void LocalPlayer_Reset(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
p->ReachDistance = 5.0f;
p->Base.Velocity = Vector3_Zero;
p->Base.Velocity = Vector3_Zero();
p->Physics.JumpVel = 0.42f;
p->Physics.ServerJumpVel = 0.42f;
/* p->Base.Health = 20; TODO: survival mode stuff */
}

static void LocalPlayer_OnNewMap(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
p->Base.Velocity = Vector3_Zero;
p->OldVelocity = Vector3_Zero;
p->Base.Velocity = Vector3_Zero();
p->OldVelocity = Vector3_Zero();

p->_WarnedRespawn = false;
p->_WarnedFly = false;
Expand Down Expand Up @@ -961,7 +961,7 @@ static void LocalPlayer_DoRespawn(void) {
spawn.Y += 2.0f/16.0f;
LocationUpdate_MakePosAndOri(&update, spawn, p->SpawnRotY, p->SpawnHeadX, false);
p->Base.VTABLE->SetLocation(&p->Base, &update, false);
p->Base.Velocity = Vector3_Zero;
p->Base.Velocity = Vector3_Zero();

/* Update onGround, otherwise if 'respawn' then 'space' is pressed, you still jump into the air if onGround was true before */
Entity_GetBounds(&p->Base, &bb);
Expand Down
9 changes: 5 additions & 4 deletions src/EntityComponents.c
Expand Up @@ -893,9 +893,10 @@ static void Collisions_CollideWithReachableBlocks(struct CollisionsComp* comp, i
void Collisions_MoveAndWallSlide(struct CollisionsComp* comp) {
struct Entity* e = comp->Entity;
struct AABB entityBB, entityExtentBB;
Vector3 zero = Vector3_Zero();
int count;

if (Vector3_Equals(&e->Velocity, &Vector3_Zero)) return;
if (Vector3_Equals(&e->Velocity, &zero)) return;
count = Searcher_FindReachableBlocks(e, &entityBB, &entityExtentBB);
Collisions_CollideWithReachableBlocks(comp, count, &entityBB, &entityExtentBB);
}
Expand Down Expand Up @@ -1148,13 +1149,13 @@ void PhysicsComp_PhysicsTick(struct PhysicsComp* comp, Vector3 vel) {
}

if (Entity_TouchesAnyWater(entity) && !hacks->Floating) {
Vector3 waterDrag = VECTOR3_CONST(0.8f, 0.8f, 0.8f);
Vector3 waterDrag = { 0.8f, 0.8f, 0.8f };
PhysicsComp_MoveNormal(comp, vel, 0.02f * horSpeed, waterDrag, LIQUID_GRAVITY, verSpeed);
} else if (Entity_TouchesAnyLava(entity) && !hacks->Floating) {
Vector3 lavaDrag = VECTOR3_CONST(0.5f, 0.5f, 0.5f);
Vector3 lavaDrag = { 0.5f, 0.5f, 0.5f };
PhysicsComp_MoveNormal(comp, vel, 0.02f * horSpeed, lavaDrag, LIQUID_GRAVITY, verSpeed);
} else if (Entity_TouchesAnyRope(entity) && !hacks->Floating) {
Vector3 ropeDrag = VECTOR3_CONST(0.5f, 0.85f, 0.5f);
Vector3 ropeDrag = { 0.5f, 0.85f, 0.5f };
PhysicsComp_MoveNormal(comp, vel, 0.02f * 1.7f, ropeDrag, ROPE_GRAVITY, verSpeed);
} else {
factor = hacks->Floating || entity->OnGround ? 0.1f : 0.02f;
Expand Down
2 changes: 1 addition & 1 deletion src/EnvRenderer.c
Expand Up @@ -308,7 +308,7 @@ void EnvRenderer_RenderSkybox(double deltaTime) {

/* Rotate around camera */
pos = Camera_CurrentPos;
Camera_CurrentPos = Vector3_Zero;
Camera_CurrentPos = Vector3_Zero();
Camera_Active->GetView(&view); Matrix_MulBy(&m, &view);
Camera_CurrentPos = pos;

Expand Down
4 changes: 2 additions & 2 deletions src/Model.c
Expand Up @@ -1474,8 +1474,8 @@ static void BlockModel_RecalcProperties(struct Entity* p) {
float height;

if (Blocks.Draw[block] == DRAW_GAS) {
bModel_minBB = Vector3_Zero;
bModel_maxBB = Vector3_One;
bModel_minBB = Vector3_Zero();
bModel_maxBB = Vector3_One();
height = 1.0f;
} else {
bModel_minBB = Blocks.MinBB[block];
Expand Down
4 changes: 2 additions & 2 deletions src/Particle.c
Expand Up @@ -83,7 +83,7 @@ static bool Particle_TestY(struct Particle* p, int y, bool topFace, bool through

if (y < 0) {
p->NextPos.Y = ENTITY_ADJUSTMENT; p->LastPos.Y = ENTITY_ADJUSTMENT;
p->Velocity = Vector3_Zero;
p->Velocity = Vector3_Zero();
particle_hitTerrain = true;
return false;
}
Expand All @@ -99,7 +99,7 @@ static bool Particle_TestY(struct Particle* p, int y, bool topFace, bool through
float adjust = topFace ? ENTITY_ADJUSTMENT : -ENTITY_ADJUSTMENT;
p->LastPos.Y = collideY + adjust;
p->NextPos.Y = p->LastPos.Y;
p->Velocity = Vector3_Zero;
p->Velocity = Vector3_Zero();
particle_hitTerrain = true;
return false;
}
Expand Down
56 changes: 0 additions & 56 deletions src/Vectors.c
Expand Up @@ -3,53 +3,6 @@
#include "Funcs.h"
#include "Constants.h"

const Vector3 Vector3_Zero = { 0.0f, 0.0f, 0.0f };
const Vector3 Vector3_One = { 1.0f, 1.0f, 1.0f };

Vector3 Vector3_Create1(float value) {
Vector3 v; v.X = value; v.Y = value; v.Z = value; return v;
}

Vector3 Vector3_Create3(float x, float y, float z) {
Vector3 v; v.X = x; v.Y = y; v.Z = z; return v;
}

Vector3I Vector3I_MaxValue(void) {
Vector3I v = { Int32_MaxValue, Int32_MaxValue, Int32_MaxValue }; return v;
}

Vector3 Vector3_BigPos(void) {
Vector3 v = { 1e25f, 1e25f, 1e25f }; return v;
}

float Vector3_LengthSquared(const Vector3* v) {
return v->X * v->X + v->Y * v->Y + v->Z * v->Z;
}

void Vector3_Add(Vector3* result, Vector3* a, Vector3* b) {
result->X = a->X + b->X; result->Y = a->Y + b->Y; result->Z = a->Z + b->Z;
}

void Vector3_Add1(Vector3* result, Vector3* a, float b) {
result->X = a->X + b; result->Y = a->Y + b; result->Z = a->Z + b;
}

void Vector3_Sub(Vector3* result, Vector3* a, Vector3* b) {
result->X = a->X - b->X; result->Y = a->Y - b->Y; result->Z = a->Z - b->Z;
}

void Vector3_Mul1(Vector3* result, Vector3* a, float scale) {
result->X = a->X * scale; result->Y = a->Y * scale; result->Z = a->Z * scale;
}

void Vector3_Mul3(Vector3* result, Vector3* a, Vector3* scale) {
result->X = a->X * scale->X; result->Y = a->Y * scale->Y; result->Z = a->Z * scale->Z;
}

void Vector3_Negate(Vector3* result, Vector3* a) {
result->X = -a->X; result->Y = -a->Y; result->Z = -a->Z;
}

void Vector3_Lerp(Vector3* result, Vector3* a, Vector3* b, float blend) {
result->X = blend * (b->X - a->X) + a->X;
result->Y = blend * (b->Y - a->Y) + a->Y;
Expand Down Expand Up @@ -103,15 +56,6 @@ Vector3 Vector3_RotateZ(Vector3 v, float angle) {
}


#define Vec3_EQ(a, b) a->X == b->X && a->Y == b->Y && a->Z == b->Z
#define Vec3_NE(a, b) a->X != b->X || a->Y != b->Y || a->Z != b->Z

bool Vector3_Equals(const Vector3* a, const Vector3* b) { return Vec3_EQ(a, b); }
bool Vector3_NotEquals(const Vector3* a, const Vector3* b) { return Vec3_NE(a, b); }
bool Vector3I_Equals(const Vector3I* a, const Vector3I* b) { return Vec3_EQ(a, b); }
bool Vector3I_NotEquals(const Vector3I* a, const Vector3I* b) { return Vec3_NE(a, b); }


void Vector3I_Floor(Vector3I* result, Vector3* a) {
result->X = Math_Floor(a->X); result->Y = Math_Floor(a->Y); result->Z = Math_Floor(a->Z);
}
Expand Down
101 changes: 81 additions & 20 deletions src/Vectors.h
@@ -1,55 +1,116 @@
#ifndef CC_VECTORS_H
#define CC_VECTORS_H
#include "Core.h"
/* Represents 2, 3 dimensional vectors, and 4 x 4 matrix.
#include "Constants.h"
/* Represents 2 and 3 component vectors, and 4 x 4 matrix.
Frustum culling sourced from http://www.crownandcutlass.com/features/technicaldetails/frustum.html
Copyright 2017 ClassicalSharp | Licensed under BSD-3
*/

/* 2 component vector (2D vector) */
typedef struct Vector2_ { float X, Y; } Vector2;
/* 3 component vector (3D vector) */
typedef struct Vector3_ { float X, Y, Z; } Vector3;
/* 3 component vector (3D integer vector) */
typedef struct Vector3I_ { int X, Y, Z; } Vector3I;
/* 4 component vector */
struct Vector4 { float X, Y, Z, W; };
/* 4x4 matrix. (for vertex transformations) */
struct Matrix { struct Vector4 Row0, Row1, Row2, Row3; };
extern const struct Matrix Matrix_Identity;

Vector3 Vector3_Create1(float value);
Vector3 Vector3_Create3(float x, float y, float z);
Vector3I Vector3I_MaxValue(void);
Vector3 Vector3_BigPos(void);
float Vector3_LengthSquared(const Vector3* v);

/* Identity matrix. (A * Identity = A) */
extern const struct Matrix Matrix_Identity;
#define VECTOR3_CONST(x, y, z) { x, y, z };
extern const Vector3 Vector3_Zero;
extern const Vector3 Vector3_One;

void Vector3_Add(Vector3* result, Vector3* a, Vector3* b);
void Vector3_Add1(Vector3* result, Vector3* a, float b);
void Vector3_Sub(Vector3* result, Vector3* a, Vector3* b);
void Vector3_Mul1(Vector3* result, Vector3* a, float scale);
void Vector3_Mul3(Vector3* result, Vector3* a, Vector3* scale);
void Vector3_Negate(Vector3* result, Vector3* a);
/* Returns a vector with all components 0. */
static CC_INLINE Vector3 Vector3_Zero(void) {
Vector3 v = { 0, 0, 0 }; return v;
}
/* Returns a vector with all components 1. */
static CC_INLINE Vector3 Vector3_One(void) {
Vector3 v = { 1, 1, 1}; return v;
}
/* Returns a vector with all components set to Int32_MaxValue. */
static CC_INLINE Vector3I Vector3I_MaxValue(void) {
Vector3I v = { Int32_MaxValue, Int32_MaxValue, Int32_MaxValue }; return v;
}
static CC_INLINE Vector3 Vector3_BigPos(void) {
Vector3 v = { 1e25f, 1e25f, 1e25f }; return v;
}

static CC_INLINE Vector3 Vector3_Create1(float value) {
Vector3 v; v.X = value; v.Y = value; v.Z = value; return v;
}
static CC_INLINE Vector3 Vector3_Create3(float x, float y, float z) {
Vector3 v; v.X = x; v.Y = y; v.Z = z; return v;
}
/* Returns the squared length of the vector. */
/* Squared length can be used for comparison, to avoid a costly sqrt() */
/* However, you must sqrt() this when adding lengths. */
static CC_INLINE float Vector3_LengthSquared(const Vector3* v) {
return v->X * v->X + v->Y * v->Y + v->Z * v->Z;
}
/* Adds components of two vectors together. */
static CC_INLINE void Vector3_Add(Vector3* result, const Vector3* a, const Vector3* b) {
result->X = a->X + b->X; result->Y = a->Y + b->Y; result->Z = a->Z + b->Z;
}
/* Adds a value to each component of a vector. */
static CC_INLINE void Vector3_Add1(Vector3* result, const Vector3* a, float b) {
result->X = a->X + b; result->Y = a->Y + b; result->Z = a->Z + b;
}
/* Subtracts components of two vectors from each other. */
static CC_INLINE void Vector3_Sub(Vector3* result, const Vector3* a, const Vector3* b) {
result->X = a->X - b->X; result->Y = a->Y - b->Y; result->Z = a->Z - b->Z;
}
/* Mulitplies each component of a vector by a value. */
static CC_INLINE void Vector3_Mul1(Vector3* result, const Vector3* a, float b) {
result->X = a->X * b; result->Y = a->Y * b; result->Z = a->Z * b;
}
/* Multiplies components of two vectors together. */
static CC_INLINE void Vector3_Mul3(Vector3* result, const Vector3* a, const Vector3* b) {
result->X = a->X * b->X; result->Y = a->Y * b->Y; result->Z = a->Z * b->Z;
}
/* Negats the components of a vector. */
static CC_INLINE void Vector3_Negate(Vector3* result, Vector3* a) {
result->X = -a->X; result->Y = -a->Y; result->Z = -a->Z;
}

#define Vector3_AddBy(dst, value) Vector3_Add(dst, dst, value)
#define Vector3_SubBy(dst, value) Vector3_Sub(dst, dst, value)
#define Vector3_Mul1By(dst, value) Vector3_Mul1(dst, dst, value)
#define Vector3_Mul3By(dst, value) Vector3_Mul3(dst, dst, value)

/* Linearly interpolates components of two vectors. */
void Vector3_Lerp(Vector3* result, Vector3* a, Vector3* b, float blend);
/* Scales all components of a vector to lie in [-1, 1] */
void Vector3_Normalize(Vector3* result, Vector3* a);

/* Transforms a vector by the given matrix. */
void Vector3_Transform(Vector3* result, Vector3* a, struct Matrix* mat);
/* Same as Vector3_Transform, but faster since X and Z are assumed as 0. */
void Vector3_TransformY(Vector3* result, float y, struct Matrix* mat);

Vector3 Vector3_RotateX(Vector3 v, float angle);
Vector3 Vector3_RotateY(Vector3 v, float angle);
Vector3 Vector3_RotateY3(float x, float y, float z, float angle);
Vector3 Vector3_RotateZ(Vector3 v, float angle);

bool Vector3_Equals(const Vector3* a, const Vector3* b);
bool Vector3_NotEquals(const Vector3* a, const Vector3* b);
bool Vector3I_Equals(const Vector3I* a, const Vector3I* b);
bool Vector3I_NotEquals(const Vector3I* a, const Vector3I* b);
/* Whether all of the components of the two vectors are equal. */
static CC_INLINE bool Vector3_Equals(const Vector3* a, const Vector3* b) {
return a->X == b->X && a->Y == b->Y && a->Z == b->Z;
}
/* Whether any of the components of the two vectors differ. */
static CC_INLINE bool Vector3_NotEquals(const Vector3* a, const Vector3* b) {
return a->X != b->X || a->Y != b->Y || a->Z != b->Z;
}
/* Whether all of the components of the two vectors are equal. */
static CC_INLINE bool Vector3I_Equals(const Vector3I* a, const Vector3I* b) {
return a->X == b->X && a->Y == b->Y && a->Z == b->Z;
}
/* Whether any of the components of the two vectors differ. */
static CC_INLINE bool Vector3I_NotEquals(const Vector3I* a, const Vector3I* b) {
return a->X != b->X || a->Y != b->Y || a->Z != b->Z;
}

void Vector3I_Floor(Vector3I* result, Vector3* a);
void Vector3I_ToVector3(Vector3* result, Vector3I* a);
Expand Down

0 comments on commit 401429b

Please sign in to comment.