Revision 1040
| branches/simulator/projects/simulator/simulator/core/world.c (revision 1040) | ||
|---|---|---|
| 12 | 12 |
#include <stdarg.h> |
| 13 | 13 |
#include "world.h" |
| 14 | 14 |
|
| 15 |
world_t world; |
|
| 15 | 16 |
|
| 17 |
int init_world(int max_objs, bbox_t b) |
|
| 18 |
{
|
|
| 19 |
world.max_objs = max_objs; |
|
| 20 |
world.cur_objs = 0; |
|
| 21 |
world.objs = calloc(max_objs, sizeof(object_t)); |
|
| 22 |
world.win = b; |
|
| 23 |
//PBBOX(b); |
|
| 24 |
} |
|
| 25 |
int destroy_world() |
|
| 26 |
{
|
|
| 27 |
int i; |
|
| 28 |
for (i = 0; i < world.cur_objs; i++) {
|
|
| 29 |
destroy(&world.objs[i]); |
|
| 30 |
} |
|
| 31 |
free(world.objs); |
|
| 32 |
} |
|
| 16 | 33 |
|
| 17 | 34 |
double (*collide_func[NUM_SHAPES])(ray_t *ray, object_t *obj) = |
| 18 | 35 |
{
|
| ... | ... | |
| 22 | 39 |
{
|
| 23 | 40 |
create_poly |
| 24 | 41 |
}; |
| 42 |
int (*destroy_func[NUM_SHAPES])(object_t *obj) = |
|
| 43 |
{
|
|
| 44 |
destroy_poly |
|
| 45 |
}; |
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 25 | 49 |
double collide_circle(ray_t *ray, object_t *obj) |
| 26 | 50 |
{
|
| 27 | 51 |
return -1; |
| ... | ... | |
| 57 | 81 |
return dist; |
| 58 | 82 |
|
| 59 | 83 |
} |
| 60 |
double collide_rect(ray_t *ray, object_t *obj) {}
|
|
| 61 |
/* |
|
| 62 |
{
|
|
| 63 |
seg_t s; |
|
| 64 |
double min = RAY_MISS, x; |
|
| 65 |
rect_t *rect = (rect_t *)obj->props; |
|
| 66 | 84 |
|
| 67 |
s.p1 = s.p2 = rect->p1; |
|
| 68 |
s.p2.y = rect->p2.y; |
|
| 69 |
if ((x = collide_seg(ray, (seg_t *)&s)) < min) |
|
| 70 |
min = x; |
|
| 71 |
|
|
| 72 |
s.p1 = rect->p2; |
|
| 73 |
if ((x = collide_seg(ray, (seg_t *)&s)) < min) |
|
| 74 |
min = x; |
|
| 75 |
|
|
| 76 |
s.p2 = rect->p1; |
|
| 77 |
s.p2.x = rect->p2.x; |
|
| 78 |
if ((x = collide_seg(ray, (seg_t *)&s)) < min) |
|
| 79 |
min = x; |
|
| 80 |
|
|
| 81 |
s.p1 = rect->p1; |
|
| 82 |
if ((x = collide_seg(ray, (seg_t *)&s)) < min) |
|
| 83 |
min = x; |
|
| 84 |
|
|
| 85 |
return min; |
|
| 86 |
} |
|
| 87 |
*/ |
|
| 88 |
|
|
| 89 | 85 |
double collide_poly(ray_t *ray, object_t *obj) |
| 90 | 86 |
{
|
| 91 | 87 |
int i; |
| ... | ... | |
| 102 | 98 |
min = x; |
| 103 | 99 |
} |
| 104 | 100 |
} |
| 105 |
if (p->type == POLY_DISCONNECTED) {
|
|
| 101 |
if (p->type == POLY_CONNECTED) {
|
|
| 106 | 102 |
if ((x = collide_seg(ray,p->pts[i],p->pts[0])) < min) |
| 107 | 103 |
min = x; |
| 108 | 104 |
} |
| ... | ... | |
| 115 | 111 |
{
|
| 116 | 112 |
return -1; |
| 117 | 113 |
} |
| 118 |
return collide_func[obj->id](ray, obj); |
|
| 114 |
return collide_func[obj->id+ID_OFFSET](ray, obj); |
|
| 119 | 115 |
} |
| 120 | 116 |
|
| 121 | 117 |
|
| 122 | 118 |
|
| 123 | 119 |
object_t *create(int id, ...) |
| 124 | 120 |
{
|
| 125 |
object_t *obj; |
|
| 121 |
object_t *obj = &(world.objs[world.cur_objs++]); |
|
| 126 | 122 |
va_list args; |
| 127 | 123 |
|
| 128 |
obj = malloc(sizeof(object_t)); |
|
| 129 |
obj->id = id; |
|
| 130 | 124 |
va_start(args, id); |
| 131 |
if (create_func[id](obj, args) < 0) {
|
|
| 132 |
free(obj); |
|
| 133 |
obj = NULL; |
|
| 125 |
if (create_func[id+ID_OFFSET](obj, args) < 0) {
|
|
| 126 |
/* Cleanup ? */ |
|
| 134 | 127 |
} |
| 135 | 128 |
va_end(args); |
| 136 | 129 |
return obj; |
| 137 | 130 |
} |
| 138 | 131 |
|
| 132 |
int destroy(object_t *obj) |
|
| 133 |
{
|
|
| 134 |
if (obj->id == ID_NULL) |
|
| 135 |
return 0; |
|
| 136 |
destroy_func[obj->id+ID_OFFSET](obj); |
|
| 137 |
obj->id = ID_NULL; |
|
| 138 |
} |
|
| 139 |
|
|
| 139 | 140 |
/** |
| 140 | 141 |
* |
| 141 | 142 |
**/ |
| ... | ... | |
| 171 | 172 |
int argc = va_arg(ap, int); |
| 172 | 173 |
} |
| 173 | 174 |
|
| 174 |
void destroy_poly (object_t *obj) |
|
| 175 |
int destroy_poly (object_t *obj) |
|
| 175 | 176 |
{
|
| 176 | 177 |
poly_t *p; |
| 177 |
int i; |
|
| 178 |
|
|
| 179 |
if (obj == NULL) |
|
| 180 |
return; |
|
| 181 | 178 |
p = (poly_t *)obj->props; |
| 182 | 179 |
if (p != NULL) free(p->pts); |
| 183 | 180 |
free(p); |
| 184 |
free(obj); |
|
| 181 |
return 1; |
|
| 182 |
} |
|
| 185 | 183 |
|
| 186 |
for (i = 0; i < p->num_pts; i++) |
|
| 187 |
{
|
|
| 184 |
void print_world(void) |
|
| 185 |
{
|
|
| 186 |
int i; |
|
| 187 |
printf("WORLD: %d object(s) in (%g,%g)->(%g,%g)\n", world.cur_objs, world.win.p1.x,world.win.p1.y,world.win.p2.x,world.win.p2.y);
|
|
| 188 |
for (i = 0; i < world.cur_objs; i++) {
|
|
| 189 |
print_object(&world.objs[i]); |
|
| 188 | 190 |
} |
| 189 |
|
|
| 190 |
|
|
| 191 |
|
|
| 192 |
|
|
| 193 | 191 |
} |
| 194 |
|
|
| 195 | 192 |
void print_object(object_t *obj) |
| 196 | 193 |
{
|
| 197 |
if (obj == NULL) {
|
|
| 198 |
printf("No object\n");
|
|
| 199 |
return; |
|
| 200 |
} |
|
| 201 | 194 |
int i; |
| 202 | 195 |
poly_t *p; |
| 203 | 196 |
switch (obj->id) {
|
| 197 |
case ID_NULL: |
|
| 198 |
printf("\tNULL\n");
|
|
| 204 | 199 |
case ID_POLY: |
| 205 | 200 |
p = (poly_t *) obj->props; |
| 206 |
printf("POLYGON (%d points, %s) { ", p->num_pts, p->type?"connected" : "disconnected");
|
|
| 201 |
printf("\tPOLYGON (%d points, %s) { ", p->num_pts, p->type?"connected" : "disconnected");
|
|
| 207 | 202 |
for (i = 0; i < p->num_pts; i++){
|
| 208 | 203 |
printf("(%g, %g) ", p->pts[i].x, p->pts[i].y);
|
| 209 | 204 |
} |
| branches/simulator/projects/simulator/simulator/core/world.h (revision 1040) | ||
|---|---|---|
| 13 | 13 |
|
| 14 | 14 |
#include <stdarg.h> |
| 15 | 15 |
|
| 16 |
#define ID_OFFSET -1 |
|
| 16 | 17 |
#define ID_RECTANGLE 3 |
| 17 |
#define ID_CIRCLE 1 |
|
| 18 |
|
|
| 19 |
#define ID_POLY 0 |
|
| 18 |
#define ID_CIRCLE 2 |
|
| 19 |
#define ID_NULL 0 |
|
| 20 |
#define ID_POLY 1 |
|
| 20 | 21 |
#define POLY_DISCONNECTED 0 |
| 21 | 22 |
#define POLY_CONNECTED 1 |
| 22 | 23 |
#define POLY_RECT 2 |
| ... | ... | |
| 24 | 25 |
#define NUM_SHAPES 3 |
| 25 | 26 |
#define RAY_MISS 1E30 |
| 26 | 27 |
#define CREATE(id, ...) (create_func[id](__VA_ARGS__)) |
| 28 |
#define PBBOX(b) {printf("(%g,%g) --> (%g, %g)\n",(b).p1.x,(b).p1.y,(b).p2.x,\
|
|
| 29 |
(b).p2.y);} |
|
| 27 | 30 |
typedef struct {
|
| 28 | 31 |
double x; |
| 29 | 32 |
double y; |
| ... | ... | |
| 52 | 55 |
} object_t; |
| 53 | 56 |
|
| 54 | 57 |
typedef struct {
|
| 55 |
int num_objs; |
|
| 58 |
int max_objs; |
|
| 59 |
int cur_objs; |
|
| 56 | 60 |
object_t *objs; |
| 57 |
bbox_t world; |
|
| 61 |
bbox_t win; |
|
| 58 | 62 |
} world_t; |
| 59 | 63 |
|
| 60 | 64 |
/* Specific collision functions */ |
| ... | ... | |
| 66 | 70 |
* Must be listed in the same order as IDs */ |
| 67 | 71 |
extern double (*collide_func[NUM_SHAPES])(ray_t *ray, object_t *obj); |
| 68 | 72 |
extern int (*create_func[NUM_SHAPES])(object_t *obj, va_list ap); |
| 73 |
extern int (*destroy_func[NUM_SHAPES])(object_t *obj); |
|
| 74 |
extern world_t world; |
|
| 69 | 75 |
|
| 70 |
|
|
| 76 |
int init_world(int num_objs, bbox_t b); |
|
| 77 |
int destroy_world(void); |
|
| 71 | 78 |
int create_poly(object_t *obj, va_list ap); |
| 79 |
int destroy_poly(object_t *obj); |
|
| 72 | 80 |
object_t *create(int id, ...); |
| 73 | 81 |
|
| 74 |
void destroy_poly (object_t *obj); |
|
| 82 |
void print_world(void); |
|
| 83 |
void print_object(object_t *obj); |
|
| 84 |
|
|
| 75 | 85 |
#endif |
| 76 | 86 |
|
Also available in: Unified diff