예제 #1
0
	void CreateRagdollExperiment_0(const dMatrix& location)
	{
		static dJointDefinition jointsDefinition[] =
		{
			{ "body" },
			{ "leg", 100.0f,{ -15.0f, 15.0f, 30.0f },{ 0.0f, 0.0f, 180.0f }, dAnimationRagdollJoint::m_threeDof },
			{ "foot", 100.0f,{ -15.0f, 15.0f, 30.0f },{ 0.0f, 90.0f, 0.0f }, dAnimationRagdollJoint::m_twoDof },
		};
		const int definitionCount = sizeof (jointsDefinition)/sizeof (jointsDefinition[0]);

		NewtonWorld* const world = GetWorld();
		DemoEntityManager* const scene = (DemoEntityManager*)NewtonWorldGetUserData(world);

		DemoEntity* const modelEntity = DemoEntity::LoadNGD_mesh("selfbalance_01.ngd", GetWorld(), scene->GetShaderCache());

		dMatrix matrix0(modelEntity->GetCurrentMatrix());
		//matrix0.m_posit = location;
		//modelEntity->ResetMatrix(*scene, matrix0);
		scene->Append(modelEntity);

		// add the root childBody
		NewtonBody* const rootBody = CreateBodyPart(modelEntity);

		// build the rag doll with rigid bodies connected by joints
		dDynamicsRagdoll* const dynamicRagdoll = new dDynamicsRagdoll(rootBody, dGetIdentityMatrix());
		AddModel(dynamicRagdoll);
		dynamicRagdoll->SetCalculateLocalTransforms(true);

		// save the controller as the collision user data, for collision culling
		NewtonCollisionSetUserData(NewtonBodyGetCollision(rootBody), dynamicRagdoll);

		int stackIndex = 0;
		DemoEntity* childEntities[32];
		dAnimationJoint* parentBones[32];

		for (DemoEntity* child = modelEntity->GetChild(); child; child = child->GetSibling()) {
			parentBones[stackIndex] = dynamicRagdoll;
			childEntities[stackIndex] = child;
			stackIndex++;
		}

		// walk model hierarchic adding all children designed as rigid body bones. 
		while (stackIndex) {
			stackIndex--;
			DemoEntity* const entity = childEntities[stackIndex];
			dAnimationJoint* parentNode = parentBones[stackIndex];

			const char* const name = entity->GetName().GetStr();
			for (int i = 0; i < definitionCount; i++) {
				if (!strcmp(jointsDefinition[i].m_boneName, name)) {
					NewtonBody* const childBody = CreateBodyPart(entity);
					// connect this body part to its parent with a rag doll joint
					parentNode = CreateChildNode(childBody, parentNode, jointsDefinition[i]);

					NewtonCollisionSetUserData(NewtonBodyGetCollision(childBody), parentNode);
					break;
				}
			}

			for (DemoEntity* child = entity->GetChild(); child; child = child->GetSibling()) {
				parentBones[stackIndex] = parentNode;
				childEntities[stackIndex] = child;
				stackIndex++;
			}
		}

		SetModelMass (100.0f, dynamicRagdoll);

		// set the collision mask
		// note this container work best with a material call back for setting bit field 
		//dAssert(0);
		//controller->SetDefaultSelfCollisionMask();

		// transform the entire contraction to its location
		dMatrix worldMatrix(modelEntity->GetCurrentMatrix() * location);
		worldMatrix.m_posit = location.m_posit;
		NewtonBodySetMatrixRecursive(rootBody, &worldMatrix[0][0]);

		// attach effectors here
		for (dAnimationJoint* joint = GetFirstJoint(dynamicRagdoll); joint; joint = GetNextJoint(joint)) {
			if (joint->GetAsRoot()) {
				dAssert(dynamicRagdoll == joint);
				dynamicRagdoll->SetHipEffector(joint);
			} else if (joint->GetAsLeaf()) {
				dynamicRagdoll->SetFootEffector(joint);
			}
		}

		dynamicRagdoll->Finalize();
		//return controller;
	}
