Source

pygame / src / pgShapeObject.c

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
#include "pgShapeObject.h"
#include "pgBodyObject.h"
#include "pgCollision.h"
#include <string.h>
#include <math.h>
#include <assert.h>
#include <float.h>

//PyTypeObject pgShapeType;
//PyTypeObject pgRectShapeType;

//functions of pgShapeObject

void PG_ShapeObjectInit(pgShapeObject* shape)
{
	//TODO: maybe these init methods are not suitable. needing refined.
	memset(&(shape->box), 0, sizeof(shape->box));
	shape->Destroy = NULL;
	shape->Collision = NULL;
}

//pgShapeObject* _PG_ShapeNewInternal(PyTypeObject *type)
//{
//	pgShapeObject* op = (pgShapeObject*)type->tp_alloc(type, 0);
//	PG_ShapeObjectInit(op);
//	return op;
//}
//
void PG_ShapeObjectDestroy(pgShapeObject* shape)
{
	if(shape != NULL)
		shape->Destroy(shape);
	PyObject_Free(shape);
}
//
//PyObject* _pgShapeNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
//{
//	/* In case we have arguments in the python code, parse them later
//	* on.
//	*/
//	PyObject* shape;
//	if(PyType_Ready(type)==-1) return NULL;
//
//	shape = (PyObject*) _PG_ShapeNewInternal(type);
//	return shape;
//}
//
//static int _pgShape_init(pgShapeObject* shape,PyObject *args, PyObject *kwds)
//{
//	PG_ShapeObjectInit(shape);
//	return 0;
//}
//
//PyTypeObject pgShapeType =
//{
//	PyObject_HEAD_INIT(NULL)
//	0,
//	"physics.Shape",            /* tp_name */
//	sizeof(pgShapeObject),      /* tp_basicsize */
//	0,                          /* tp_itemsize */
//	(destructor)PG_ShapeObjectDestroy,/* tp_dealloc */
//	0,                          /* tp_print */
//	0,                          /* tp_getattr */
//	0,                          /* tp_setattr */
//	0,                          /* tp_compare */
//	0,                          /* tp_repr */
//	0,                          /* tp_as_number */
//	0,                          /* tp_as_sequence */
//	0,                          /* tp_as_mapping */
//	0,                          /* tp_hash */
//	0,                          /* tp_call */
//	0,                          /* tp_str */
//	0,                          /* tp_getattro */
//	0,                          /* tp_setattro */
//	0,                          /* tp_as_buffer */
//	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
//	"",                         /* tp_doc */
//	0,                          /* tp_traverse */
//	0,                          /* tp_clear */
//	0,                          /* tp_richcompare */
//	0,                          /* tp_weaklistoffset */
//	0,                          /* tp_iter */
//	0,                          /* tp_iternext */
//	0,		   	/* tp_methods */
//	0,            /* tp_members */
//	0,          /* tp_getset */
//	0,                          /* tp_base */
//	0,                          /* tp_dict */
//	0,                          /* tp_descr_get */
//	0,                          /* tp_descr_set */
//	0,                          /* tp_dictoffset */
//	_pgShape_init,                          /* tp_init */
//	0,                          /* tp_alloc */
//	_pgShapeNew,                /* tp_new */
//	0,                          /* tp_free */
//	0,                          /* tp_is_gc */
//	0,                          /* tp_bases */
//	0,                          /* tp_mro */
//	0,                          /* tp_cache */
//	0,                          /* tp_subclasses */
//	0,                          /* tp_weaklist */
//	0                           /* tp_del */
//};



//functions of pgRectShape

void PG_RectShapeUpdateAABB(pgBodyObject* body)
{
	int i;
	pgVector2 gp[4];

	if(body->shape->type == ST_RECT)
	{
		pgRectShape *p = (pgRectShape*)body->shape;
		
		PG_AABBClear(&(p->shape.box));
		for(i = 0; i < 4; ++i)
			gp[i] = PG_GetGlobalPos(body, &(p->point[i]));
		for(i = 0; i < 4; ++i)
			PG_AABBExpandTo(&(p->shape.box), &gp[i]);
	}
}

