Source

pygame / src / pgCollision.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
#include "pgCollision.h"
#include "pgAABBBox.h"
#include "pgShapeObject.h"
#include "pgBodyObject.h"
#include "glTestSuite.h"
#include <assert.h>
#include <GL/glut.h>

extern PyTypeObject pgContactType;

// We borrow this graph from Box2DLite
// Box vertex and edge numbering:
//
//        ^ y
//        |
//        e3
//   v3 ----- v2
//    |        |
// e0 |        | e2  --> x
//    |        |
//   v0 ----- v1
//        e1


//TODO: add rest contact


// Apply Liang-Barskey clip on a AABB box
// (p1, p2) is the input line segment to be clipped (note: it's a 2d vector)
// (ans_p1, ans_p2) is the output line segment
//TEST: Liang-Barskey clip has been tested.
int _LiangBarskey_Internal(double p, double q, double* u1, double* u2)
{
	double val;

	if(is_zero(p))
	{
		if(q < 0)
			return 0;
		
		return 1;
	}
	
	val = q/p;
	
	if(p < 0)
		*u1 = MAX(*u1, val);
	else
		*u2 = MIN(*u2, val);

	return 1; 
}

int PG_LiangBarskey(pgAABBBox* box, pgVector2* p1, pgVector2* p2, 
					 pgVector2* ans_p1, pgVector2* ans_p2)
{
	pgVector2 dp;
	double u1, u2;
	

	u1 = 0.f;
	u2 = 1.f;
	dp = c_diff(*p2, *p1);	//dp = p2 - p1

	if(!_LiangBarskey_Internal(-dp.real, p1->real - box->left, &u1, &u2)) return 0;
	if(!_LiangBarskey_Internal(dp.real, box->right - p1->real, &u1, &u2)) return 0;
	if(!_LiangBarskey_Internal(-dp.imag, p1->imag - box->bottom, &u1, &u2)) return 0;
	if(!_LiangBarskey_Internal(dp.imag, box->top - p1->imag, &u1, &u2)) return 0;

	if(u1 > u2) return 0;

	if(u1 == 0.f)
		*ans_p1 = *p1;
	else
		*ans_p1 = c_sum(*p1, c_mul_complex_with_real(dp, u1)); //ans_p1 = p1 + u1*dp
	if(u2 == 1.f)
		*ans_p2 = *p2;
	else
		*ans_p2 = c_sum(*p1, c_mul_complex_with_real(dp, u2)); //ans_p2 = p2 + u2*dp;

	return 1;
}

int PG_PartlyLB(pgAABBBox* box, pgVector2* p1, pgVector2* p2, 
				pgCollisionAxis axis, pgVector2* ans_p1, pgVector2* ans_p2, 
				int* valid_p1, int* valid_p2)
{
	pgVector2 dp;
	double u1, u2;
	
	u1 = 0.f;
	u2 = 1.f;
	dp = c_diff(*p2, *p1);

	switch(axis)
	{
	case CA_X:
		if(!_LiangBarskey_Internal(-dp.imag, p1->imag - box->bottom, &u1, &u2)) return 0;
		if(!_LiangBarskey_Internal(dp.imag, box->top - p1->imag, &u1, &u2)) return 0;
		break;
	case CA_Y:
		if(!_LiangBarskey_Internal(-dp.real, p1->real - box->left, &u1, &u2)) return 0;
		if(!_LiangBarskey_Internal(dp.real, box->right - p1->real, &u1, &u2)) return 0;
		break;
	default:
		assert(0);
		break;
	}

	if(u1 > u2) return 0;

	if(u1 == 0.f)
		*ans_p1 = *p1;
	else
		*ans_p1 = c_sum(*p1, c_mul_complex_with_real(dp, u1)); //ans_p1 = p1 + u1*dp
	if(u2 == 1.f)
		*ans_p2 = *p2;
	else
		*ans_p2 = c_sum(*p1, c_mul_complex_with_real(dp, u2)); //ans_p2 = p2 + u2*dp;

	switch(axis)
	{
	case CA_X:
		*valid_p1 = less_equal(box->left, ans_p1->real) && 
					less_equal(ans_p1->real, box->right);
		*valid_p2 = less_equal(box->left, ans_p2->real) && 
					less_equal(ans_p2->real, box->right);
		break;
	case CA_Y:
		*valid_p1 = less_equal(box->bottom, ans_p1->imag) && 
					less_equal(ans_p1->imag, box->top);
		*valid_p2 = less_equal(box->bottom, ans_p2->imag) && 
					less_equal(ans_p2->imag, box->top);
		break;
	default:
		assert(0);
		break;
	}

	return *valid_p1 || *valid_p2;
}