예제 #2
0
	void CreateBasicGait (const dVector& posit, dFloat angle)
	{
		dCustomActiveCharacterController* const controller = CreateTransformController();
		NewtonBody* const root = ParseRagdollFile("balancingGait.txt", controller);

		dMatrix bodyMatrix;
		NewtonBodyGetMatrix (root, &bodyMatrix[0][0]);

		bodyMatrix = dYawMatrix(angle) * bodyMatrix;
		bodyMatrix.m_posit = posit;
		bodyMatrix.m_posit.m_w = 1.0f;
		NewtonBodySetMatrixRecursive (root, &bodyMatrix[0][0]);
/*
return;
dCustomHinge* xxx = new dCustomHinge(bodyMatrix, root);
xxx->SetFriction (20000.0f);
xxx->SetLimits(0.0f, 0.0f);

		void* const rootNode = controller->AddRoot(root);

		int stack = 1;
		void* stackPool[128];
		stackPool[0] = rootNode;

		dString pevisEffector ("Bip01_Pelvis"); 
		dString leftFootEffector ("Bip01_L_Foot");
		dString rightFootEffector ("Bip01_R_Foot");

		dTree<int, NewtonJoint*> filter; 
		while (stack) {
			stack --;
			void* const node = stackPool[stack];
			NewtonBody* const body = controller->GetBody(node);
			DemoEntity* const entity = (DemoEntity*) NewtonBodyGetUserData(body);


			// add the end effectors.
			if (entity->GetName() == pevisEffector) {
				// root effect has it pivot at the center of mass 
				dVector com;
				dMatrix matrix;
				NewtonBodyGetMatrix(body, &matrix[0][0]);

				NewtonBodyGetCentreOfMass(body, &com[0]);
				matrix.m_posit = matrix.TransformVector(com);
				controller->AddEndEffector(node, matrix);

			} else if (entity->GetName() == leftFootEffector) {

				// all other effector are centered and oriented a the joint pivot 
				dMatrix matrix;
				NewtonBodyGetMatrix(body, &matrix[0][0]);

				dCustomJoint* const joint = controller->GetJoint(node);
				dAssert (joint->GetBody0() == body);
				matrix = joint->GetMatrix0() * matrix;
				dCustomRagdollMotor_EndEffector* const effector = controller->AddEndEffector(node, matrix);
dCustomKinematicController* xxx = new dCustomKinematicController (body, matrix);

				matrix.m_posit.m_z -= 0.4f;
				matrix.m_posit.m_x -= 0.2f;
				matrix.m_posit.m_y += 0.0f;
				effector->SetTargetMatrix(matrix);

xxx->SetPickMode(0);
xxx->SetTargetMatrix(matrix);
xxx->SetMaxLinearFriction (5000.0f);
xxx->SetMaxAngularFriction (5000.0f);
				

			} else if (entity->GetName() == rightFootEffector) {
				// all other effector are centered and oriented a the joint pivot 
				dMatrix matrix;
				NewtonBodyGetMatrix(body, &matrix[0][0]);

				dCustomJoint* const joint = controller->GetJoint(node);
				dAssert(joint->GetBody0() == body);
				matrix = joint->GetMatrix0() * matrix;
				dCustomRagdollMotor_EndEffector* const effector = controller->AddEndEffector(node, matrix);
dCustomKinematicController* xxx = new dCustomKinematicController (body, matrix);

				matrix.m_posit.m_z += 0.4f;
				matrix.m_posit.m_x += 0.2f;
				matrix.m_posit.m_y += 0.0f;
				effector->SetTargetMatrix(matrix);

xxx->SetPickMode(0);
xxx->SetTargetMatrix(matrix);
xxx->SetMaxLinearFriction(5000.0f);
xxx->SetMaxAngularFriction(5000.0f);
			}


			for (NewtonJoint* newtonJoint = NewtonBodyGetFirstJoint(body); newtonJoint; newtonJoint = NewtonBodyGetNextJoint(body, newtonJoint)) {
				if (!filter.Find(newtonJoint)) {
					filter.Insert(newtonJoint);
					dCustomJoint* const customJoint = (dCustomJoint*)NewtonJointGetUserData(newtonJoint);
					if (customJoint->IsType(dCustomRagdollMotor::GetType())) {
						dCustomRagdollMotor* const ragDollMotor = (dCustomRagdollMotor*)customJoint;
						void* const bone = controller->AddBone(ragDollMotor, node);
						//ragDollMotor->SetMode(true);
						stackPool[stack] = bone;
						stack ++;
					}
				}
			}
		}

		controller->Finalize();
*/
	}