void PG_RectShapeDestroy(pgShapeObject* rectShape)
{
	PyObject_Free((pgRectShape*)rectShape);
}


int PG_RectShapeCollision(pgBodyObject* selfBody, pgBodyObject* incidBody, PyObject* contactList);

//just for C test usage, not for python???
pgShapeObject*	PG_RectShapeNew(pgBodyObject* body, double width, double height, double seta)
{
	int i;
	pgRectShape* p = (pgRectShape*)PyObject_MALLOC(sizeof(pgRectShape));
	//pgRectShape* p = PyObject_New(pgRectShape,&pgRectShapeType);

	//PG_ShapeObjectInit(&(p->shape));
	p->shape.Destroy = PG_RectShapeDestroy;
	p->shape.UpdateAABB = PG_RectShapeUpdateAABB;
	p->shape.Collision = PG_RectShapeCollision;
	p->shape.type = ST_RECT;
	p->shape.rInertia = body->fMass*(width*width + height*height)/12; // I = M(a^2 + b^2)/12

	PG_Set_Vector2(p->bottomLeft, -width/2, -height/2);
	PG_Set_Vector2(p->bottomRight, width/2, -height/2);
	PG_Set_Vector2(p->topRight, width/2, height/2);
	PG_Set_Vector2(p->topLeft, -width/2, height/2);
	for(i = 0; i < 4; ++i)
		c_rotate(&(p->point[i]), seta);
	
	return (pgShapeObject*)p;
}

//static int _pgRectShape_init(pgRectShape* joint,PyObject *args, PyObject *kwds)
//{
//	if(pgJointType.tp_init((PyObject*)joint, args, kwds) < 0)
//	{
//		return -1;
//	}
//
//	return 0;
//}

//-------------box's collision test------------------
//TEST: these functions have been partly tested.

