Nilorea Library

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros Groups
n_3d.c
Go to the documentation of this file.
1
8 #include "n_3d.h"
9
10
11
18 double distance( VECTOR3D p1 , VECTOR3D p2 )
19 {
20 return sqrt( ( p1[ 0 ] - p2[ 0 ] )*( p1[ 0 ] - p2[ 0 ] ) + ( p1[ 1 ] - p2[ 1 ] )*( p1[ 1 ] - p2[ 1 ] ) + ( p1[ 2 ] - p2[ 2 ] )*( p1[ 2 ] - p2[ 2 ] ) );
21 } /* distance(...) */
22
23
24
32 int update_physics_position_nb( PHYSICS *object , int it , double delta_t )
33 {
34 _assert( object , return FALSE );
35
36 object -> speed[ it ] = object -> speed[ it ] + ( object -> acceleration[ it ] * delta_t )/ 1000000.0 ;
37 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 ;
38
39 return TRUE ;
40 } /* update_physics_position_nb(...) */
41
42
43
51 int update_physics_position_reverse_nb( PHYSICS *object , int it , double delta_t )
52 {
53 _assert( object , return FALSE );
54
55 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 );
56 object -> speed[ it ] = object -> speed[ it ] - ( object -> acceleration[ it ] * delta_t )/ 1000000.0 ;
57 object -> angular_speed[ it ] = object -> angular_speed[ it ] - ( object -> angular_acceleration[ it ] * delta_t ) / 1000000.0 ;
58
59 return TRUE ;
60 } /* update_physics_position_reverse_nb */
61
62
69 int update_physics_position_reverse( PHYSICS *object , double delta_t )
70 {
71 for( int it = 0 ; it < 3 ; it ++ )
72 {
73 object -> speed[ it ] = -object -> speed[ it ] ;
74 object -> acceleration[ it ] = -object -> acceleration[ it ] ;
75
76 update_physics_position_nb( object , it , delta_t );
77
78 object -> speed[ it ] = -object -> speed[ it ] ;
79 object -> acceleration[ it ] = -object -> acceleration[ it ] ;
80 }
81 return TRUE ;
82 } /* update_physics_position_reverse(...) */
83
84
85
92 int update_physics_position( PHYSICS *object , double delta_t )
93 {
94 object -> delta_t = delta_t ;
95 for( int it = 0 ; it < 3 ; it ++ )
96 {
97 update_physics_position_nb( object , it , delta_t );
98 }
99 return TRUE ;
100 }
101
102
103
110 static int same_sign( double a , double b )
111 {
112 return (( a * b) >= 0 );
113 } /* same_sign(...) */
114
115
116
126 int vector_intersect( VECTOR3D p1 , VECTOR3D p2 , VECTOR3D p3 , VECTOR3D p4 , VECTOR3D *px )
127 {
128 double a1 = 0 , a2 = 0 , b1 = 0 , b2 = 0 , c1 = 0 , c2 = 0 ,
129 r1 = 0 , r2 = 0 , r3 = 0 , r4 = 0 ,
130 denom = 0 , offset = 0 , num = 0 ;
131
132 /* Compute a1, b1, c1, where line joining points 1 and 2 is
133 "a1 x + b1 y + c1 = 0". */
134 a1 = p2[ 1 ] - p1[ 1 ];
135 b1 = p1[ 0 ] - p2[ 0 ];
136 c1 = (p2[ 0 ] * p1[ 1 ]) - (p1[ 0 ] * p2[ 1 ]);
137
138 /* Compute r3 and r4. */
139 r3 = ((a1 * p3[ 0 ]) + (b1 * p3[ 1 ]) + c1);
140 r4 = ((a1 * p4[ 0 ]) + (b1 * p4[ 1 ]) + c1);
141
142 /* Check signs of r3 and r4. If both point 3 and point 4 lie on
143 same side of line 1, the line segments do not intersect. */
144 if((r3 != 0) && (r4 != 0) && same_sign(r3, r4))
145 {
146 return VECTOR3D_DONT_INTERSECT;
147 }
148
149 /* Compute a2, b2, c2 */
150 a2 = p4[ 1 ] - p3[ 1 ];
151 b2 = p3[ 0 ] - p4[ 0 ];
152 c2 = (p4[ 0 ] * p3[ 1 ]) - (p3[ 0 ] * p4[ 1 ]);
153
154 /* Compute r1 and r2 */
155 r1 = (a2 * p1[ 0 ]) + (b2 * p1[ 1 ]) + c2;
156 r2 = (a2 * p2[ 0 ]) + (b2 * p2[ 1 ]) + c2;
157
158 /* Check signs of r1 and r2. If both point 1 and point 2 lie
159 on same side of second line segment, the line segments do
160 not intersect. */
161 if( (r1 != 0) && (r2 != 0) && same_sign(r1, r2) )
162 {
163 return VECTOR3D_DONT_INTERSECT;
164 }
165
166 /* Line segments intersect: compute intersection point. */
167 denom = (a1 * b2) - (a2 * b1);
168
169 if( denom == 0 )
170 {
171 return VECTOR3D_COLLINEAR;
172 }
173
174 if( denom < 0 )
175 {
176 offset = -denom / 2;
177 }
178 else
179 {
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 {
189 (*px)[ 0 ] = (num - offset) / denom;
190 }
191 else
192 {
193 (*px)[ 0 ] = (num + offset) / denom;
194 }
195
196 num = (a2 * c1) - (a1 * c2);
197 if(num < 0)
198 {
199 (*px)[ 1 ] = ( num - offset) / denom;
200 }
201 else
202 {
203 (*px)[ 1 ] = (num + offset) / denom;
204 }
205
206 /* lines_intersect */
207 return VECTOR3D_DO_INTERSECT;
208 } /* vector_intersect(...) */