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