//we use a simple SAT to select the contactNormal:
//Supposing the relative velocity between selfBody and incidBody in
//two frame is "small", the face(actually is an edge in 2D) with minimum 
//average penetrating depth is considered to be contact face, then we
//get the contact normal.
//note: this method is not available in CCD(continue collision detection)
//since the velocity is not small.
//static double _SAT_GetContactNormal1(pgAABBBox* clipBox, PyObject* contactList,
//								  int from, int to)
//{
//	int i;
//	int id;
//	double deps[4], min_dep;
//	pgContact* p;
//	pgVector2 normal;
//		
//	memset(deps, 0, sizeof(deps));
//	for(i = from; i <= to; ++i)
//	{
//		p = (pgContact*)PyList_GetItem(contactList, i);
//		deps[0] += p->pos.real - clipBox->left; //left
//		deps[1] += p->pos.imag - clipBox->bottom; //bottom
//		deps[2] += clipBox->right - p->pos.real; //right
//		deps[3] += clipBox->top - p->pos.imag; //top
//	}
//	
//	//find min penetrating face
//	id = 0;
//	min_dep = deps[0];
//	for(i = 1; i < 4; ++i)
//	{
//		if(min_dep > deps[i])
//		{
//			min_dep = deps[i];
//			id = i;
//		}
//	}
//	PG_Set_Vector2(normal, 0, 0);
//	//generate contactNormal
//	switch(id)
//	{
//	case 0://left
//		PG_Set_Vector2(normal, -1, 0);
//		break;
//	case 1://bottom
//		PG_Set_Vector2(normal, 0, -1);
//		break;
//	case 2://right
//		PG_Set_Vector2(normal, 1, 0);
//		break;
//	case 3://top
//		PG_Set_Vector2(normal, 0, 1);
//		break;
//	}
//
//    for(i = from; i <= to; ++i)
//    {
//        p = (pgContact*)PyList_GetItem(contactList, i);
//        p->normal = normal;
//    }
//
//	return min_dep;
//}
//
//#define _swap(a, b, t) {(t) = (a); (a) = (b); (b) = (t);}
//
////TODO: now just detect Box-Box collision, later add Box-Circle
//int PG_RectShapeCollision1(pgBodyObject* selfBody, pgBodyObject* incidBody, PyObject* contactList)
//{
//	int i, i1, k;
//	int from, to, _from[2], _to[2];
//	int apart;
//	pgVector2 ip[4];
//	int has_ip[4]; //use it to prevent from duplication
//	pgVector2 pf, pt;
//	pgRectShape *self, *incid, *tmp;
//	pgBodyObject * tmpBody;
//	pgAABBBox clipBox;
//	pgContact* contact;
//	pgVector2* pAcc;
//	PyObject* list[2];
//	double dist[2];
//
//	list[0] = PyList_New(0);
//	list[1] = PyList_New(0);
//
//	self = (pgRectShape*)selfBody->shape;
//	incid = (pgRectShape*)incidBody->shape;
//
//	apart = 1;	
//	for(k = 0; k < 2; ++k)
//	{
//		//transform incidBody's coordinate according to selfBody's coordinate
//		for(i = 0; i < 4; ++i)
//			ip[i] = PG_GetRelativePos(selfBody, incidBody, &(incid->point[i]));
//		//clip incidBody by selfBody
//		clipBox = PG_GenAABB(self->bottomLeft.real, self->topRight.real,
//			self->bottomLeft.imag, self->topRight.imag);
//		memset(has_ip, 0, sizeof(has_ip));
//		_from[k] = PyList_Size(list[k]);
//		
//		for(i = 0; i < 4; ++i)
//		{
//			i1 = (i+1)%4;
//			if(PG_LiangBarskey(&clipBox, &ip[i], &ip[i1], &pf, &pt))
//			{
//				apart = 0;
//				if(pf.real == ip[i].real && pf.imag == ip[i].imag)
//				{
//					has_ip[i] = 1;
//				}
//				else
//				{
//					contact = (pgContact*)PG_ContactNew(selfBody, incidBody);
//					contact->pos = pf;
//					PyList_Append(list[k], (PyObject*)contact);
//				}
//				
//				if(pt.real == ip[i1].real && pt.imag == ip[i1].imag)
//				{	
//					has_ip[i1] = 1;
//				}
//				else
//				{
//					contact = (pgContact*)PG_ContactNew(selfBody, incidBody);
//					contact->pos = pt;
//					PyList_Append(list[k], (PyObject*)contact);
//				}
//
//			}
//		}
//
//		if(apart)
//			goto END;
//
//		for(i = 0; i < 4; ++i)
//		{
//			if(has_ip[i])
//			{
//				contact = (pgContact*)PG_ContactNew(selfBody, incidBody);
//				contact->pos = ip[i];
//				PyList_Append(list[k], (PyObject*)contact);
//			}
//		}
//		//now all the contact points are added to list
//		_to[k] = PyList_Size(list[k]) - 1;
//		dist[k] = _SAT_GetContactNormal(&clipBox, list[k], _from[k], _to[k]);
//
//		//now swap refBody and incBody, and do it again
//		_swap(selfBody, incidBody, tmpBody);
//		_swap(self, incid, tmp);
//	}
//
//	from = PyList_Size(contactList);
//	for(i = 0; i < 2; ++i)
//	{
//		i1 = (i+1)%2;
//		if(dist[i] <= dist[i1])
//		{
//			for(k = _from[i]; k <= _to[i]; ++k)
//			{
//				contact = (pgContact*)PyList_GetItem(list[i], k);
//				PyList_Append(contactList, (PyObject*)contact);
//				Py_XINCREF((PyObject*)contact);
//			}
//			break;
//		}
//		_swap(selfBody, incidBody, tmpBody);
//	}
//	to = PyList_Size(contactList) - 1;
//
//	pAcc = PyObject_Malloc(sizeof(pgVector2));
//	pAcc->real = pAcc->imag = 0;
//	for(i = from; i <= to; ++i)
//	{
//		contact = (pgContact*)PyList_GetItem(contactList, i);
//
//		c_rotate(&(contact->pos), selfBody->fRotation);
//		contact->pos = c_sum(contact->pos, selfBody->vecPosition);
//		c_rotate(&(contact->normal), selfBody->fRotation);
//
//		contact->ppAccMoment = PyObject_Malloc(sizeof(pgVector2*));
//		*(contact->ppAccMoment) = pAcc;
//
//		contact->weight = (to - from)+1;
//	}
//
//
//END:
//	Py_XDECREF(list[0]);
//	Py_XDECREF(list[1]);
//	return 1;
//}


