Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_3d.c
Go to the documentation of this file.
1
8#include "nilorea/n_3d.h"
9#include "math.h"
10
17double distance(VECTOR3D* p1, VECTOR3D* p2) {
18 return sqrt(((*p1)[0] - (*p2)[0]) * ((*p1)[0] - (*p2)[0]) +
19 ((*p1)[1] - (*p2)[1]) * ((*p1)[1] - (*p2)[1]) +
20 ((*p1)[2] - (*p2)[2]) * ((*p1)[2] - (*p2)[2]));
21} /* distance(...) */
22
30int update_physics_position_nb(PHYSICS* object, int it, double delta_t) {
31 __n_assert(object, return FALSE);
32
33 object->speed[it] = object->speed[it] + (object->acceleration[it] * delta_t) / 1000000.0;
34 object->position[it] = object->position[it] + (object->speed[it] * delta_t) / 1000000.0 + (object->acceleration[it] * (delta_t / 1000000.0) * (delta_t / 1000000.0)) / 2.0;
35 object->angular_speed[it] = object->angular_speed[it] + (object->angular_acceleration[it] * delta_t) / 1000000.0;
36 object->speed[it] = object->speed[it] + (object->gravity[it] * delta_t) / 1000000.0;
37
38 return TRUE;
39} /* update_physics_position_nb(...) */
40
48int update_physics_position_reverse_nb(PHYSICS* object, int it, double delta_t) {
49 __n_assert(object, return FALSE);
50
51 object->position[it] = object->position[it] - ((object->speed[it] * delta_t) / 1000000.0 + (object->acceleration[it] * (delta_t / 1000000.0) * (delta_t / 1000000.0)) / 2.0);
52 object->speed[it] = object->speed[it] - (object->acceleration[it] * delta_t) / 1000000.0;
53 object->angular_speed[it] = object->angular_speed[it] - (object->angular_acceleration[it] * delta_t) / 1000000.0;
54
55 return TRUE;
56} /* update_physics_position_reverse_nb */
57
64int update_physics_position_reverse(PHYSICS* object, double delta_t) {
65 for (int it = 0; it < 3; it++) {
66 object->speed[it] = -object->speed[it];
67 object->acceleration[it] = -object->acceleration[it];
68 object->angular_speed[it] = -object->angular_speed[it];
69
70 update_physics_position_nb(object, it, delta_t);
71
72 object->speed[it] = -object->speed[it];
73 object->acceleration[it] = -object->acceleration[it];
74 object->angular_speed[it] = -object->angular_speed[it];
75 }
76 return TRUE;
77} /* update_physics_position_reverse(...) */
78
85int update_physics_position(PHYSICS* object, double delta_t) {
86 object->delta_t = delta_t;
87 for (int it = 0; it < 3; it++) {
88 update_physics_position_nb(object, it, delta_t);
89 }
90 return TRUE;
91}
92
99static int same_sign(double a, double b) {
100 return ((a * b) >= 0);
101} /* same_sign(...) */
102
113 double a1 = 0, a2 = 0, b1 = 0, b2 = 0, c1 = 0, c2 = 0,
114 r1 = 0, r2 = 0, r3 = 0, r4 = 0,
115 denom = 0, offset = 0, num = 0;
116
117 /* Compute a1, b1, c1, where line joining points 1 and 2 is
118 "a1 x + b1 y + c1 = 0". */
119 a1 = (*p2)[1] - (*p1)[1];
120 b1 = (*p1)[0] - (*p2)[0];
121 c1 = ((*p2)[0] * (*p1)[1]) - ((*p1)[0] * (*p2)[1]);
122
123 /* Compute r3 and r4. */
124 r3 = ((a1 * (*p3)[0]) + (b1 * (*p3)[1]) + c1);
125 r4 = ((a1 * (*p4)[0]) + (b1 * (*p4)[1]) + c1);
126
127 /* Check signs of r3 and r4. If both point 3 and point 4 lie on
128 same side of line 1, the line segments do not intersect. */
129 if ((r3 != 0) && (r4 != 0) && same_sign(r3, r4)) {
131 }
132
133 /* Compute a2, b2, c2 */
134 a2 = (*p4)[1] - (*p3)[1];
135 b2 = (*p3)[0] - (*p4)[0];
136 c2 = ((*p4)[0] * (*p3)[1]) - ((*p3)[0] * (*p4)[1]);
137
138 /* Compute r1 and r2 */
139 r1 = (a2 * (*p1)[0]) + (b2 * (*p1)[1]) + c2;
140 r2 = (a2 * (*p2)[0]) + (b2 * (*p2)[1]) + c2;
141
142 /* Check signs of r1 and r2. If both point 1 and point 2 lie
143 on same side of second line segment, the line segments do
144 not intersect. */
145 if ((r1 != 0) && (r2 != 0) && same_sign(r1, r2)) {
147 }
148
149 /* Line segments intersect: compute intersection point. */
150 denom = (a1 * b2) - (a2 * b1);
151
152 if (denom == 0) {
153 return VECTOR3D_COLLINEAR;
154 }
155
156 if (denom < 0) {
157 offset = -denom / 2;
158 } else {
159 offset = denom / 2;
160 }
161
162 /* The denom/2 is to get rounding instead of truncating. It
163 is added or subtracted to the numerator, depending upon the
164 sign of the numerator. */
165 num = (b1 * c2) - (b2 * c1);
166 if (num < 0) {
167 (*px)[0] = (num - offset) / denom;
168 } else {
169 (*px)[0] = (num + offset) / denom;
170 }
171
172 num = (a2 * c1) - (a1 * c2);
173 if (num < 0) {
174 (*px)[1] = (num - offset) / denom;
175 } else {
176 (*px)[1] = (num + offset) / denom;
177 }
178
179 /* lines_intersect */
181} /* vector_intersect(...) */
182
190 return (*vec1)[0] * (*vec2)[0] + (*vec1)[1] * (*vec2)[1] + (*vec1)[2] * (*vec2)[2];
191} /* vector_dot_product(...) */
192
199 double res = 0.0;
200 for (int i = 0; i < 3; i++) {
201 res += pow((*vec)[i], 2);
202 }
203 return sqrt(res);
204} /* vector_normalize(...) */
205
213 return acos(vector_dot_product(&(*vec1), &(*vec2)) / (vector_normalize(&(*vec1)) * vector_normalize(&(*vec2))));
214} /* vector_angle_between( ... ) */
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:258
VECTOR3D acceleration
ax,ay,az actual acceleration
Definition n_3d.h:56
double vector_dot_product(VECTOR3D *vec1, VECTOR3D *vec2)
Compute the dot product of two VECTOR3D.
Definition n_3d.c:189
#define VECTOR3D_COLLINEAR
value when the two VECTOR3D are collinear
Definition n_3d.h:34
double distance(VECTOR3D *p1, VECTOR3D *p2)
compute the distance between two VECTOR3D points
Definition n_3d.c:17
#define VECTOR3D_DO_INTERSECT
value when the two VECTOR3D are intersecting
Definition n_3d.h:36
double VECTOR3D[3]
struct of a point
Definition n_3d.h:39
double vector_angle_between(VECTOR3D *vec1, VECTOR3D *vec2)
Compute angle between two VECTOR3D.
Definition n_3d.c:212
int update_physics_position_nb(PHYSICS *object, int it, double delta_t)
Update object position component.
Definition n_3d.c:30
#define VECTOR3D_DONT_INTERSECT
value when the two VECTOR3D are not connected
Definition n_3d.h:32
int update_physics_position_reverse(PHYSICS *object, double delta_t)
Update object position, reversed.
Definition n_3d.c:64
int update_physics_position_reverse_nb(PHYSICS *object, int it, double delta_t)
Update object position component, reversed.
Definition n_3d.c:48
double vector_normalize(VECTOR3D *vec)
Return the normalized value of vec.
Definition n_3d.c:198
int update_physics_position(PHYSICS *object, double delta_t)
Update object position, reversed.
Definition n_3d.c:85
int vector_intersect(VECTOR3D *p1, VECTOR3D *p2, VECTOR3D *p3, VECTOR3D *p4, VECTOR3D *px)
Compute if two vectors are intersecting or not.
Definition n_3d.c:112
structure of the physics of an object
Definition n_3d.h:48
static int same_sign(double a, double b)
Quickly check if two walues are the same sign or not.
Definition n_3d.c:99
Simple 3D movement simulation.