Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_3d.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#include "nilorea/n_3d.h"
28#include "math.h"
29
36double distance(VECTOR3D* p1, VECTOR3D* p2) {
37 return sqrt(((*p1)[0] - (*p2)[0]) * ((*p1)[0] - (*p2)[0]) +
38 ((*p1)[1] - (*p2)[1]) * ((*p1)[1] - (*p2)[1]) +
39 ((*p1)[2] - (*p2)[2]) * ((*p1)[2] - (*p2)[2]));
40} /* distance(...) */
41
49int update_physics_position_nb(PHYSICS* object, int it, double delta_t) {
50 __n_assert(object, return FALSE);
51
52 object->speed[it] = object->speed[it] + (object->acceleration[it] * delta_t) / 1000000.0;
53 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;
54 object->angular_speed[it] = object->angular_speed[it] + (object->angular_acceleration[it] * delta_t) / 1000000.0;
55 object->speed[it] = object->speed[it] + (object->gravity[it] * delta_t) / 1000000.0;
56
57 return TRUE;
58} /* update_physics_position_nb(...) */
59
67int update_physics_position_reverse_nb(PHYSICS* object, int it, double delta_t) {
68 __n_assert(object, return FALSE);
69
70 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);
71 object->speed[it] = object->speed[it] - (object->acceleration[it] * delta_t) / 1000000.0;
72 object->angular_speed[it] = object->angular_speed[it] - (object->angular_acceleration[it] * delta_t) / 1000000.0;
73
74 return TRUE;
75} /* update_physics_position_reverse_nb */
76
83int update_physics_position_reverse(PHYSICS* object, double delta_t) {
84 __n_assert(object, return FALSE);
85 for (int it = 0; it < 3; it++) {
86 object->speed[it] = -object->speed[it];
87 object->acceleration[it] = -object->acceleration[it];
88 object->angular_speed[it] = -object->angular_speed[it];
89
90 update_physics_position_nb(object, it, delta_t);
91
92 object->speed[it] = -object->speed[it];
93 object->acceleration[it] = -object->acceleration[it];
94 object->angular_speed[it] = -object->angular_speed[it];
95 }
96 return TRUE;
97} /* update_physics_position_reverse(...) */
98
105int update_physics_position(PHYSICS* object, double delta_t) {
106 __n_assert(object, return FALSE);
107 object->delta_t = (time_t)delta_t;
108 for (int it = 0; it < 3; it++) {
109 update_physics_position_nb(object, it, delta_t);
110 }
111 return TRUE;
112}
113
120static int same_sign(double a, double b) {
121 return ((a * b) >= 0);
122} /* same_sign(...) */
123
134 double a1 = 0, a2 = 0, b1 = 0, b2 = 0, c1 = 0, c2 = 0,
135 r1 = 0, r2 = 0, r3 = 0, r4 = 0,
136 denom = 0, offset = 0, num = 0;
137
138 /* Compute a1, b1, c1, where line joining points 1 and 2 is
139 "a1 x + b1 y + c1 = 0". */
140 a1 = (*p2)[1] - (*p1)[1];
141 b1 = (*p1)[0] - (*p2)[0];
142 c1 = ((*p2)[0] * (*p1)[1]) - ((*p1)[0] * (*p2)[1]);
143
144 /* Compute r3 and r4. */
145 r3 = ((a1 * (*p3)[0]) + (b1 * (*p3)[1]) + c1);
146 r4 = ((a1 * (*p4)[0]) + (b1 * (*p4)[1]) + c1);
147
148 /* Check signs of r3 and r4. If both point 3 and point 4 lie on
149 same side of line 1, the line segments do not intersect. */
150 if ((r3 != 0) && (r4 != 0) && same_sign(r3, r4)) {
152 }
153
154 /* Compute a2, b2, c2 */
155 a2 = (*p4)[1] - (*p3)[1];
156 b2 = (*p3)[0] - (*p4)[0];
157 c2 = ((*p4)[0] * (*p3)[1]) - ((*p3)[0] * (*p4)[1]);
158
159 /* Compute r1 and r2 */
160 r1 = (a2 * (*p1)[0]) + (b2 * (*p1)[1]) + c2;
161 r2 = (a2 * (*p2)[0]) + (b2 * (*p2)[1]) + c2;
162
163 /* Check signs of r1 and r2. If both point 1 and point 2 lie
164 on same side of second line segment, the line segments do
165 not intersect. */
166 if ((r1 != 0) && (r2 != 0) && same_sign(r1, r2)) {
168 }
169
170 /* Line segments intersect: compute intersection point. */
171 denom = (a1 * b2) - (a2 * b1);
172
173 if (denom == 0) {
174 return VECTOR3D_COLLINEAR;
175 }
176
177 if (denom < 0) {
178 offset = -denom / 2;
179 } else {
180 offset = denom / 2;
181 }
182
183 /* The denom/2 is to get rounding instead of truncating. It
184 is added or subtracted to the numerator, depending upon the
185 sign of the numerator. */
186 num = (b1 * c2) - (b2 * c1);
187 if (num < 0) {
188 (*px)[0] = (num - offset) / denom;
189 } else {
190 (*px)[0] = (num + offset) / denom;
191 }
192
193 num = (a2 * c1) - (a1 * c2);
194 if (num < 0) {
195 (*px)[1] = (num - offset) / denom;
196 } else {
197 (*px)[1] = (num + offset) / denom;
198 }
199
200 /* lines_intersect */
202} /* vector_intersect(...) */
203
211 return (*vec1)[0] * (*vec2)[0] + (*vec1)[1] * (*vec2)[1] + (*vec1)[2] * (*vec2)[2];
212} /* vector_dot_product(...) */
213
220 double res = 0.0;
221 for (int i = 0; i < 3; i++) {
222 res += pow((*vec)[i], 2);
223 }
224 return sqrt(res);
225} /* vector_normalize(...) */
226
234 double norm = vector_normalize(vec1) * vector_normalize(vec2);
235 if (norm == 0.0) return 0.0;
236 return acos(vector_dot_product(vec1, vec2) / norm);
237} /* vector_angle_between( ... ) */
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:278
VECTOR3D acceleration
ax,ay,az actual acceleration
Definition n_3d.h:77
double vector_dot_product(VECTOR3D *vec1, VECTOR3D *vec2)
Compute the dot product of two VECTOR3D.
Definition n_3d.c:210
#define VECTOR3D_COLLINEAR
value when the two VECTOR3D are collinear
Definition n_3d.h:53
double distance(VECTOR3D *p1, VECTOR3D *p2)
compute the distance between two VECTOR3D points
Definition n_3d.c:36
#define VECTOR3D_DO_INTERSECT
value when the two VECTOR3D are intersecting
Definition n_3d.h:55
double VECTOR3D[3]
struct of a point
Definition n_3d.h:58
double vector_angle_between(VECTOR3D *vec1, VECTOR3D *vec2)
Compute angle between two VECTOR3D.
Definition n_3d.c:233
int update_physics_position_nb(PHYSICS *object, int it, double delta_t)
Update object position component.
Definition n_3d.c:49
#define VECTOR3D_DONT_INTERSECT
value when the two VECTOR3D are not connected
Definition n_3d.h:51
int update_physics_position_reverse(PHYSICS *object, double delta_t)
Update object position, reversed.
Definition n_3d.c:83
int update_physics_position_reverse_nb(PHYSICS *object, int it, double delta_t)
Update object position component, reversed.
Definition n_3d.c:67
double vector_normalize(VECTOR3D *vec)
Return the normalized value of vec.
Definition n_3d.c:219
int update_physics_position(PHYSICS *object, double delta_t)
Update object position.
Definition n_3d.c:105
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:133
structure of the physics of an object
Definition n_3d.h:69
static int same_sign(double a, double b)
Quickly check if two values are the same sign or not.
Definition n_3d.c:120
Simple 3D movement simulation.