////all the points are in refbox's locate coordinate system
//static double _SAT_GetContactNormal(pgAABBBox* clipBox, pgVector2* clist, 
//									int csize, pgVector2* normal)
//{	
//	int i;
//	int id;
//	double deps[4], min_dep;
//	pgVector2* p;
//		
//	memset(deps, 0, sizeof(deps));
//	for(i = 0; i < csize; ++i)
//	{
//		p = &clist[i];
//		deps[0] += fabs(p->real - clipBox->left); //left
//		deps[1] += fabs(p->imag - clipBox->bottom); //bottom
//		deps[2] += fabs(clipBox->right - p->real); //right
//		deps[3] += fabs(clipBox->top - p->imag); //top
//	}
//	
//	//find min penetrating face
//	id = 0;
//	min_dep = deps[0];
//	for(i = 1; i < 4; ++i)
//	{
//		if(min_dep > deps[i])
//		{
//			min_dep = deps[i];
//			id = i;
//		}
//	}
//	PG_Set_Vector2(*normal, 0, 0);
//	//generate contactNormal
//	switch(id)
//	{
//	case 0://left
//		PG_Set_Vector2(*normal, -1, 0);
//		break;
//	case 1://bottom
//		PG_Set_Vector2(*normal, 0, -1);
//		break;
//	case 2://right
//		PG_Set_Vector2(*normal, 1, 0);
//		break;
//	case 3://top
//		PG_Set_Vector2(*normal, 0, 1);
//		break;
//	default:
//		assert(0);
//		break;
//	}
//
//	return min_dep;
//}
//
//
//static int _PG_RectShapeCollision(pgBodyObject* selfBody, pgBodyObject* incidBody,
//								  pgVector2* clist, int* csize, pgVector2 *normal, double* depth)
//{
//	pgRectShape * self, * incid;
//	pgVector2 gp[4], ans_p1, ans_p2;
//	int has_ip[4];
//	pgAABBBox clipBox;
//	int i, i1;
//	int axis;
//	int valid_p1, valid_p2;
//	pgVector2 cpoints[2][10], n[2];
//	int csizes[2];
//	double dep[2];
//	int id;
//	int apart[2];
//
//	self = (pgRectShape*)selfBody->shape;
//	incid = (pgRectShape*)incidBody->shape;
//
//	clipBox = PG_GenAABB(self->bottomLeft.real, self->topRight.real,
//		self->bottomLeft.imag, self->topRight.imag);
//
//	for(i = 0; i < 4; ++i)
//		gp[i] = PG_GetRelativePos(selfBody, incidBody, &(incid->point[i]));
//	
//	for(axis = CA_X; axis <= CA_Y; ++axis)
//	{
//		memset(has_ip, 0, sizeof(has_ip));
//		apart[axis] = 1;
//		csizes[axis] = 0;
//		for(i = 0; i < 4; ++i)
//		{
//			i1 = (i+1)%4;
//			if(PG_PartlyLB(&clipBox, &gp[i], &gp[i1], axis, 
//				&ans_p1, &ans_p2, &valid_p1, &valid_p2))
//			{
//				apart[axis] = 0;
//				if(valid_p1)
//				{
//					if(c_equal(&ans_p1, &gp[i]))
//						has_ip[i] = 1;
//					else
//						cpoints[axis][csizes[axis]++] = ans_p1;
//				}
//				if(valid_p2)
//				{
//					if(c_equal(&ans_p2, &gp[i1]))
//						has_ip[i1] = 1;
//					else
//						cpoints[axis][csizes[axis]++] = ans_p2;
//				}
//			}
//		}
//
//		for(i = 0; i < 4; ++i)
//			if(has_ip[i])
//				cpoints[axis][csizes[axis]++] = gp[i];
//		dep[axis] = _SAT_GetContactNormal(&clipBox, cpoints[axis], csizes[axis], &n[axis]);
//	}
//
//	if(apart[CA_X] && apart[CA_Y]) return 0;
//	//assert(csizes[0] > 0 && csizes[1] > 0);
//	
//	id = dep[CA_X] < dep[CA_Y] ? CA_X : CA_Y;
//	if(csizes[id] == 0)
//		id = (id + 1)%2;
//	*csize = csizes[id];
//	for(i = 0; i < csizes[id]; ++i)
//		clist[i] = cpoints[id][i];
//	*normal = n[id];
//	*depth = dep[id];
//
//	return 1;
//}
//
//int PG_RectShapeCollision(pgBodyObject* selfBody, pgBodyObject* incidBody, 
//						  PyObject* contactList)
//{
//	pgVector2 clist1[10], clist2[10], *clist, n1, n2, *n;
//	int csize1, csize2, *csize;
//	double dep1, dep2;
//	int is_collided;
//	pgBodyObject *self, *incid;
//	int i;
//	pgContact* contact;
//	pgVector2* pAcc;
//
//	is_collided = _PG_RectShapeCollision(selfBody, incidBody, clist1, 
//				  &csize1, &n1, &dep1);
//	if(!is_collided) return 0;
//	is_collided = _PG_RectShapeCollision(incidBody, selfBody, clist2, 
//				  &csize2, &n2, &dep2);
//	assert(is_collided);
//
//	if(dep1 < dep2)
//	{
//		clist = clist1;
//		csize = &csize1;
//		n = &n1;
//		self = selfBody;
//		incid = incidBody;
//	}
//	else
//	{
//		clist = clist2;
//		csize = &csize2;
//		n = &n2;
//		self = incidBody;
//		incid = selfBody;
//	}
//	assert(*csize > 0);
//
//
//	pAcc = PyObject_Malloc(sizeof(pgVector2));
//	pAcc->real = pAcc->imag = 0;
//	for(i = 0; i < *csize; ++i)
//	{
//		contact = (pgContact*)PG_ContactNew(self, incid);
//		contact->pos = clist[i];
//		contact->normal = *n;
//		c_rotate(&(contact->pos), self->fRotation);
//		contact->pos = c_sum(contact->pos, self->vecPosition);
//		c_rotate(&(contact->normal), self->fRotation);
//
//		contact->ppAccMoment = PyObject_Malloc(sizeof(pgVector2*));
//		*(contact->ppAccMoment) = pAcc;
//		contact->weight = *csize;
//		PyList_Append(contactList, (PyObject*)contact);
//	}
//
//	return 1;
//}


