Nilorea Library
C utilities for networking, threading, graphics
n_3d.c
Go to the documentation of this file.
1
8#include "nilorea/n_3d.h"
9#include "math.h"
10
11
18double distance( VECTOR3D *p1, VECTOR3D *p2 )
19{
20 return sqrt( ( (*p1)[ 0 ] - (*p2)[ 0 ] ) * ( (*p1)[ 0 ] - (*p2)[ 0 ] ) +
21 ( (*p1)[ 1 ] - (*p2)[ 1 ] ) * ( (*p1)[ 1 ] - (*p2)[ 1 ] ) +
22 ( (*p1)[ 2 ] - (*p2)[ 2 ] ) * ( (*p1)[ 2 ] - (*p2)[ 2 ] ) );
23} /* distance(...) */
24
25
26
34int update_physics_position_nb( PHYSICS *object, int it, double delta_t )
35{
36 __n_assert( object, return FALSE );
37
38 object -> speed[ it ] = object -> speed[ it ] + ( object -> acceleration[ it ] * delta_t )/ 1000000.0 ;
39 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 ;
40 object -> angular_speed[ it ] = object -> angular_speed[ it ] + ( object -> angular_acceleration[ it ] * delta_t ) / 1000000.0 ;
41 object -> speed[ it ] = object -> speed[ it ] + ( object -> gravity[ it ] * delta_t ) / 1000000.0 ;
42
43 return TRUE ;
44} /* update_physics_position_nb(...) */
45
46
47
55int update_physics_position_reverse_nb( PHYSICS *object, int it, double delta_t )
56{
57 __n_assert( object, return FALSE );
58
59 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 );
60 object -> speed[ it ] = object -> speed[ it ] - ( object -> acceleration[ it ] * delta_t )/ 1000000.0 ;
61 object -> angular_speed[ it ] = object -> angular_speed[ it ] - ( object -> angular_acceleration[ it ] * delta_t ) / 1000000.0 ;
62
63 return TRUE ;
64} /* update_physics_position_reverse_nb */
65
66
73int update_physics_position_reverse( PHYSICS *object, double delta_t )
74{
75 for( int it = 0 ; it < 3 ; it ++ )
76 {
77 object -> speed[ it ] = -object -> speed[ it ] ;
78 object -> acceleration[ it ] = -object -> acceleration[ it ] ;
79 object -> angular_speed[ it ] = -object -> angular_speed[ it ];
80
81 update_physics_position_nb( object, it, delta_t );
82
83 object -> speed[ it ] = -object -> speed[ it ] ;
84 object -> acceleration[ it ] = -object -> acceleration[ it ] ;
85 object -> angular_speed[ it ] = -object -> angular_speed[ it ] ;
86 }
87 return TRUE ;
88} /* update_physics_position_reverse(...) */
89
90
91
98int update_physics_position( PHYSICS *object, double delta_t )
99{
100 object -> delta_t = delta_t ;
101 for( int it = 0 ; it < 3 ; it ++ )
102 {
103 update_physics_position_nb( object, it, delta_t );
104 }
105 return TRUE ;
106}
107
108
109
116static int same_sign( double a, double b )
117{
118 return (( a * b) >= 0 );
119} /* same_sign(...) */
120
121
122
133{
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))
151 {
153 }
154
155 /* Compute a2, b2, c2 */
156 a2 = (*p4)[ 1 ] - (*p3)[ 1 ];
157 b2 = (*p3)[ 0 ] - (*p4)[ 0 ];
158 c2 = ((*p4)[ 0 ] * (*p3)[ 1 ]) - ((*p3)[ 0 ] * (*p4)[ 1 ]);
159
160 /* Compute r1 and r2 */
161 r1 = (a2 * (*p1)[ 0 ]) + (b2 * (*p1)[ 1 ]) + c2;
162 r2 = (a2 * (*p2)[ 0 ]) + (b2 * (*p2)[ 1 ]) + c2;
163
164 /* Check signs of r1 and r2. If both point 1 and point 2 lie
165 on same side of second line segment, the line segments do
166 not intersect. */
167 if( (r1 != 0) && (r2 != 0) && same_sign(r1, r2) )
168 {
170 }
171
172 /* Line segments intersect: compute intersection point. */
173 denom = (a1 * b2) - (a2 * b1);
174
175 if( denom == 0 )
176 {
177 return VECTOR3D_COLLINEAR;
178 }
179
180 if( denom < 0 )
181 {
182 offset = -denom / 2;
183 }
184 else
185 {
186 offset = denom / 2 ;
187 }
188
189 /* The denom/2 is to get rounding instead of truncating. It
190 is added or subtracted to the numerator, depending upon the
191 sign of the numerator. */
192 num = (b1 * c2) - (b2 * c1);
193 if(num < 0)
194 {
195 (*px)[ 0 ] = (num - offset) / denom;
196 }
197 else
198 {
199 (*px)[ 0 ] = (num + offset) / denom;
200 }
201
202 num = (a2 * c1) - (a1 * c2);
203 if(num < 0)
204 {
205 (*px)[ 1 ] = ( num - offset) / denom;
206 }
207 else
208 {
209 (*px)[ 1 ] = (num + offset) / denom;
210 }
211
212 /* lines_intersect */
214} /* vector_intersect(...) */
215
216
217
225{
226 return (*vec1)[ 0 ] * (*vec2)[ 0 ] + (*vec1)[ 1 ] * (*vec2)[ 1 ] + (*vec1)[ 2 ] * (*vec2)[ 2 ];
227} /* vector_dot_product(...) */
228
229
230
237{
238 double res = 0.0 ;
239 for( int i=0 ; i < 3 ; i++ )
240 {
241 res += pow( (*vec)[ i ], 2 );
242 }
243 return sqrt( res );
244} /* vector_normalize(...) */
245
246
247
255{
256 return acos( vector_dot_product( &(*vec1), &(*vec2) ) / ( vector_normalize( &(*vec1) ) * vector_normalize( &(*vec2) ) ) );
257} /* vector_angle_between( ... ) */
258
259
#define __n_assert(__ptr, __ret)
macro to assert things
Definition: n_common.h:276
double vector_dot_product(VECTOR3D *vec1, VECTOR3D *vec2)
Compute the dot product of two VECTOR3D.
Definition: n_3d.c:224
#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:18
#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 beteen two VECTOR3D.
Definition: n_3d.c:254
int update_physics_position_nb(PHYSICS *object, int it, double delta_t)
Update object position componant.
Definition: n_3d.c:34
#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:73
int update_physics_position_reverse_nb(PHYSICS *object, int it, double delta_t)
Update object position componant, reversed.
Definition: n_3d.c:55
double vector_normalize(VECTOR3D *vec)
Return the normalized value of vec.
Definition: n_3d.c:236
int update_physics_position(PHYSICS *object, double delta_t)
Update object position, reversed.
Definition: n_3d.c:98
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:132
structure of the physics of an object
Definition: n_3d.h:50
static int same_sign(double a, double b)
Quickly check if two walue are the same sign or not.
Definition: n_3d.c:116
Simple 3D movement simulation.