void PG_AppendContact(pgBodyObject* refBody, pgBodyObject* incidBody, PyObject* contactList)
{
	refBody->shape->Collision(refBody, incidBody, contactList);
}

void PG_ApplyContact(PyObject* contactObject, double step)
{
#define MAX_C_DEP 0.02
#define BIAS_FACTOR 0.15

	pgVector2 neg_dV, refV, incidV;
	pgVector2 refR, incidR;
	pgContact *contact;
	pgBodyObject *refBody, *incidBody;
	double k, tmp1, tmp2;
	double moment_len;
	pgVector2 moment;
	pgVector2* p;

	double vbias;
	pgVector2 brefV, bincidV, bneg_dV;
	double bm_len;
	pgVector2 bm;

	contact = (pgContact*)contactObject;
	refBody = contact->joint.body1;
	incidBody = contact->joint.body2;

	contact->resist = sqrtf(refBody->fRestitution*incidBody->fRestitution);

	//calculate the normal impulse
	//k
	refR = c_diff(contact->pos, refBody->vecPosition);
	incidR = c_diff(contact->pos, incidBody->vecPosition);

	tmp1 = c_dot(c_fcross(c_cross(refR, contact->normal), refR), contact->normal)
		 /refBody->shape->rInertia;
	tmp2 = c_dot(c_fcross(c_cross(incidR, contact->normal), incidR), contact->normal)
		/incidBody->shape->rInertia;

	k = 1/refBody->fMass + 1/incidBody->fMass + tmp1 + tmp2;
	
	//dV = v2 + w2xr2 - (v1 + w1xr1)
	incidV = c_sum(incidBody->vecLinearVelocity, c_fcross(incidBody->fAngleVelocity,
		           incidR));
	refV = c_sum(refBody->vecLinearVelocity, c_fcross(refBody->fAngleVelocity,
		         refR));

	contact->dv = c_diff(incidV, refV);
	neg_dV = c_diff(refV, incidV);
	
	moment_len = c_dot(c_mul_complex_with_real(neg_dV, (1 + contact->resist)), 
		               contact->normal)/k;
	moment_len = MAX(0, moment_len);
	//moment_len = MAX(0, moment_len + vbias/k);
	
	//finally we get the momentum(oh...)
	moment = c_mul_complex_with_real(contact->normal, moment_len);
	p = *(contact->ppAccMoment);
	//TODO: test weight
	p->real += moment.real/contact->weight;
	p->imag += moment.imag/contact->weight; 

	//split impulse
	vbias = BIAS_FACTOR*MAX(0, contact->depth - MAX_C_DEP)/step;
	//biasdv
	bincidV = c_sum(incidBody->cBiasLV, c_fcross(incidBody->cBiasW, incidR));
	brefV = c_sum(refBody->cBiasLV, c_fcross(refBody->cBiasW, refR));
	bneg_dV = c_diff(brefV, bincidV); 
	//bias_moment
	bm_len = c_dot(c_mul_complex_with_real(bneg_dV, (1)),
		contact->normal)/k;
	bm_len = MAX(0, bm_len + vbias/k);
	bm = c_mul_complex_with_real(contact->normal, bm_len);
	p = *(contact->ppSplitAccMoment);
	p->real += bm.real/contact->weight;
	p->imag += bm.imag/contact->weight;

}