static int _Get_Depth(pgBodyObject* refBody, pgBodyObject* incBody,
					   int* faceId, double* min_dep, pgVector2* gp_in_ref, 
					   pgAABBBox* clipBox)
{
	int i, apart;
	pgRectShape *ref, *inc;
	double deps[4];

	ref = (pgRectShape*)refBody->shape;
	inc = (pgRectShape*)incBody->shape;
	memset(gp_in_ref, 0, sizeof(gp_in_ref));
	memset(deps, 0, sizeof(deps));
	for(i = 0; i < 4; ++i)
		gp_in_ref[i] = PG_GetRelativePos(refBody, incBody, &(inc->point[i]));

	*clipBox = PG_GenAABB(ref->bottomLeft.real, ref->topRight.real,
		ref->bottomLeft.imag, ref->topRight.imag);

	apart = 1;
	for(i = 0; i < 4; ++i)
		if(PG_IsIn(&gp_in_ref[i], clipBox))
		{
			apart = 0;
			deps[CF_LEFT] += fabs(gp_in_ref[i].real - clipBox->left);
			deps[CF_RIGHT] += fabs(clipBox->right - gp_in_ref[i].real);
			deps[CF_BOTTOM] += fabs(gp_in_ref[i].imag - clipBox->bottom);
			deps[CF_TOP] += fabs(clipBox->top - gp_in_ref[i].imag);
		}

	if(apart) return 0;

	*min_dep = deps[0];
	*faceId = 0;

	for(i = 1; i < 4; ++i)
		if(deps[i] < *min_dep)
		{
			*min_dep = deps[i];
			*faceId = i;
		}

	return 1;
}

