1
0
Fork 0

vec2 reflect

This commit is contained in:
Pabloader 2025-12-22 07:28:50 +00:00
parent b1f93ce9b1
commit 472037e3ca
3 changed files with 23 additions and 16 deletions

View File

@ -8,9 +8,11 @@ typedef struct vec2_t {
vec2 vec2_add(vec2 a, vec2 b);
vec2 vec2_sub(vec2 a, vec2 b);
float vec2_dot(vec2 a, vec2 b);
vec2 vec2_mul(vec2 x, float f);
vec2 vec2_div(vec2 x, float f);
float vec2_mag(vec2 x);
vec2 vec2_normalize(vec2 x);
vec2 vec2_mul(vec2 v, float f);
vec2 vec2_div(vec2 v, float f);
float vec2_mag(vec2 v);
vec2 vec2_normalize(vec2 v);
vec2 vec2_lerp(vec2 a, vec2 b, float t);
vec2 vec2_reflect(vec2 v, vec2 n);
#endif

View File

@ -19,25 +19,33 @@ float vec2_dot(vec2 a, vec2 b) {
return a.x * b.x + a.y * b.y;
}
vec2 vec2_mul(vec2 x, float f) {
vec2 vec2_mul(vec2 v, float f) {
return (vec2){
.x = x.x * f,
.y = x.y * f,
.x = v.x * f,
.y = v.y * f,
};
}
vec2 vec2_div(vec2 x, float f) {
vec2 vec2_div(vec2 v, float f) {
return (vec2){
.x = x.x / f,
.y = x.y / f,
.x = v.x / f,
.y = v.y / f,
};
}
float vec2_mag(vec2 x) {
return sqrtf(x.x * x.x + x.y * x.y);
float vec2_mag(vec2 v) {
return sqrtf(v.x * v.x + v.y * v.y);
}
vec2 vec2_normalize(vec2 x) {
float len = vec2_mag(x);
return vec2_div(x, len);
}
vec2 vec2_lerp(vec2 a, vec2 b, float t) {
return vec2_add(a, vec2_mul(vec2_sub(b, a), t));
}
vec2 vec2_reflect(vec2 x, vec2 n) {
return vec2_sub(x, vec2_mul(vec2_mul(n, vec2_dot(x, n)), 2));
}

View File

@ -224,10 +224,7 @@ void rigid_body_handle_collision(rigid_body_index idx1, rigid_body_index idx2) {
vec2 n_overlap = vec2_mul(n, overlap);
rb2->pos = vec2_add(rb2->pos, n_overlap);
float vn = vec2_dot(rb2->vel, n);
vec2 vt = vec2_sub(rb2->vel, vec2_mul(n, vn));
rb2->vel = vec2_add(vt, vec2_mul(n, -vn));
rb2->vel = vec2_reflect(rb2->vel, n);
} else if (rb1->type == TYPE_CIRCLE && rb2->type == TYPE_PLANE) {
rigid_body_handle_collision(idx2, idx1);
}