Commits

Anonymous committed fc26bde

  • Participants
  • Parent commits 8cc23d4
  • Branches physics

Comments (0)

Files changed (3)

 
 void TestBasic4Init()
 {
-#define  BODY_NUM  3
+#define  BODY_NUM  1
 
 	int i;
 	pgBodyObject* body[BODY_NUM + 1];
 
 	s_world = PG_WorldNew();
 	s_world->fStepTime = 0.03;
-	PG_Set_Vector2(s_world->vecGravity,0,-100)
+	PG_Set_Vector2(s_world->vecGravity,0,0)
 
 	body[0] = NULL;
 	for (i = 1;i < BODY_NUM + 1;i++)
 	pgBodyObject* body[2];
 	pgJointObject* joint;
 	pgVector2 a1,a2;
-	PG_Set_Vector2(a1,10,0);
-	PG_Set_Vector2(a2,10,0);
+	PG_Set_Vector2(a1,0,0);
+	PG_Set_Vector2(a2,0,0);
 	s_world = PG_WorldNew();
 	s_world->fStepTime = 0.03;
 	PG_Set_Vector2(s_world->vecGravity,0,0)
 	PG_Set_Vector2(body[1]->vecPosition,50,0)
 	PG_AddBodyToWorld(s_world,body[1]);
 
-	PG_Set_Vector2(body[0]->vecLinearVelocity,0,100)
+	PG_Set_Vector2(body[0]->vecLinearVelocity,0,0)
 	PG_Set_Vector2(body[1]->vecLinearVelocity,0,0)
 
 	joint = PG_DistanceJointNew(body[0],body[1],0,100,a1,a2);
 
 void InitWorld()
 {
-	TestBasic5Init();
+	TestBasic6Init();
 }
 
 int main (int argc, char** argv)

File src/pgJointObject.c

-include "pgJointObject.h"
+#include "pgJointObject.h"
 #include "pgShapeObject.h"
 #include "pgHelpFunctions.h"
 #include <structmember.h>
 
 	localP = c_diff(localP,body->vecPosition);
 	k = body->shape->rInertia / body->fMass;
-	k *= 2;
+	k*=2;
 	k += c_get_length_square(localP);
 	bb = (distance - c_get_length(L));
 	c_normalize(&L);
 	dAngleV /= k;
 	body->fAngleVelocity += dAngleV;
 
+	//printf("dvBody %f,%f\n",dvBody.real,dvBody.imag);
+	if (c_get_length(body->vecLinearVelocity) < 100)
+	{
+		printf("joint\n");
+		printf("body speed:%f,%f\n",body->vecLinearVelocity.real,body->vecLinearVelocity.imag);
+	}
 
 	//for position correction
 	
-	temp = -bb /a;
+	/*temp = -bb /a;
 	dvBody = c_mul_complex_with_real(L,temp);
 	dAngleV = c_cross(localP,dvBody);
 	dAngleV /= k;
 	body->vecPosition = c_sum(body->vecPosition,dvBody);
-	body->fRotation += dAngleV;
+	body->fRotation += dAngleV;*/
 
 	/*temp = c_get_length(c_diff(*staticAnchor,PG_GetGlobalPos(body,localAnchor)));
 	temp -= distance; 
 	pgVector2 dvBody1,dvBody2;
 	double dAngleV1,dAngleV2;
 	k1 = body1->shape->rInertia / body1->fMass;
-	k1 *= 2;
 	k2 = body2->shape->rInertia / body2->fMass;
-	k2 *= 2;
 
 	localP1 = c_diff(localP1,body1->vecPosition);
 	localP2 = c_diff(localP2,body2->vecPosition);
 	return (pgJointObject*)pjoint;
 }
 
+void ReComputeDistance(pgDistanceJointObject* joint)
+{
+	pgBodyObject *b1 = joint->joint.body1;
+	pgBodyObject *b2 = joint->joint.body2;
+	if (b1 && b2)
+	{
+		pgVector2 s1 = PG_GetGlobalPos(b1,&joint->anchor1);
+		pgVector2 s2 = PG_GetGlobalPos(b2,&joint->anchor2);
+		joint->distance = c_get_length(c_diff(s1,s2));
+	}
+	else
+	{
+		pgVector2 s1 = PG_GetGlobalPos(b1,&joint->anchor1);
+		joint->distance = c_get_length(c_diff(s1,joint->anchor2));
+	}
+}
+
 static PyObject* _pgDistanceJoint_getDistance(pgDistanceJointObject* joint,void* closure)
 {
 	return PyFloat_FromDouble(joint->distance);
 	return PyComplex_FromCComplex(joint->anchor1);
 }
 
+
+
 static int _pgDistanceJoint_setAnchor1(pgDistanceJointObject* joint,PyObject* value,void* closure)
 {
 	if (PyComplex_Check(value))
 	{
 		joint->anchor1 = PyComplex_AsCComplex(value);
+		ReComputeDistance(joint);
 		return 0;
 	}
 	else
 	if (PyComplex_Check(value))
 	{
 		joint->anchor2 = PyComplex_AsCComplex(value);
+		ReComputeDistance(joint);
 		return 0;
 	}
 	else
 
 static PyGetSetDef _pgDistanceJoint_getseters[] = {
 	{
-		"distance",(getter)_pgDistanceJoint_getDistance,(setter)_pgDistanceJoint_setDistance,"",NULL,
+		"distance",(getter)NULL,(setter)_pgDistanceJoint_setDistance,"",NULL, //can't direct set distance
 	},
 	{
 		"anchor1",(getter)_pgDistanceJoint_getAnchor1,(setter)_pgDistanceJoint_setAnchor1,"",NULL,

File src/pgWorldObject.c

 	contact_cnt = PyList_Size(world->contactList);
 	if (contact_cnt)
 	{
-		printf("contact_cnt:%d\n",contact_cnt);
+		//printf("contact_cnt:%d\n",contact_cnt);
 	}
 	
 	for(j = 0; j < MAX_ITERATION; ++j)
 {
 	int i;
 	_PG_FreeBodySimulation(world, stepTime);
-	_PG_BodyPositionUpdate(world, stepTime);
+	//_PG_BodyPositionUpdate(world, stepTime);
 	for(i = 0; i < MAX_ITERATION; ++i)
 	{
 		_PG_BodyCollisionDetection(world, stepTime);
 		_PG_JointSolve(world, stepTime);
 		
 	}
-	//_PG_BodyPositionUpdate(world, stepTime);
+	_PG_BodyPositionUpdate(world, stepTime);
 	
 }