Add Vec3 helpers, Vec3 project and reflect fns
This commit is contained in:
parent
0aa8d2b09a
commit
a25788a51b
3 changed files with 126 additions and 0 deletions
|
@ -669,3 +669,29 @@ SC_SACD_AABB_Box SC_SACD_AABB_Combine(SC_SACD_AABB_Box a, SC_SACD_AABB_Box b) {
|
|||
temp_b,
|
||||
temp_c};
|
||||
}
|
||||
|
||||
SC_SACD_Vec3 SC_SACD_Vec3_Sum(SC_SACD_Vec3 a, SC_SACD_Vec3 b) { return a + b; }
|
||||
|
||||
SC_SACD_Vec3 SC_SACD_Vec3_Difference(SC_SACD_Vec3 a, SC_SACD_Vec3 b) {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
SC_SACD_Vec3 SC_SACD_Vec3_Mult(SC_SACD_Vec3 vec, float scalar) {
|
||||
return vec * scalar;
|
||||
}
|
||||
|
||||
SC_SACD_Vec3 SC_SACD_Vec3_Div(SC_SACD_Vec3 vec, float scalar) {
|
||||
return vec / scalar;
|
||||
}
|
||||
|
||||
SC_SACD_Vec3 SC_SACD_Vec3_Project(SC_SACD_Vec3 vec, SC_SACD_Vec3 target) {
|
||||
float upper_dot_product = SC_SACD_Dot_Product(vec, target);
|
||||
float lower_dot_product = SC_SACD_Dot_Product(target, target);
|
||||
|
||||
return target * (upper_dot_product / lower_dot_product);
|
||||
}
|
||||
|
||||
SC_SACD_Vec3 SC_SACD_Vec3_Reflect(SC_SACD_Vec3 vec, SC_SACD_Vec3 target) {
|
||||
SC_SACD_Vec3 proj = SC_SACD_Vec3_Project(vec, target);
|
||||
return proj * 2.0F - vec;
|
||||
}
|
||||
|
|
|
@ -136,6 +136,20 @@ SC_SACD_Generic_Box_To_AABB(SC_SACD_Generic_Box s);
|
|||
SC_SACD_EXPORT SC_SACD_AABB_Box SC_SACD_AABB_Combine(SC_SACD_AABB_Box a,
|
||||
SC_SACD_AABB_Box b);
|
||||
|
||||
SC_SACD_EXPORT SC_SACD_Vec3 SC_SACD_Vec3_Sum(SC_SACD_Vec3 a, SC_SACD_Vec3 b);
|
||||
SC_SACD_EXPORT SC_SACD_Vec3 SC_SACD_Vec3_Difference(SC_SACD_Vec3 a,
|
||||
SC_SACD_Vec3 b);
|
||||
SC_SACD_EXPORT SC_SACD_Vec3 SC_SACD_Vec3_Mult(SC_SACD_Vec3 vec, float scalar);
|
||||
SC_SACD_EXPORT SC_SACD_Vec3 SC_SACD_Vec3_Div(SC_SACD_Vec3 vec, float scalar);
|
||||
|
||||
/// Projects "vec" onto "target".
|
||||
SC_SACD_EXPORT SC_SACD_Vec3 SC_SACD_Vec3_Project(SC_SACD_Vec3 vec,
|
||||
SC_SACD_Vec3 target);
|
||||
|
||||
/// Reflects "vec" about "target".
|
||||
SC_SACD_EXPORT SC_SACD_Vec3 SC_SACD_Vec3_Reflect(SC_SACD_Vec3 vec,
|
||||
SC_SACD_Vec3 target);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
86
src/test.cpp
86
src/test.cpp
|
@ -754,6 +754,92 @@ int main() {
|
|||
2.0F);
|
||||
}
|
||||
|
||||
// Test Sum/Diff/Mult/Div Vec3.
|
||||
{
|
||||
SC_SACD_Vec3 a{1.0F, 2.0F, 3.0F};
|
||||
SC_SACD_Vec3 b{10.0F, 100.0F, 1000.0F};
|
||||
|
||||
SC_SACD_Vec3 vec_result = SC_SACD_Vec3_Sum(a, b);
|
||||
CHECK_FLOAT(vec_result.x, 11.0F);
|
||||
CHECK_FLOAT(vec_result.y, 102.0F);
|
||||
CHECK_FLOAT(vec_result.z, 1003.0F);
|
||||
|
||||
vec_result = SC_SACD_Vec3_Difference(a, b);
|
||||
CHECK_FLOAT(vec_result.x, -9.0F);
|
||||
CHECK_FLOAT(vec_result.y, -98.0F);
|
||||
CHECK_FLOAT(vec_result.z, -997.0F);
|
||||
|
||||
vec_result = SC_SACD_Vec3_Mult(a, 2.0F);
|
||||
CHECK_FLOAT(vec_result.x, 2.0F);
|
||||
CHECK_FLOAT(vec_result.y, 4.0F);
|
||||
CHECK_FLOAT(vec_result.z, 6.0F);
|
||||
|
||||
vec_result = SC_SACD_Vec3_Div(a, 2.0F);
|
||||
CHECK_FLOAT(vec_result.x, 0.5F);
|
||||
CHECK_FLOAT(vec_result.y, 1.0F);
|
||||
CHECK_FLOAT(vec_result.z, 1.5F);
|
||||
}
|
||||
|
||||
// Test project.
|
||||
{
|
||||
SC_SACD_Vec3 a{2.0F, 3.0F, 4.0F};
|
||||
SC_SACD_Vec3 target{0.0F, 1.0F, 0.0F};
|
||||
|
||||
SC_SACD_Vec3 result = SC_SACD_Vec3_Project(a, target);
|
||||
CHECK_FLOAT(result.x, 0.0F);
|
||||
CHECK_FLOAT(result.y, 3.0F);
|
||||
CHECK_FLOAT(result.z, 0.0F);
|
||||
|
||||
target.x = 2.0F;
|
||||
target.y = 0.0F;
|
||||
result = SC_SACD_Vec3_Project(a, target);
|
||||
CHECK_FLOAT(result.x, 2.0F);
|
||||
CHECK_FLOAT(result.y, 0.0F);
|
||||
CHECK_FLOAT(result.z, 0.0F);
|
||||
|
||||
target.x = 0.0F;
|
||||
target.z = 9.0F;
|
||||
result = SC_SACD_Vec3_Project(a, target);
|
||||
CHECK_FLOAT(result.x, 0.0F);
|
||||
CHECK_FLOAT(result.y, 0.0F);
|
||||
CHECK_FLOAT(result.z, 4.0F);
|
||||
}
|
||||
|
||||
// Test reflect.
|
||||
{
|
||||
SC_SACD_Vec3 a{2.0F, 3.0F, 4.0F};
|
||||
SC_SACD_Vec3 target{0.0F, 3.0F, 0.0F};
|
||||
|
||||
SC_SACD_Vec3 result = SC_SACD_Vec3_Reflect(a, target);
|
||||
CHECK_FLOAT(result.x, -2.0F);
|
||||
CHECK_FLOAT(result.y, 3.0F);
|
||||
CHECK_FLOAT(result.z, -4.0F);
|
||||
|
||||
target.x = 9.0F;
|
||||
target.y = 0.0F;
|
||||
|
||||
result = SC_SACD_Vec3_Reflect(a, target);
|
||||
CHECK_FLOAT(result.x, 2.0F);
|
||||
CHECK_FLOAT(result.y, -3.0F);
|
||||
CHECK_FLOAT(result.z, -4.0F);
|
||||
|
||||
target.x = 0.0F;
|
||||
target.z = 110.0F;
|
||||
|
||||
result = SC_SACD_Vec3_Reflect(a, target);
|
||||
CHECK_FLOAT(result.x, -2.0F);
|
||||
CHECK_FLOAT(result.y, -3.0F);
|
||||
CHECK_FLOAT(result.z, 4.0F);
|
||||
|
||||
target.y = 1.0F;
|
||||
target.z = 1.0F;
|
||||
|
||||
result = SC_SACD_Vec3_Reflect(a, target);
|
||||
CHECK_FLOAT(result.x, -2.0F);
|
||||
CHECK_FLOAT(result.y, 4.0F);
|
||||
CHECK_FLOAT(result.z, 3.0F);
|
||||
}
|
||||
|
||||
std::cout << "Checks checked: " << checks_checked << '\n'
|
||||
<< "Checks passed: " << checks_passed << '\n';
|
||||
|
||||
|
|
Loading…
Reference in a new issue