Project Ne10
An open, optimized software library for the ARM architecture.
NE10_physics.c
Go to the documentation of this file.
1 /*
2  * Copyright 2014-16 ARM Limited and Contributors.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of ARM Limited nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY ARM LIMITED AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL ARM LIMITED AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * NE10 Library : physics/NE10_physics.c
30  */
31 
32 #include "NE10_types.h"
33 
47 static inline ne10_vec2f_t ne10_mul_matvec_float (ne10_mat2x2f_t T, ne10_vec2f_t v)
48 {
49  ne10_vec2f_t tmp;
50  ne10_float32_t x = (T.c2.r2 * v.x - T.c2.r1 * v.y) + T.c1.r1;
51  ne10_float32_t y = (T.c2.r1 * v.x + T.c2.r2 * v.y) + T.c1.r2;
52  tmp.x = x;
53  tmp.y = y;
54  return tmp;
55 }
56 
57 static inline ne10_float32_t min (float a, ne10_float32_t b)
58 {
59  return a < b ? a : b;
60 }
61 
62 static inline ne10_vec2f_t min_2f (ne10_vec2f_t a, ne10_vec2f_t b)
63 {
64  ne10_vec2f_t tmp = {min (a.x, b.x), min (a.y, b.y) };
65  return tmp;
66 }
67 
68 static inline ne10_float32_t max (float a, ne10_float32_t b)
69 {
70  return a > b ? a : b;
71 }
72 
73 static inline ne10_vec2f_t max_2f (ne10_vec2f_t a, ne10_vec2f_t b)
74 {
75  ne10_vec2f_t tmp = {max (a.x, b.x), max (a.y, b.y) };
76  return tmp;
77 }
78 
84  ne10_vec2f_t *vertices,
85  ne10_mat2x2f_t *xf,
86  ne10_vec2f_t *radius,
87  ne10_uint32_t vertex_count)
88 {
89  ne10_vec2f_t lower = ne10_mul_matvec_float (*xf, vertices[0]);
90  ne10_vec2f_t upper = lower;
91  ne10_vec2f_t v;
92  ne10_int32_t i;
93 
94  for (i = 1; i < vertex_count; ++i)
95  {
96  v = ne10_mul_matvec_float (*xf, vertices[i]);
97  lower = min_2f (lower, v);
98  upper = max_2f (upper, v);
99  }
100 
101  aabb->c1.r1 = lower.x - radius->x;
102  aabb->c1.r2 = lower.y - radius->y;
103  aabb->c2.r1 = upper.x + radius->x;
104  aabb->c2.r2 = upper.y + radius->y;
105 
106 }
107 
113  ne10_vec3f_t *v_wa,
114  ne10_vec2f_t *ra,
115  ne10_vec3f_t *v_wb,
116  ne10_vec2f_t *rb,
117  ne10_uint32_t count)
118 {
119  ne10_int32_t i;
120  ne10_vec2f_t va;
121  ne10_vec2f_t vb;
122 
123  for (i = 0; i < count; i++)
124  {
125  va.x = v_wa->x - v_wa->z * ra->y;
126  va.y = v_wa->y + v_wa->z * ra->x;
127  vb.x = v_wb->x - v_wb->z * rb->y;
128  vb.y = v_wb->y + v_wb->z * rb->x;
129 
130  dv->x = vb.x - va.x;
131  dv->y = vb.y - va.y;
132 
133  v_wa++;
134  v_wb++;
135  ra++;
136  rb++;
137  dv++;
138 
139  }
140 }
141 
147  ne10_vec3f_t *v_wb,
148  ne10_vec2f_t *ra,
149  ne10_vec2f_t *rb,
150  ne10_vec2f_t *ima,
151  ne10_vec2f_t *imb,
152  ne10_vec2f_t *p,
153  ne10_uint32_t count)
154 {
155  ne10_int32_t i;
156 
157  for (i = 0; i < count; i++)
158  {
159  v_wa->x -= ima->x * p->x;
160  v_wa->y -= ima->x * p->y;
161  v_wa->z -= ima->y * (ra->x * p->y - ra->y * p->x);
162 
163  v_wb->x += imb->x * p->x;
164  v_wb->y += imb->x * p->y;
165  v_wb->z += imb->y * (rb->x * p->y - rb->y * p->x);
166 
167  v_wa++;
168  v_wb++;
169  ra++;
170  rb++;
171  ima++;
172  imb++;
173  p++;
174  }
175 }
int32_t ne10_int32_t
Definition: NE10_types.h:76
A 2-tuple of ne10_float32_t values.
Definition: NE10_types.h:87
float ne10_float32_t
Definition: NE10_types.h:80
void ne10_physics_apply_impulse_vec2f_c(ne10_vec3f_t *v_wa, ne10_vec3f_t *v_wb, ne10_vec2f_t *ra, ne10_vec2f_t *rb, ne10_vec2f_t *ima, ne10_vec2f_t *imb, ne10_vec2f_t *p, ne10_uint32_t count)
Specific implementation of ne10_physics_apply_impulse_vec2f using plain C.
Definition: NE10_physics.c:146
uint32_t ne10_uint32_t
Definition: NE10_types.h:77
void ne10_physics_compute_aabb_vec2f_c(ne10_mat2x2f_t *aabb, ne10_vec2f_t *vertices, ne10_mat2x2f_t *xf, ne10_vec2f_t *radius, ne10_uint32_t vertex_count)
Specific implementation of ne10_physics_compute_aabb_vec2f using plain C.
Definition: NE10_physics.c:83
void ne10_physics_relative_v_vec2f_c(ne10_vec2f_t *dv, ne10_vec3f_t *v_wa, ne10_vec2f_t *ra, ne10_vec3f_t *v_wb, ne10_vec2f_t *rb, ne10_uint32_t count)
Specific implementation of ne10_physics_relative_v_vec2f using plain C.
Definition: NE10_physics.c:112
ne10_float32_t x
Definition: NE10_types.h:98
ne10_float32_t x
Definition: NE10_types.h:89
ne10_mat_row2f c2
Definition: NE10_types.h:127
A 3-tuple of ne10_float32_t values.
Definition: NE10_types.h:96
ne10_float32_t z
Definition: NE10_types.h:100
ne10_float32_t y
Definition: NE10_types.h:90
ne10_float32_t r2
Definition: NE10_types.h:121
ne10_float32_t y
Definition: NE10_types.h:99
ne10_float32_t r1
Definition: NE10_types.h:120
ne10_mat_row2f c1
Definition: NE10_types.h:126