static int _SAT_Select(pgBodyObject* body1, pgBodyObject* body2,
					   pgBodyObject** refBody, pgBodyObject** incBody,
					   int* face_id, pgVector2* gp_in_ref, pgAABBBox* clipBox,
					   double* minDep)
{
	double min_dep[2];
	int id[2];
	pgVector2 gp[2][4];
	pgAABBBox cb[2];
	int is_in[2];
	int i;
	
	min_dep[0] = min_dep[1] = DBL_MAX;
	is_in[0] = _Get_Depth(body1, body2, &id[0], &min_dep[0], gp[0], &cb[0]);
	is_in[1] = _Get_Depth(body2, body1, &id[1], &min_dep[1], gp[1], &cb[1]);

	if(!is_in[0] && !is_in[1]) return 0;

	if(min_dep[0] < min_dep[1])
	{
		*refBody = body1;
		*incBody = body2;
		*face_id = id[0];
		for(i = 0; i < 4; ++i)
			gp_in_ref[i] = gp[0][i];
		*clipBox = cb[0];
		*minDep = min_dep[0];
	}
	else
	{
		*refBody = body2;
		*incBody = body1;
		*face_id = id[1];
		for(i = 0; i < 4; ++i)
			gp_in_ref[i] = gp[1][i];
		*clipBox = cb[1];
		*minDep = min_dep[1];
	}

	return 1;
}

int _Build_Contacts(pgVector2* gp, pgAABBBox* clipBox, int axis,
					 pgVector2 contacts[], int* size)
{
	int i, i1;
	int apart = 1;
	int has_ip[4];
	pgVector2 pf, pt;
	int valid_pf, valid_pt;

	*size = 0;
	memset(has_ip, 0, sizeof(has_ip));
	for(i = 0; i < 4; ++i)
	{
		i1 = (i+1)%4;
		if(PG_PartlyLB(clipBox, &gp[i], &gp[i1], axis, 
			&pf, &pt, &valid_pf, &valid_pt))
		{
			apart = 0;
			if(valid_pf)
			{
				if(c_equal(&pf, &gp[i]))
					has_ip[i] = 1;
				else
					contacts[(*size)++] = pf;
			}
			if(valid_pt)
			{
				if(c_equal(&pt, &gp[i1]))
					has_ip[i1] = 1;
				else
					contacts[(*size)++] = pt;
			}
		}
	}
	for(i = 0; i < 4; ++i)
		if(has_ip[i])
			contacts[(*size)++] = gp[i];

	return !apart;
}

