Commits

Anonymous committed 81dfa73

resovle some bugs.

  • Participants
  • Parent commits 34361b4
  • Branches physics

Comments (0)

Files changed (4)

 void do_render()
 {
 	glColor3f(1.f, 1.f, 1.f);
-	PG_Update(s_world, 0.005);
+	PG_Update(s_world, 0.02);
 	PGT_RenderWorld(s_world);
 	//glprintf(0, 0, "Velocity of body: (%.2f, %.2f)", body->vecLinearVelocity.real, 
 	//	body->vecLinearVelocity.imag);
 	int i = 0;
 	s_world = PG_WorldNew();
 	s_world->fStepTime = 0.03;
+	PG_Set_Vector2(s_world->vecGravity, 0, -600.f); //Orz重力居然要-10000.f才像话
 
-	for(i = 0; i < 3; ++i)
+	for(i = 0; i < 7; ++i)
 	{
 		body = PG_BodyNew();
 		PG_Set_Vector2(body->vecPosition, -100+100*i, 200);
-		PG_Set_Vector2(body->vecLinearVelocity, (100 - 100)/2, 0.f);
-		body->fRotation = .8f;
-		body->fAngleVelocity = 30.f;
-		body->fRestitution = 0.f;
-		PG_Bind_RectShape(body, 50, 30, 0);
+		PG_Set_Vector2(body->vecLinearVelocity, (20 - 10*i)/2, 0.f);
+		body->fRotation = 0;
+		body->fAngleVelocity = 3.f;
+		body->fRestitution = 0.0f;
+		body->fMass = 30.f;
+		PG_Bind_RectShape(body, 40, 25, 0);
 		PG_AddBodyToWorld(s_world, body);
 	}
 
 	body1->bStatic = 1;
 	body1->fRestitution = 1.f;//for test
 	body1->fMass = 1e24;
-	PG_Bind_RectShape(body1, 10000, 20, 0);
+	PG_Bind_RectShape(body1, 1000, 20, 0);
 	PG_AddBodyToWorld(s_world, body1);
 
 }

src/pgBodyObject.c

 
 void PG_FreeUpdateBodyPos(pgWorldObject* world,pgBodyObject* body,double dt)
 {
-	pgVector2 totalPosAdd;
+	pgVector2 v;
+	double w;
 
 	if(body->bStatic) return;
-
-	totalPosAdd = c_mul_complex_with_real(body->vecLinearVelocity, dt);
-	body->vecPosition = c_sum(body->vecPosition,totalPosAdd);
-	body->fRotation += body->fAngleVelocity*dt;
+	
+	v = c_sum(body->vecLinearVelocity, body->cBiasLV);
+	w = body->fAngleVelocity + body->cBiasW;
+	body->vecPosition = c_sum(body->vecPosition, 
+		c_mul_complex_with_real(v, dt));
+	body->fRotation += w*dt;
 }
 
 void PG_BodyInit(pgBodyObject* body)

src/pgCollision.c

 
 void PG_ApplyContact(PyObject* contactObject, double step)
 {
-#define MAX_C_DEP 0.01
-#define BIAS_FACTOR 0.1
+#define MAX_C_DEP 0.02
+#define BIAS_FACTOR 0.05
 
 	pgVector2 neg_dV, refV, incidV;
 	pgVector2 refR, 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->resist)),
+	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);
 	double w;
 
 	//TODO: concern dt
-	if(!joint->body1->bStatic)
-	{
-		v = c_sum(joint->body1->vecLinearVelocity, joint->body1->cBiasLV);
-		w = joint->body1->fAngleVelocity + joint->body1->cBiasW;
+	//if(!joint->body1->bStatic)
+	//{
+	//	//v = joint->body1->vecLinearVelocity;
+	//	//w = joint->body1->fAngleVelocity;
 
-		joint->body1->vecPosition = c_sum(joint->body1->vecPosition, 
-			c_mul_complex_with_real(v, step));
-		joint->body1->fRotation += w*step;
-	}
+	//	v = c_sum(joint->body1->vecLinearVelocity, joint->body1->cBiasLV);
+	//	w = joint->body1->fAngleVelocity + joint->body1->cBiasW;
 
-	if(!joint->body2->bStatic)
-	{
-		v = c_sum(joint->body2->vecLinearVelocity, joint->body2->cBiasLV);
-		w = joint->body2->fAngleVelocity + joint->body2->cBiasW;
+	//	joint->body1->vecPosition = c_sum(joint->body1->vecPosition, 
+	//		c_mul_complex_with_real(v, step));
+	//	joint->body1->fRotation += w*step;
+	//}
 
-		joint->body2->vecPosition = c_sum(joint->body2->vecPosition, 
-			c_mul_complex_with_real(v, step));
-		joint->body2->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)

src/pgWorldObject.c

 			contact = (pgJointObject*)(PyList_GetItem((PyObject*)(world->contactList), i));
 			contact->SolveConstraintVelocity(contact, step);
 		}
-	//update P
-	for(i = 0; i < cnt; ++i)
-	{
-		contact = (pgJointObject*)(PyList_GetItem((PyObject*)(world->contactList), i));
-		contact->SolveConstraintPosition(contact, step);
-	}
-	
 }
 
 void _PG_JointSolve(pgWorldObject* world,double stepTime)
 	for (i = 0; i < size; ++i)
 	{
 		pgJointObject* joint = (pgJointObject*)(PyList_GetItem((PyObject*)(world->jointList),i));
+		//what happened here?
 		if (joint->SolveConstraintPosition)
 		{
 			joint->SolveConstraintPosition(joint,stepTime);
 	for (i = 0; i < size; ++i)
 	{
 		pgBodyObject* body = (pgBodyObject*)(PyList_GetItem((PyObject*)(world->bodyList),i));
-		PG_FreeUpdateBodyPos(world,body,stepTime);
-		
+		PG_FreeUpdateBodyPos(world,body,stepTime);	
 	}
 }
 
 void PG_Update(pgWorldObject* world,double stepTime)
 {
 	int i;
-	pgBodyObject* refBody;
 	_PG_FreeBodySimulation(world, stepTime);
 
 	_PG_BodyCollisionDetection(world, stepTime);
 	}
 	
 	_PG_BodyPositionUpdate(world, stepTime);
-	refBody = (pgBodyObject*)(PyList_GetItem((PyObject*)(world->bodyList), 0));
 }