void PG_UpdateV(pgJointObject* joint, double step)
{
	pgContact *contact;
	pgBodyObject *refBody, *incidBody;
	pgVector2 moment, bm;
	pgVector2 refR, incidR;

	contact = (pgContact*)joint;
	refBody = joint->body1;
	incidBody = joint->body2;
	moment = **(contact->ppAccMoment);
	bm = **(contact->ppSplitAccMoment);

	refR = c_diff(contact->pos, refBody->vecPosition);
	incidR = c_diff(contact->pos, incidBody->vecPosition);

	if(c_dot(contact->dv, contact->normal) > 0) return;
	glPointSize(6.f);
	glColor3f(1.f, 0.f, 0.f);
	glBegin(GL_POINTS);
	glVertex2d(contact->pos.real, contact->pos.imag);
	glEnd();
	glPointSize(1.f);

	if(!refBody->bStatic)
	{
		refBody->vecLinearVelocity = c_diff(refBody->vecLinearVelocity, 
			c_div_complex_with_real(moment, refBody->fMass));
		refBody->fAngleVelocity -= c_cross(refR, moment)/refBody->shape->rInertia;

		refBody->cBiasLV = c_diff(refBody->cBiasLV,
			c_div_complex_with_real(bm, refBody->fMass));
		refBody->cBiasW -= c_cross(refR, bm)/refBody->shape->rInertia;
	}

	if(!incidBody->bStatic)
	{
		incidBody->vecLinearVelocity = c_sum(incidBody->vecLinearVelocity, 
			c_div_complex_with_real(moment, incidBody->fMass));
		incidBody->fAngleVelocity += c_cross(incidR, moment)/incidBody->shape->rInertia;

		incidBody->cBiasLV = c_sum(incidBody->cBiasLV,
			c_div_complex_with_real(bm, incidBody->fMass));
		incidBody->cBiasW += c_cross(incidR, bm)/incidBody->shape->rInertia;
	}
}

void PG_UpdateP(pgJointObject* joint, double step)
{

	pgVector2 v;
	double w;

	//TODO: concern dt
	//if(!joint->body1->bStatic)
	//{
	//	//v = joint->body1->vecLinearVelocity;
	//	//w = joint->body1->fAngleVelocity;

	//	v = c_sum(joint->body1->vecLinearVelocity, joint->body1->cBiasLV);
	//	w = joint->body1->fAngleVelocity + joint->body1->cBiasW;

	//	joint->body1->vecPosition = c_sum(joint->body1->vecPosition, 
	//		c_mul_complex_with_real(v, step));
	//	joint->body1->fRotation += w*step;
	//}

	//if(!joint->body2->bStatic)
	//{
	//	//v = joint->body1->vecLinearVelocity;
	//	//w = joint->body1->fAngleVelocity;

	//	v = c_sum(joint->body2->vecLinearVelocity, joint->body2->cBiasLV);
	//	w = joint->body2->fAngleVelocity + joint->body2->cBiasW;

	//	joint->body2->vecPosition = c_sum(joint->body2->vecPosition, 
	//		c_mul_complex_with_real(v, step));
	//	joint->body2->fRotation += w*step;
	//}
}

void PG_ContactDestroy(pgJointObject* contact)
{
	pgVector2 **p = ((pgContact*)contact)->ppAccMoment;
	if(p)
	{
		if(*p)
		{
			PyObject_Free(*p);
			*p = NULL;
		}
		PyObject_Free(p);
		p = NULL;
	}

	p = ((pgContact*)contact)->ppSplitAccMoment;
	if(p)
	{
		if(*p)
		{
			PyObject_Free(*p);
			*p = NULL;
		}
		PyObject_Free(p);
		p = NULL;
	}
}

PyObject* _PG_ContactNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
	pgContact* op;
	type->tp_base = &pgJointType;
	if(PyType_Ready(type) < 0) return NULL;
	op = (pgContact*)type->tp_alloc(type, 0);
	return (PyObject*)op;
}

pgJointObject* PG_ContactNew(pgBodyObject* refBody, pgBodyObject* incidBody)
{
	pgContact* contact;
	//TODO: this function would be replaced.
	contact = (pgContact*)_PG_ContactNew(&pgContactType, NULL, NULL);
	contact->joint.body1 = refBody;
	contact->joint.body2 = incidBody;
	contact->joint.SolveConstraintPosition = PG_UpdateP;
	contact->joint.SolveConstraintVelocity = PG_UpdateV;
	contact->joint.Destroy = PG_ContactDestroy;

	contact->ppAccMoment = NULL;
	contact->ppSplitAccMoment = NULL;

	return (pgJointObject*)contact;
}

PyTypeObject pgContactType =
{
	PyObject_HEAD_INIT(NULL)
	0,
	"physics.Contact",			/* tp_name */
	sizeof(pgContact),			/* tp_basicsize */
	0,                          /* tp_itemsize */
	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 */
	0,							/* tp_init */
	0,							/* tp_alloc */
	_PG_ContactNew,				/* 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 */
};