int PG_RectShapeCollision(pgBodyObject* selfBody, pgBodyObject* incidBody, 
						  PyObject* contactList)
{
#define MAX_CONTACTS 8

	pgBodyObject* ref = NULL, *inc = NULL;
	int face_id;
	pgVector2 gp[4];
	pgVector2 contacts[MAX_CONTACTS];
	int csize;
	pgVector2 normal;
	pgAABBBox clipBox;
	int overlap;
	pgVector2* pAcc, * pSplitAcc;
	pgContact* contact;
	int i;
	double minDep;


	overlap = _SAT_Select(selfBody, incidBody,
						  &ref, &inc,
						  &face_id, gp, &clipBox, &minDep);

	if(!overlap) return 0;

	switch(face_id)
	{
	case CF_LEFT:
		PG_Set_Vector2(normal, -1, 0);
		assert(_Build_Contacts(gp, &clipBox, CA_X, contacts, &csize)); 
		break;
	case CF_BOTTOM:
		PG_Set_Vector2(normal, 0, -1);
		assert(_Build_Contacts(gp, &clipBox, CA_Y, contacts, &csize));
		break;
	case CF_RIGHT:
		PG_Set_Vector2(normal, 1, 0);
		assert(_Build_Contacts(gp, &clipBox, CA_X, contacts, &csize));
		break;
	case CF_TOP:
		PG_Set_Vector2(normal, 0, 1);
		assert(_Build_Contacts(gp, &clipBox, CA_Y, contacts, &csize));
		break;
	default:
		assert(0);
		break;
	}

	pAcc = PyObject_Malloc(sizeof(pgVector2));
	pAcc->real = pAcc->imag = 0;
	pSplitAcc = PyObject_Malloc(sizeof(pgVector2));
	pSplitAcc->real = pSplitAcc->imag = 0;
	for(i = 0; i < csize; ++i)
	{
		contact = (pgContact*)PG_ContactNew(ref, inc);
		contact->pos = contacts[i];
		contact->normal = normal;
		c_rotate(&(contact->pos), ref->fRotation);
		contact->pos = c_sum(contact->pos, ref->vecPosition);
		c_rotate(&(contact->normal), ref->fRotation);

		contact->ppAccMoment = PyObject_Malloc(sizeof(pgVector2*));
		*(contact->ppAccMoment) = pAcc;
		contact->ppSplitAccMoment = PyObject_Malloc(sizeof(pgVector2*));
		*(contact->ppSplitAccMoment) = pSplitAcc;

		contact->weight = csize;
		contact->depth = minDep;
		PyList_Append(contactList, (PyObject*)contact);
	}

	return 1;

}


//PyTypeObject pgRectShapeType =
//{
//	PyObject_HEAD_INIT(NULL)
//	0,
//	"physics.RectShape",            /* tp_name */
//	sizeof(pgRectShape),      /* tp_basicsize */
//	0,                          /* tp_itemsize */
//	(destructor) 0,				/* tp_dealloc */
//	0,                          /* tp_print */
//	0,                          /* tp_getattr */
//	0,                          /* tp_setattr */
//	0,                          /* tp_compare */
//	0,                          /* tp_repr */
//	0,                          /* tp_as_number */
//	0,                          /* tp_as_sequence */
//	0,                          /* tp_as_mapping */
//	0,                          /* tp_hash */
//	0,                          /* tp_call */
//	0,                          /* tp_str */
//	0,                          /* tp_getattro */
//	0,                          /* tp_setattro */
//	0,                          /* tp_as_buffer */
//	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
//	"",                         /* tp_doc */
//	0,                          /* tp_traverse */
//	0,                          /* tp_clear */
//	0,                          /* tp_richcompare */
//	0,                          /* tp_weaklistoffset */
//	0,                          /* tp_iter */
//	0,                          /* tp_iternext */
//	0,							/* tp_methods */
//	0,	/* tp_members */
//	0,							/* tp_getset */
//	0,							/* tp_base */
//	0,                          /* tp_dict */
//	0,                          /* tp_descr_get */
//	0,                          /* tp_descr_set */
//	0,                          /* tp_dictoffset */
//	(initproc)_pgRectShape_init,  /* tp_init */
//	0,                          /* tp_alloc */
//	0,							/* tp_new */
//	0,                          /* tp_free */
//	0,                          /* tp_is_gc */
//	0,                          /* tp_bases */
//	0,                          /* tp_mro */
//	0,                          /* tp_cache */
//	0,                          /* tp_subclasses */
//	0,                          /* tp_weaklist */
//	0                           /* tp_del */
//};