Esempio n. 1
0
bool
PMDHandler::doLoad(Model& model, istream& stream) noexcept
{
	PMD _pmd;
	if (!stream.read((char*)&_pmd.Header, sizeof(_pmd.Header))) return false;

	// vertex
	if (!stream.read((char*)&_pmd.VertexCount, sizeof(_pmd.VertexCount))) return false;

	if (_pmd.VertexCount > 0)
	{
		_pmd.VertexList.resize(_pmd.VertexCount);

		if (!stream.read((char*)&_pmd.VertexList[0], (std::streamsize)(sizeof(PMD_Vertex)* _pmd.VertexCount))) return false;
	}

	// index
	if (!stream.read((char*)&_pmd.IndexCount, sizeof(_pmd.IndexCount))) return false;

	if (_pmd.IndexCount > 0)
	{
		_pmd.IndexList.resize(_pmd.IndexCount);

		if (!stream.read((char*)&_pmd.IndexList[0], (std::streamsize)(sizeof(PMD_Index)* _pmd.IndexCount))) return false;
	}

	// materal
	if (!stream.read((char*)&_pmd.MaterialCount, sizeof(_pmd.MaterialCount))) return false;

	if (_pmd.MaterialCount > 0)
	{
		_pmd.MaterialList.resize(_pmd.MaterialCount);

		if (!stream.read((char*)&_pmd.MaterialList[0], (std::streamsize)(sizeof(PMD_Material)* _pmd.MaterialCount))) return false;
	}

	// bone
	if (!stream.read((char*)&_pmd.BoneCount, sizeof(_pmd.BoneCount))) return false;

	if (_pmd.BoneCount > 0)
	{
		_pmd.BoneList.resize(_pmd.BoneCount);

		if (!stream.read((char*)&_pmd.BoneList[0], (std::streamsize)(sizeof(PMD_Bone)* _pmd.BoneCount))) return false;
	}

	// IK
	if (!stream.read((char*)&_pmd.IkCount, sizeof(_pmd.IkCount))) return false;

	if (_pmd.IkCount > 0)
	{
		_pmd.IkList.resize(_pmd.IkCount);

		for (std::size_t i = 0; i < (std::size_t)_pmd.IkCount; i++)
		{
			if (!stream.read((char*)&_pmd.IkList[i].IK, sizeof(_pmd.IkList[i].IK))) return false;
			if (!stream.read((char*)&_pmd.IkList[i].Target, sizeof(_pmd.IkList[i].Target))) return false;
			if (!stream.read((char*)&_pmd.IkList[i].LinkCount, sizeof(_pmd.IkList[i].LinkCount))) return false;
			if (!stream.read((char*)&_pmd.IkList[i].LoopCount, sizeof(_pmd.IkList[i].LoopCount))) return false;
			if (!stream.read((char*)&_pmd.IkList[i].LimitOnce, sizeof(_pmd.IkList[i].LimitOnce))) return false;

			_pmd.IkList[i].LinkList.resize(_pmd.IkList[i].LinkCount);

			if (!stream.read((char*)&_pmd.IkList[i].LinkList[0], (std::streamsize)(sizeof(PMD_Link)* _pmd.IkList[i].LinkCount))) return false;
		}
	}

	// Morph
	if (!stream.read((char*)&_pmd.MorphCount, sizeof(_pmd.MorphCount))) return false;

	if (_pmd.MorphCount > 0)
	{
		_pmd.MorphList.resize(_pmd.MorphCount);

		for (std::size_t i = 0; i < (std::size_t)_pmd.MorphCount; i++)
		{
			if (!stream.read((char*)&_pmd.MorphList[i].Name, sizeof(_pmd.MorphList[i].Name))) return false;
			if (!stream.read((char*)&_pmd.MorphList[i].VertexCount, sizeof(_pmd.MorphList[i].VertexCount))) return false;
			if (!stream.read((char*)&_pmd.MorphList[i].Category, sizeof(_pmd.MorphList[i].Category))) return false;

			if (_pmd.MorphList[i].VertexCount > 0)
			{
				_pmd.MorphList[i].VertexList.resize(_pmd.MorphList[i].VertexCount);

				if (!stream.read((char*)&_pmd.MorphList[i].VertexList[0], (std::streamsize)(sizeof(PMD_MorphVertex)* _pmd.MorphList[i].VertexCount))) return false;
			}
		}
	}

	// frame window
	if (!stream.read((char*)&_pmd.FrameWindow.ExpressionListCount, sizeof(_pmd.FrameWindow.ExpressionListCount))) return false;

	if (_pmd.FrameWindow.ExpressionListCount > 0)
	{
		_pmd.FrameWindow.ExpressionList.resize(_pmd.FrameWindow.ExpressionListCount);

		if (!stream.read((char*)&_pmd.FrameWindow.ExpressionList[0], (std::streamsize)(sizeof(PMD_Expression)* _pmd.FrameWindow.ExpressionListCount))) return false;
	}

	if (!stream.read((char*)&_pmd.FrameWindow.NodeNameCount, sizeof(_pmd.FrameWindow.NodeNameCount))) return false;

	if (_pmd.FrameWindow.NodeNameCount > 0)
	{
		_pmd.FrameWindow.NodeNameList.resize(_pmd.FrameWindow.NodeNameCount);

		if (!stream.read((char*)&_pmd.FrameWindow.NodeNameList[0].Name, (std::streamsize)(sizeof(PMD_NodeName)* _pmd.FrameWindow.NodeNameCount))) return false;
	}

	if (!stream.read((char*)&_pmd.FrameWindow.BoneToNodeCount, sizeof(_pmd.FrameWindow.BoneToNodeCount))) return false;

	if (_pmd.FrameWindow.BoneToNodeCount > 0)
	{
		_pmd.FrameWindow.BoneToNodeList.resize(_pmd.FrameWindow.BoneToNodeCount);

		if (!stream.read((char*)&_pmd.FrameWindow.BoneToNodeList[0].Bone, (std::streamsize)(sizeof(PMD_BoneToNode)* _pmd.FrameWindow.BoneToNodeCount))) return false;
	}

	// description
	if (!stream.read((char*)&_pmd.HasDescription, sizeof(_pmd.HasDescription))) return false;

	if (_pmd.HasDescription)
	{
		if (!stream.read((char*)&_pmd.Description.ModelName, sizeof(_pmd.Description.ModelName))) return false;

		if (!stream.read((char*)&_pmd.Description.Comment, sizeof(_pmd.Description.Comment))) return false;

		for (PMD_BoneCount i = 0; i < _pmd.BoneCount; i++)
		{
			PMD_BoneName name;

			if (!stream.read((char*)&name.Name, sizeof(name))) return false;

			_pmd.Description.BoneName.push_back(name);
		}

		for (PMD_uint8_t i = 0; i < _pmd.FrameWindow.ExpressionListCount; i++)
		{
			PMD_MorphName name;

			if (!stream.read((char*)&name.Name, sizeof(name))) return false;

			_pmd.Description.FaceName.push_back(name);
		}

		for (PMD_uint8_t i = 0; i < _pmd.FrameWindow.NodeNameCount; i++)
		{
			PMD_NodeName name;

			if (!stream.read((char*)&name.Name, sizeof(name))) return false;

			_pmd.Description.FrameName.push_back(name);
		}
	}

	// toon
	_pmd.ToonCount = PMD_NUM_TOON;

	_pmd.ToonList.resize(_pmd.ToonCount);

	if (!stream.read((char*)&_pmd.ToonList[0].Name, (std::streamsize)(sizeof(PMD_Toon) * _pmd.ToonCount))) return false;

	// rigidbody
	if (!stream.read((char*)&_pmd.BodyCount, sizeof(_pmd.BodyCount))) return false;

	if (_pmd.BodyCount > 0)
	{
		_pmd.BodyList.resize(_pmd.BodyCount);

		if (!stream.read((char*)&_pmd.BodyList[0], (std::streamsize)(sizeof(PMD_Body)* _pmd.BodyCount))) return false;
	}

	// joint
	if (!stream.read((char*)&_pmd.JointCount, sizeof(_pmd.JointCount))) return false;

	if (_pmd.JointCount > 0)
	{
		_pmd.JointList.resize(_pmd.JointCount);

		if (!stream.read((char*)&_pmd.JointList[0], (std::streamsize)(sizeof(PMD_Body)* _pmd.JointCount))) return false;
	}

	for (std::size_t index = 0; index < _pmd.MaterialList.size(); index++)
	{
		auto& it = _pmd.MaterialList[index];

		auto material = std::make_shared<MaterialProperty>();
		material->set(MATKEY_COLOR_DIFFUSE, it.Diffuse);
		material->set(MATKEY_COLOR_AMBIENT, it.Ambient);
		material->set(MATKEY_COLOR_SPECULAR, it.Specular);
		material->set(MATKEY_OPACITY, it.Opacity);
		material->set(MATKEY_SHININESS, it.Shininess);

		std::string name = it.TextureName;
		std::string::size_type substr = name.find_first_of("*");
		if (substr != std::string::npos)
		{
			name.erase(name.begin() + substr, name.end());
		}

		material->set(MATKEY_TEXTURE_DIFFUSE(0), name);
		material->set(MATKEY_TEXTURE_AMBIENT(0), name);

		model.addMaterial(material);
	}

	PMD_Index* indices = _pmd.IndexList.data();
	PMD_Vertex* vertices = _pmd.VertexList.data();

	MeshPropertyPtr root = std::make_shared<MeshProperty>();
	MeshPropertyPtr mesh = root;
	MeshPropertyPtr last = nullptr;

	for (std::size_t index = 0; index < _pmd.MaterialList.size(); index++)
	{
		auto& it = _pmd.MaterialList[index];

		Vector3Array points;
		Vector3Array normals;
		Vector2Array texcoords;
		VertexWeights weights;
		UintArray faces;

		for (PMD_IndexCount i = 0; i < it.FaceVertexCount; i++, indices++)
		{
			PMD_Vertex& v = vertices[*indices];

			points.push_back(v.Position);
			normals.push_back(v.Normal);
			texcoords.push_back(v.UV);
			faces.push_back(i);

			VertexWeight weight;
			weight.weight1 = v.Weight / 100.0;
			weight.weight2 = 1.0 - weight.weight1;
			weight.weight3 = 0.0f;
			weight.weight4 = 0.0f;
			weight.bone1 = v.Bone.Bone1;
			weight.bone2 = v.Bone.Bone2;
			weight.bone3 = 0;
			weight.bone4 = 0;

			weights.push_back(weight);
		}

		if (last == mesh)
		{
			mesh = std::make_shared<MeshProperty>();
			root->addChild(mesh);
		}

		mesh->setMaterialID(index);
		mesh->setVertexArray(points);
		mesh->setNormalArray(normals);
		mesh->setTexcoordArray(texcoords);
		mesh->setWeightArray(weights);
		mesh->setFaceArray(faces);

		last = mesh;
	}


	if (_pmd.BoneCount > 0)
	{
		Bones bones;
		InverseKinematics iks;

		for (auto& it : _pmd.BoneList)
		{
			Bone bone;

			char inbuf[MAX_PATH + 1] = { 0 };
			char outbuf[MAX_PATH + 1] = { 0 };
			char *in = inbuf;
			char *out = outbuf;
			std::size_t in_size = (size_t)MAX_PATH;
			std::size_t out_size = (size_t)MAX_PATH;

			memcpy(in, it.Name.Name, sizeof(it.Name.Name));

			iconv_t ic = iconv_open("GBK", "SJIS");
			iconv(ic, &in, &in_size, &out, &out_size);
			iconv_close(ic);

			bone.setName(std::string(outbuf));
			bone.setPosition(it.Position);
			bone.setParent(it.Parent);
			bone.setChild(it.Child);

			bones.push_back(bone);
		}

		for (auto& it : _pmd.IkList)
		{
			IKAttr attr;
			attr.IKBoneIndex = it.IK;
			attr.IKTargetBoneIndex = it.Target;
			attr.IKLimitedRadian = it.LimitOnce;
			attr.IKLinkCount = it.LinkCount;
			attr.IKLoopCount = it.LoopCount;
			for (auto& bone : it.LinkList)
			{
				IKChild child;
				child.BoneIndex = bone;
				child.MinimumRadian = Vector3::Zero;
				child.MaximumRadian = Vector3(3.14, 3.14, 3.14);
				child.RotateLimited = 1;

				attr.IKList.push_back(child);
			}

			iks.push_back(attr);
		}

		root->setInverseKinematics(iks);
		root->setBoneArray(bones);
	}

	model.addMesh(root);

	return true;
}
Esempio n. 2
0
/**
 * Draws the skeleton.
 * \param mode  bitmask of RENDER_WIREFRAME, RENDER_FEEDBACK or RENDER_OUTPUT,
 *              determines how the primitives are drawn.
 * \param active active state
 **/
void Skeleton::draw(int mode, int active)
{
    unsigned hit = 0;
    SelectItem *selected = NULL;

    if (selector->getPickLayer()
        && selector->getPickLayer()->getSkeleton() == this) {
        hit = selector->getHitCount();
        selected = selector->getSelected();
    }

    // select objects from the selection buffer
    pJoint = NULL;
    pBone = NULL;
    for (unsigned int i = 0; i < hit; i++) {
        if (selected->type == Selection::SELECT_JOINT &&
            ((ui->settings.mode == ANIMATA_MODE_CREATE_JOINT) ||
             (ui->settings.mode == ANIMATA_MODE_SKELETON_SELECT) ||
             (ui->settings.mode == ANIMATA_MODE_SKELETON_DELETE) ||
             (ui->settings.mode == ANIMATA_MODE_CREATE_BONE))) {
            /* joints are prefered to bones if they overlap */
            pJoint = (*joints)[selected->name];
            pBone = NULL;
            break;
        }
        else if (selected->type == Selection::SELECT_BONE &&
                 (ui->settings.mode != ANIMATA_MODE_CREATE_BONE)) {
            pBone = (*bones)[selected->name];
        }

        selected++;
    }

    if ((mode & RENDER_WIREFRAME)
        && ((!(mode & RENDER_OUTPUT)
             && ui->settings.display_elements & DISPLAY_EDITOR_BONE)
            || ((mode & RENDER_OUTPUT)
                && ui->settings.display_elements & DISPLAY_OUTPUT_BONE)))
    {
        glLoadName(Selection::SELECT_BONE);    /* type of primitive */
        glPushName(0);                        /* id of primitive */

        for (unsigned i = 0; i < bones->size(); i++) {
            Bone *bone = (*bones)[i];

            glLoadName(i);

            if (mode & RENDER_OUTPUT)
                bone->draw(false);
            else
                bone->draw(bone == pBone, active);
        }

        glPopName();
    }

    if (mode & RENDER_FEEDBACK) {
        for (unsigned i = 0; i < joints->size(); i++) {
            Joint *joint = (*joints)[i];

            glPassThrough(i);
            glBegin(GL_POINTS);
                glVertex2f(joint->position.x, joint->position.y);
            glEnd();
        }
    }
    else if ((mode & RENDER_WIREFRAME) &&
             ((!(mode & RENDER_OUTPUT)
               && ui->settings.display_elements & DISPLAY_EDITOR_JOINT) ||
             ((mode & RENDER_OUTPUT)
              && ui->settings.display_elements & DISPLAY_OUTPUT_JOINT)))
    {
        glLoadName(Selection::SELECT_JOINT);    /* type of primitive */
        glPushName(0);                            /* id of primitive */

        for (unsigned i = 0; i < joints->size(); i++) {
            Joint *joint = (*joints)[i];

            glLoadName(i);

            if (mode & RENDER_OUTPUT)
                joint->draw(false);
            else
                joint->draw(joint == pJoint, active);
        }

        glPopName();
    }
}
Esempio n. 3
0
void SceneMesh::renderMeshLocally() {
	Renderer *renderer = CoreServices::getInstance()->getRenderer();
	
	if(skeleton) {	
		for(int i=0; i < mesh->getPolygonCount(); i++) {
			Polygon *polygon = mesh->getPolygon(i);			
			unsigned int vCount = polygon->getVertexCount();			
			for(int j=0; j < vCount; j++) {
				Vertex *vert = polygon->getVertex(j);
				Vector3 norm;
				
					Vector3 aPos = vert->restPosition;
					Vector3 tPos;
				
					Number mult = 0;
					for(int b =0; b < vert->getNumBoneAssignments(); b++) {
						BoneAssignment *bas = vert->getBoneAssignment(b);
						mult += bas->weight;
					}
					mult = 1.0f/mult;
				
					for(int b =0; b < vert->getNumBoneAssignments(); b++) {
						BoneAssignment *bas = vert->getBoneAssignment(b);
						Bone *bone = bas->bone;
						if(bone) {
							
							Matrix4 restMatrix = bone->getRestMatrix();
							Matrix4 finalMatrix = bone->getFinalMatrix();
							
							Vector3 vec = restMatrix * aPos;
							tPos += finalMatrix * vec * (bas->weight*mult);
							
							Vector3 nvec = vert->restNormal;
							nvec = restMatrix.rotateVector(nvec);
							nvec = finalMatrix.rotateVector(nvec);
							
							norm += nvec * (bas->weight*mult);
						}
					}					
					
				
					vert->x = tPos.x;
					vert->y = tPos.y;
					vert->z = tPos.z;				
				
					norm.Normalize();
					vert->setNormal(norm.x, norm.y, norm.z);
				
			}
		}
		mesh->arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;		
		mesh->arrayDirtyMap[RenderDataArray::NORMAL_DATA_ARRAY] = true;		
	}

	if(mesh->useVertexColors) {
		renderer->pushDataArrayForMesh(mesh, RenderDataArray::COLOR_DATA_ARRAY);
	}
	 
	renderer->pushDataArrayForMesh(mesh, RenderDataArray::VERTEX_DATA_ARRAY);
	renderer->pushDataArrayForMesh(mesh, RenderDataArray::NORMAL_DATA_ARRAY);		
	renderer->pushDataArrayForMesh(mesh, RenderDataArray::TEXCOORD_DATA_ARRAY);	
	
	renderer->drawArrays(mesh->getMeshType());
}
Esempio n. 4
0
bool Board::display() //Displays the board
{
	if (!hasSpinner() && rightRow.size() == 0)
		return false;

	if (!hasSpinner())  //if no spinner is in the board
	{
		vector<Bone> vecRow = getRow('e');
		int numChars = vecRow.size()*4;
		numChars = 60 - numChars/2;
		int x = numChars;
		int y = 27;
		for (unsigned int i = 0; i < vecRow.size(); i++)
		{
			Bone tempBone = vecRow[i];
			if (i == 0)
				tempBone.swap();
			tempBone.display(x,y,'e');
		}
	}
	else               //if there is a spinner on the board
	{
		int tempX = 60;
		int tempY = 27;
		spinner.display(tempX,tempY,'e');

		if (rightRow.size() != 0)
		{
			int x = 62;
			int y = 27;
			bool up = false;
			bool left = false;
			bool lastDouble = false;
			for (unsigned int i = 0; i < rightRow.size(); i++) //the following code accounts for the possibility of the bones coming out of the board's limits
			{
				if (x >= 114 && !up)
				{
					up = true;
					if (lastDouble)
					{
						y = y - 3;
						x = x - 2;
					}
					else
					{
						x = x - 2;
						y = y - 2;
					}
				}
				if (y <= 8 && !left)
				{
					left = true;
					if (lastDouble)
					{
						y = y + 2;
						x = x - 3;
					}
					else
					{
						y = y + 2;
						x = x - 2;
					}
				}

				if (up)
					if (left)
						rightRow[i].display(x,y,'w');
					else
						rightRow[i].display(x,y,'n');
				else
					rightRow[i].display(x,y,'e');

				if (rightRow[i].isDouble())
					lastDouble = true;
				else
					lastDouble = false;
			}
		}

		if (leftRow.size() != 0)
		{
			int x = 58;
			int y = 27;
			bool down = false;
			bool right = false;
			bool lastDouble = false;
			for (unsigned int i = 0; i < leftRow.size(); i++)
			{
				if (x <= 4 && !down)
				{
					down = true;
					if (lastDouble)
					{
						x = x + 2;
						y = y + 3;
					}
					else
					{
						x = x + 2;
						y = y + 2;
					}
				}
				if (y >= 45 && !right)
				{
					right = true;
					if (lastDouble)
					{
						y = y - 2;
						x = x + 3;
					}
					else
					{
						y = y - 2;
						x = x + 2;
					}
				}

				if (down)
					if (right)
						leftRow[i].display(x,y,'e');
					else
						leftRow[i].display(x,y,'s');
				else
					leftRow[i].display(x,y,'w');

				if (leftRow[i].isDouble())
					lastDouble = true;
				else
					lastDouble = false;
			}
		}

		if (topRow.size() != 0)
		{
			int x = 60;
			int y = 24;
			bool left = false;
			bool down = false;
			bool lastDouble = false;
 			for (unsigned int i = 0; i < topRow.size(); i++)
			{				
				if (x <= 20 && !down)
				{
					down = true;
					if (lastDouble)
					{
						x = x + 2;
						y = y + 3;
					}
					else
					{
						x = x + 2;
						y = y + 2;
					}
				}
				if (y <= 5 && !left)
				{
					left = true;
					if (lastDouble)
					{
						y = y + 2;
						x = x - 3;
					}
					else
					{
						y = y + 2;
						x = x - 2;
					}
				}

				if (left)
					if (down)
						topRow[i].display(x,y,'s');
					else
						topRow[i].display(x,y,'w');
				else
					topRow[i].display(x,y,'n');

				if (topRow[i].isDouble())
					lastDouble = true;
				else
					lastDouble = false;
			}
		}

		if (bottomRow.size() != 0)
		{
			int x = 60;
			int y = 30;
			bool right = false;
			bool up = false;
			bool lastDouble = false;
			for (unsigned int i = 0; i < bottomRow.size(); i++)
			{
				if (x >= 114 && !up)
				{
					up = true;
					if (lastDouble)
					{
						x = x - 2;
						y = y - 3;
					}
					else
					{
						x = x - 2;
						y = y - 2;
					}
				}
				if (y >= 45 && !right)
				{
					right = true;
					if (lastDouble)
					{
						y = y - 2;
						x = x + 3;
					}
					else
					{
						y = y - 2;
						x = x + 2;
					}
				}

				if (right)
					if (up)
						bottomRow[i].display(x,y,'n');
					else
						bottomRow[i].display(x,y,'e');
				else
					bottomRow[i].display(x,y,'s');

				if (bottomRow[i].isDouble())
					lastDouble = true;
				else
					lastDouble = false;
			}
		}

	}


	return true;
}
Esempio n. 5
0
	void CreateEmptyTransformNodeTree( TransformNode& root_transform_node ) { m_RootBone.CreateEmptyTransformNodeTree( root_transform_node ); }
Esempio n. 6
0
void NormalEnemy::headDropDie()
{
    
    if (footBody != NULL) {
        gameWorld->DestroyBody(footBody);
        footBody = NULL;
    }
    
    setB2bodyPosition();

    if (!dead) {
        
        for (auto o : deadSpriteArray) {
            if (armature->getScaleX() < 0) {
                o->setScaleX(-o->getScaleX());
            }
        }

        
        armature->setVisible(false);
        
        
        b2Filter filter;
        filter.categoryBits = DEAD_ZOMBIE;
        filter.maskBits = UPPER_GROUND | BASE_GROUND;
        
        const Map<std::string, Bone*>& dic = armature->getBoneDic();
        for(auto& element : dic)
        {
            //Ref *o = dickeyarr->objectAtIndex(i);
            // CCString *key = dynamic_cast<CCString*>(o);
            Bone *bone = element.second;
            Skin *skin = (Skin*)element.second->getDisplayRenderNode();
            string name = element.first;
            
            if (skin) {
                b2Body *body = skin->body;
                
                body->GetFixtureList()->SetFixtureType(f_bodydead);
                body->GetFixtureList()->SetFilterData(filter);
                body->SetType(b2_dynamicBody);
                
                Bone *parentBone = bone->getParentBone();
                string selfName = bone->getName();
                string parentName;
                if (parentBone) {
                    parentName = parentBone->getName();
                }
                

                if (parentBone && selfName.compare("headbone") != 0 && parentName.compare("headbone") != 0) {
                    
                    Skin *parentSkin = (Skin*)parentBone->getDisplayRenderNode();
                    b2Body *parentBody = parentSkin->body;
                    b2RevoluteJointDef jdef;
                    Mat4 tran = bone->_getNodeToParentTransform();
                    Point p = Point(tran.m[12], tran.m[13]);
                    // Point p = skin->getWorldPosition();
                    //printf("p x = %f y = %f\n", p.x, p.y);
                    jdef.Initialize(parentBody, body, b2Vec2(p.x/PTM_RATIO, p.y/PTM_RATIO));
                    jdef.lowerAngle = -0.35f * b2_pi;
                    jdef.upperAngle = 0.35f * b2_pi;
                    jdef.enableLimit = true;
                    gameWorld->CreateJoint(&jdef);
                }
                
            }
        }
        
       // update(0);

        setBodySprites();
        dead = true;
        
        for (auto o : deadSpriteArray) {
             o->setVisible(true);
        }
        
        
    }
}
Esempio n. 7
0
AEResult Skeleton::Load()
{
	std::lock_guard<std::mutex> lock(m_GameResourceMutex);

	AEAssert(!m_FileName.empty());
	if(m_FileName.empty())
	{
		return AEResult::EmptyFilename;
	}
	
	/////////////////////////////////////////////
	//Clean Up memory before loading File
	CleanUp();

	AEResult ret = AEResult::Ok;
	
	/////////////////////////////////////////////
	//Start reading file
	std::ifstream skeletonFile;

	skeletonFile.open(m_FileName, std::ios::binary | std::ios::in);

	if(!skeletonFile.is_open())
	{
		AETODO("add log");

		return AEResult::OpenFileFail;
	}

	char* tempPtr = nullptr;
	uint32_t sizeToRead = 0;
	
	/////////////////////////////////////////////
	//Verify Header
	bool verifyHeader = AEGameContentHelpers::ReadFileHeaderAndVerify(skeletonFile, AE_CT_AE3DS_FILE_HEADER, AE_CT_AE3DS_FILE_VERSION_MAYOR, AE_CT_AE3DS_FILE_VERSION_MINOR, AE_CT_AE3DS_FILE_VERSION_REVISON);

	if(!verifyHeader)
	{
		AETODO("Add log");

		return AEResult::InvalidFileHeader;
	}

	/////////////////////////////////////////////
	//Read Number of Bones
	uint32_t numBones = 0;
	tempPtr = reinterpret_cast<char*>(&numBones);
	sizeToRead = sizeof(uint32_t);
	skeletonFile.read(tempPtr, sizeToRead);
	
	/////////////////////////////////////////////
	//Read Bones
	for(uint32_t i = 0; i < numBones; ++i)
	{
		Bone* bone = new Bone();
		glm::mat4 tempMat = AEMathHelpers::Mat4Identity;
		int32_t tempInt32 = 0;

		/////////////////////////////////////////////
		//Write name of Bone and size of Name
		bone->SetName(AEGameContentHelpers::ReadString(skeletonFile));

		/////////////////////////////////////////////
		//Read Bone Matrices
		sizeToRead = sizeof(glm::mat4);
		tempPtr = reinterpret_cast<char*>(&tempMat);

		//Local Matrix
		skeletonFile.read(tempPtr, sizeToRead);
		bone->SetLocalMatrix(tempMat);

		//World Matrix
		skeletonFile.read(tempPtr, sizeToRead);
		bone->SetWorldMatrix(tempMat);
		 
		//Off Set Matrix
		skeletonFile.read(tempPtr, sizeToRead);
		bone->SetOffSetMatrix(tempMat);
		
		/////////////////////////////////////////////
		//Read Bone Indices
		tempPtr = reinterpret_cast<char*>(&tempInt32);
		sizeToRead = sizeof(int32_t);

		//Parent Index
		skeletonFile.read(tempPtr, sizeToRead);
		bone->SetParentIndex(tempInt32);

		//Bone Index
		skeletonFile.read(tempPtr, sizeToRead);
		bone->SetIndex(tempInt32);
		
		/////////////////////////////////////////////
		//Add Bone to Skeleton
		m_BoneHierarchy.push_back(bone);
	}
	
	/////////////////////////////////////////////
	//Read Footer 
	bool verifyFooter = AEGameContentHelpers::ReadFileFooterAndVerify(skeletonFile, AE_CT_AE3DS_FILE_FOOTER);
	if(!verifyFooter)
	{
		AETODO("Add Warning log");
	}
	
	/////////////////////////////////////////////
	//Finish
	skeletonFile.close();

	return AEResult::Ok;
}
Esempio n. 8
0
void TestParticleDisplay::onEnter()
{
    ArmatureTestLayer::onEnter();

    auto listener = EventListenerTouchAllAtOnce::create();
    listener->onTouchesEnded = CC_CALLBACK_2(TestParticleDisplay::onTouchesEnded, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

    animationID = 0;

    armature = Armature::create("robot");
    armature->getAnimation()->playWithIndex(0);
    armature->setPosition(VisibleRect::center());
    armature->setScale(0.48f);
    armature->getAnimation()->setSpeedScale(0.5f);
    addChild(armature);


    ParticleSystem *p1 = CCParticleSystemQuad::create("Particles/SmallSun.plist");
    ParticleSystem *p2 = CCParticleSystemQuad::create("Particles/SmallSun.plist");

    Bone *bone  = Bone::create("p1");
    bone->addDisplay(p1, 0);
    bone->changeDisplayWithIndex(0, true);
    bone->setIgnoreMovementBoneData(true);
    bone->setLocalZOrder(100);
    bone->setScale(1.2f);
    armature->addBone(bone, "bady-a3");

    bone  = Bone::create("p2");
    bone->addDisplay(p2, 0);
    bone->changeDisplayWithIndex(0, true);
    bone->setIgnoreMovementBoneData(true);
    bone->setLocalZOrder(100);
    bone->setScale(1.2f);
    armature->addBone(bone, "bady-a30");
}
    SkeletonPtr MergeSkeleton::bake()
    {    
        MeshCombiner::getSingleton().log( 
             "Baking: New Skeleton started" );

        SkeletonPtr sp = SkeletonManager::getSingleton().create( "mergeSkeleton", 
             ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, true );
        
        for( std::vector< Ogre::SkeletonPtr >::iterator it = m_Skeletons.begin();
             it != m_Skeletons.end(); ++it )
        {   
            if(  it == m_Skeletons.begin() )
            {
                MeshCombiner::getSingleton().log( 
                    "Baking: using " + (*it)->getName() + " as the base skeleton"   );

                MeshCombiner::getSingleton().log( 
                    "Baking: adding bones"   );
                Skeleton::BoneIterator bit = (*it)->getBoneIterator();
                while( bit.hasMoreElements() )
                {
                    Bone* bone = bit.getNext();
                    Bone* newbone = sp->createBone( bone->getName(), bone->getHandle() );
                    newbone->setScale( bone->getScale() );
                    newbone->setOrientation( bone->getOrientation() );
                    newbone->setPosition( bone->getPosition() );
                }
                MeshCombiner::getSingleton().log( 
                    "Baking: building bone hierarchy"   );
                // bone hierarchy
                bit = (*it)->getBoneIterator();
                while( bit.hasMoreElements() )
                {
                    Bone* bone = bit.getNext();
                    Node* pnode = bone->getParent();
                    if( pnode != NULL )
                    {
                        Bone* pbone = static_cast<Bone*>( pnode );
                        sp->getBone( pbone->getHandle() )->addChild( sp->getBone( bone->getHandle() ) );
                    }
                }
            }   

            MeshCombiner::getSingleton().log( 
                "Baking: adding animations for " + (*it)->getName() );

            // insert all animations
            for (unsigned short a=0; a < (*it)->getNumAnimations(); ++a )
            {
                Animation* anim = (*it)->getAnimation( a );
                Animation* newanim = sp->createAnimation( anim->getName(), anim->getLength() );

                if( anim->getNumNodeTracks() > 0 )
                    MeshCombiner::getSingleton().log( 
                        "Baking: adding node tracks" );
                for( unsigned short na=0; na < anim->getNumNodeTracks(); ++na )
                {
                    if( anim->hasNodeTrack( na ) )
                    {
                        NodeAnimationTrack* nat = anim->getNodeTrack( na );
                        NodeAnimationTrack* newnat = newanim->createNodeTrack( na );
                        // all key frames
                        for( unsigned short nf=0; nf < nat->getNumKeyFrames(); ++nf )
                        {
                            TransformKeyFrame* tkf = nat->getNodeKeyFrame( nf );
                            TransformKeyFrame* newtkf = newnat->createNodeKeyFrame( tkf->getTime() );
                            newtkf->setRotation( tkf->getRotation() );
                            newtkf->setTranslate( tkf->getTranslate() );
                            newtkf->setScale( tkf->getScale() );
                        }

                        newnat->setAssociatedNode( sp->getBone( nat->getHandle() ) );
                    }
                }

                if( anim->getNumNumericTracks() > 0 )
                    MeshCombiner::getSingleton().log( 
                        "Baking: adding numeric tracks" );
                for( unsigned short na=0; na < anim->getNumNumericTracks(); ++na )
                {
                    if( anim->hasNumericTrack( na ) )
                    {
                        NumericAnimationTrack* nat = anim->getNumericTrack( na );
                        NumericAnimationTrack* newnat = newanim->createNumericTrack( na );

                        // all key frames
                        for( unsigned short nf=0; nf < nat->getNumKeyFrames(); ++nf )
                        {
                            NumericKeyFrame* nkf = nat->getNumericKeyFrame( nf );
                            NumericKeyFrame* newnkf = newnat->createNumericKeyFrame( nkf->getTime() );
                            newnkf->setValue( nkf->getValue() );
                        }
                    }
                }

                if( anim->getNumVertexTracks() > 0 )
                    MeshCombiner::getSingleton().log( 
                        "Baking: adding vertex tracks" );
                for( unsigned short va=0; va < anim->getNumVertexTracks(); ++va )
                {
                    if( anim->hasVertexTrack( va ) )
                    {
                        VertexAnimationTrack* vat = anim->getVertexTrack( va );
                        VertexAnimationTrack* newvat = newanim->createVertexTrack( va, vat->getAnimationType() );

                        // all key frames
                        for( unsigned short nf=0; nf < vat->getNumKeyFrames(); ++nf )
                        {
                            // all morphs
                            VertexMorphKeyFrame* vmkf = vat->getVertexMorphKeyFrame( nf );
                            if( vmkf != NULL )
                            {
                                VertexMorphKeyFrame* newvmkf = newvat->createVertexMorphKeyFrame( vmkf->getTime() );
                                // @todo vertex buffer copying correct??
                                HardwareVertexBufferSharedPtr buf = vmkf->getVertexBuffer();
                                HardwareVertexBufferSharedPtr newbuf = HardwareBufferManager::getSingleton().createVertexBuffer(
                                    buf->getVertexSize(), buf->getNumVertices(), buf->getUsage(), buf->hasShadowBuffer() );
                                newbuf->copyData( *buf.getPointer(), 0, 0, buf->getSizeInBytes() );
                            }

                            // all poses
                            VertexPoseKeyFrame* vpkf = vat->getVertexPoseKeyFrame( nf );
                            if( vpkf != NULL )
                            {
                                VertexPoseKeyFrame* newvpkf = newvat->createVertexPoseKeyFrame( vpkf->getTime() );
                                
                                VertexPoseKeyFrame::PoseRefIterator pit = vpkf->getPoseReferenceIterator();
                                while( pit.hasMoreElements() )
                                {
                                    VertexPoseKeyFrame::PoseRef pr = pit.getNext();
                                    newvpkf->addPoseReference( pr.poseIndex, pr.influence );
                                }
                            }

                        }
                    }
                }
            }
        }           

        return sp;
	}
    // ---------------------------------------------------------
    // set positions and orientations.
    void XMLSkeletonSerializer::readBones2(Skeleton* skel, TiXmlElement* mBonesNode)
    {
        LogManager::getSingleton().logMessage("XMLSkeletonSerializer: Reading Bones data...");

        Bone* btmp;
        Quaternion quat;

        for (TiXmlElement* bonElem = mBonesNode->FirstChildElement();
            bonElem != 0; bonElem = bonElem->NextSiblingElement())
        {
            String name = bonElem->Attribute("name");
            //			int id = StringConverter::parseInt(bonElem->Attribute("id"));

            TiXmlElement* posElem = bonElem->FirstChildElement("position");
            TiXmlElement* rotElem = bonElem->FirstChildElement("rotation");
            TiXmlElement* axisElem = rotElem->FirstChildElement("axis");
            TiXmlElement* scaleElem = bonElem->FirstChildElement("scale");

            Vector3 pos;
            Vector3 axis;
            Radian angle;
            Vector3 scale;

            pos.x = StringConverter::parseReal(posElem->Attribute("x"));
            pos.y = StringConverter::parseReal(posElem->Attribute("y"));
            pos.z = StringConverter::parseReal(posElem->Attribute("z"));

            angle = Radian(StringConverter::parseReal(rotElem->Attribute("angle")));

            axis.x = StringConverter::parseReal(axisElem->Attribute("x"));
            axis.y = StringConverter::parseReal(axisElem->Attribute("y"));
            axis.z = StringConverter::parseReal(axisElem->Attribute("z"));

            // Optional scale
            if (scaleElem)
            {
                // Uniform scale or per axis?
                const char* factorAttrib = scaleElem->Attribute("factor");
                if (factorAttrib)
                {
                    // Uniform scale
                    Real factor = StringConverter::parseReal(factorAttrib);
                    scale = Vector3(factor, factor, factor);
                }
                else
                {
                    // axis scale
                    scale = Vector3::UNIT_SCALE;
                    const char* factorString = scaleElem->Attribute("x");
                    if (factorString)
                    {
                        scale.x = StringConverter::parseReal(factorString);
                    }
                    factorString = scaleElem->Attribute("y");
                    if (factorString)
                    {
                        scale.y = StringConverter::parseReal(factorString);
                    }
                    factorString = scaleElem->Attribute("z");
                    if (factorString)
                    {
                        scale.z = StringConverter::parseReal(factorString);
                    }
                }
            }
            else
            {
                scale = Vector3::UNIT_SCALE;
            }

            /*LogManager::getSingleton().logMessage("bone " + name + " : position("
            + StringConverter::toString(pos.x) + "," + StringConverter::toString(pos.y) + "," + StringConverter::toString(pos.z) + ")"
            + " - angle: " + StringConverter::toString(angle) +" - axe: "
            + StringConverter::toString(axis.x) + "," + StringConverter::toString(axis.y) + "," + StringConverter::toString(axis.z) );
            */

            btmp = skel->getBone(name);

            btmp->setPosition(pos);
            quat.FromAngleAxis(angle, axis);
            btmp->setOrientation(quat);
            btmp->setScale(scale);

        } // bones
    }
Esempio n. 11
0
///***************************************************************************************
/// CalcIK_2D_CCD
/// Given a bone chain located at the origin, this function will perform a single cyclic
/// coordinate descent (CCD) iteration. This finds a solution of bone angles that places
/// the final bone in the given chain at a target position. The supplied bone angles are
/// used to prime the CCD iteration. If a valid solution does not exist, the angles will
/// move as close to the target as possible. The user should resupply the updated angles 
/// until a valid solution is found (or until an iteration limit is met).
///  
/// returns: CCD_Result.Success when a valid solution was found.
///          CCD_Result.Processing when still searching for a valid solution.
///          CCD_Result.Failure when it can get no closer to the target.
///***************************************************************************************
CCD_Result CalcIK_CCD(Bone *startBone, Bone *endBone, CCPoint &targetPoint, float arrivalDist)
{

	double arrivalDistSqr = arrivalDist * arrivalDist;

	//===
	// Track the end effector position (the final bone)
	CCPoint endPoint = ccp(startBone->m_tWorldTransform.tx, startBone->m_tWorldTransform.ty);
	
	CCPoint currentPoint = ccp(0, 0); 

	//===
	// Perform CCD on the bones by optimizing each bone in a loop 
	// from the final bone to the root bone
	bool modifiedBones = false;

	Bone *currentBone = startBone->getParentBone();
	Bone *lastCurrentBone = startBone;

	while(!currentBone->getParentBone() == NULL)
	{
		currentPoint = ccp(currentBone->m_tWorldTransform.tx, currentBone->m_tWorldTransform.ty);

		// Get the vector from the current bone to the end effector position.
 		double curToEndX = endPoint.x - currentPoint.x;
		double curToEndY = endPoint.y - currentPoint.y;
		double curToEndMag = ccpDistance(endPoint, currentPoint);

		// Get the vector from the current bone to the target position.
		double curToTargetX = targetPoint.x - currentPoint.x;
		double curToTargetY = targetPoint.y - currentPoint.y;
		double curToTargetMag = ccpDistance(targetPoint, currentPoint);

		// Get rotation to place the end effector on the line from the current
		// joint position to the target postion.
		double cosRotAng;
		double sinRotAng;
		double endTargetMag = (curToEndMag*curToTargetMag);
		if( endTargetMag <= epsilon )
		{
			cosRotAng = 1;
			sinRotAng = 0;
		}
		else
		{
			cosRotAng = (curToEndX*curToTargetX + curToEndY*curToTargetY) / endTargetMag;
			sinRotAng = (curToEndX*curToTargetY - curToEndY*curToTargetX) / endTargetMag;
		}

        
		// Clamp the cosine into range when computing the angle (might be out of range
		// due to floating point error).
		double rotAng = acos( MAX(-1, MIN(1, cosRotAng) ) );
		if( sinRotAng < 0.0 )
			rotAng = -rotAng;

		// Rotate the end effector position.
		endPoint.x = currentPoint.x + cosRotAng*curToEndX - sinRotAng*curToEndY;
		endPoint.y = currentPoint.y + sinRotAng*curToEndX + cosRotAng*curToEndY;

#if CS_DEBUG_FOR_EDIT
		// Rotate the current bone in local space (this value is output to the user)
		if (dynamic_cast<EditorTween*>(currentBone->getTween()) != 0)
		{


			EditorTween *tween = ((EditorTween*)currentBone->getTween());

			if(currentBone == endBone || currentBone->getChildren()->count() > 1)
			{
				CCPoint p = ccpRotateByAngle(ccp(lastCurrentBone->m_tWorldTransform.tx, lastCurrentBone->m_tWorldTransform.ty), currentPoint, rotAng);

				((EditorDisplayManager*)currentBone->getDisplayManager())->convertPointToSpace(p);

				((EditorTween*)lastCurrentBone->getTween())->editPosition(p.x, p.y);
			}
			else
			{
				rotAng = tween->getRotation() - rotAng;
				rotAng = simplifyAngle(rotAng);

				((EditorTween*)currentBone->getTween())->editRotation(rotAng);
			}



		}
		else
		{
// 			if(currentBone == endBone || currentBone->getChildren()->count() > 1)
// 			{
// 				CCPoint p = rotatePointByPoint(currentPoint, ccp(lastCurrentBone->m_tWorldTransform.tx, lastCurrentBone->m_tWorldTransform.ty), rotAng);
// 				lastCurrentBone->setPosition(p.x, p.y);
// 			}
// 			else
// 			{
// 				currentBone->setRotation(simplifyAngle(currentBone->getRotation() - rotAng));
// 			}
			
		}
#endif
		

		// Check for termination
		double endToTargetX = (targetPoint.x-endPoint.x);
		double endToTargetY = (targetPoint.y-endPoint.y);
		if( endToTargetX*endToTargetX + endToTargetY*endToTargetY <= arrivalDistSqr )
		{
			// We found a valid solution.
			return Success;
		}

		// Track if the arc length that we moved the end effector was
		// a nontrivial distance.
		if( !modifiedBones && fabsf(rotAng)*curToEndMag > trivialArcLength )
		{
			modifiedBones = true;
		}

		if (currentBone == endBone || currentBone->getChildren()->count() > 1)
		{
			break;
		}

		lastCurrentBone = currentBone;
		currentBone = currentBone->getParentBone();
	}


	// We failed to find a valid solution during this iteration.
	if( modifiedBones )
		return Processing;
	else
		return Failure;

}
Esempio n. 12
0
bool Armature::init(const char *name)
{
    bool bRet = false;
    do
    {
        //cocos2d::CCLog("Armature (%s)  create.", name);

		CC_SAFE_DELETE(m_pAnimation);
        m_pAnimation = Animation::create(this);
        CCAssert(m_pAnimation, "create Armature::m_pAnimation fail!");
        m_pAnimation->retain();

		CC_SAFE_DELETE(m_pBoneDic);
        m_pBoneDic	= CCDictionary::create();
        CCAssert(m_pBoneDic, "create Armature::m_pBoneDic fail!");
        m_pBoneDic->retain();

		m_sBlendFunc.src = CC_BLEND_SRC;
		m_sBlendFunc.dst = CC_BLEND_DST;
        

		m_strName = name == NULL ? "" : name;

        ArmatureDataManager *armatureDataManager = ArmatureDataManager::sharedArmatureDataManager();
        
        if(m_strName.compare("") != 0)
        {
            m_strName = name;
            
            AnimationData* animationData = armatureDataManager->getAnimationData(name);
            CCAssert(animationData, "AnimationData not exist! ");
            
            m_pAnimation->setAnimationData(animationData);
            
            
            ArmatureData *armatureData = armatureDataManager->getArmatureData(name);
            CCAssert(armatureData, "");
            
            m_pArmatureData = armatureData;
            
            
            CCDictElement *_element = NULL;
			CCDictionary *boneDataDic = &armatureData->boneDataDic;
            CCDICT_FOREACH(boneDataDic, _element)
            {
                Bone *bone = createBone(_element->getStrKey());
                
                //! init bone's  Tween to 1st movement's 1st frame
                do {
                    
                    MovementData *movData = animationData->getMovement(animationData->movementNames.at(0).c_str());
                    CC_BREAK_IF(!movData);
                    
                    MovementBoneData *movBoneData = movData->getMovementBoneData(bone->getName().c_str());
                    CC_BREAK_IF(!movBoneData || movBoneData->frameList.count() <= 0);
                    
                    FrameData *_frameData = movBoneData->getFrameData(0);
                    CC_BREAK_IF(!_frameData);
                    
                    bone->getTweenData()->copy(_frameData);
                } while (0);
            }
            
        }
Esempio n. 13
0
		void Spawned()
		{
			physics = corpse->game_state->physics_world;

			// get bone pos/ori info
			vector<Mat4> mats = vector<Mat4>();
			unsigned int count = character->skeleton->bones.size();
			for(unsigned int i = 0; i < count; ++i)
			{
				Bone* bone = character->skeleton->bones[i];

				bone_offsets.push_back(bone->rest_pos);
				mats.push_back(whole_xform * bone->GetTransformationMatrix());
			}

			UberModel::BonePhysics** bone_physes = new UberModel::BonePhysics* [count];

			// create rigid bodies
			for(unsigned int i = 0; i < count; ++i)
			{
				Bone* bone = character->skeleton->bones[i];

				Mat4 mat = mats[i];
				float ori_values[] = {mat[0], mat[1], mat[2], mat[4], mat[5], mat[6], mat[8], mat[9], mat[10]};
				bone->ori = Quaternion::FromRotationMatrix(Mat3(ori_values));

				Vec3 bone_pos = mat.TransformVec3(bone_offsets[i], 1);

				UberModel::BonePhysics* phys = NULL;
				for(unsigned int j = 0; j < model->bone_physics.size(); ++j)
					if(Bone::string_table[model->bone_physics[j].bone_name] == bone->name)
						phys = &model->bone_physics[j];

				bone_physes[i] = phys;

				if(phys != NULL)
				{
					btCollisionShape* shape = phys->shape;
					if(shape != NULL)
					{
						RigidBodyInfo* rigid_body = new RigidBodyInfo(shape, MassInfo::FromCollisionShape(shape, phys->mass), bone_pos, bone->ori);

						rigid_body->SetLinearVelocity(initial_vel);

						// these constants taken from the ragdoll demo
						rigid_body->SetDamping(0.05f, 0.85f);
						rigid_body->SetDeactivationTime(0.8f);
						rigid_body->SetSleepingThresholds(1.6f, 2.5f);

						rigid_body->SetFriction(1.0f);
						rigid_body->SetRestitution(0.01f);

						physics->AddRigidBody(rigid_body);
						rigid_bodies.push_back(rigid_body);

						CorpseBoneShootable* shootable = new CorpseBoneShootable(corpse->game_state, corpse, rigid_body, blood_material);
						shootables.push_back(shootable);
						rigid_body->SetCustomCollisionEnabled(shootable);

						bone_indices.push_back(i);
					}
				}
			}

			// create constraints between bones
			for(unsigned int i = 0; i < rigid_bodies.size(); ++i)
			{
				unsigned int bone_index = bone_indices[i];
				UberModel::BonePhysics* phys = bone_physes[bone_index];

				if(phys != NULL)
				{
					Bone* bone = character->skeleton->bones[bone_index];
					Bone* parent = bone->parent;

					if(parent != NULL)
					{
						// find index of parent (bone's index is the same as rigid body info's index)
						for(unsigned int j = 0; j < rigid_bodies.size(); ++j)
						{
							unsigned int j_index = bone_indices[j];

							if(character->skeleton->bones[j_index] == parent)
							{
								if(bone_physes[j_index] != NULL)
								{
									RigidBodyInfo* my_body = rigid_bodies[i];
									RigidBodyInfo* parent_body = rigid_bodies[j];

									ConeTwistConstraint* c = new ConeTwistConstraint(my_body, parent_body, Quaternion::Identity(), Vec3(), phys->ori, phys->pos);
									c->SetLimit(phys->span);
									c->SetDamping(0.1f);							// default is 0.01

									constraints.push_back(c);
									physics->AddConstraint(c, true);				// true = prevent them from colliding normally

									break;
								}
							}
						}
					}
				}
			}

			// emancipate bones
			for(unsigned int i = 0; i < count; ++i)
			{
				Bone* bone = character->skeleton->bones[i];
				bone->parent = NULL;									// orientation and position are no longer relative to a parent!
			}

			if(rigid_bodies.size() == 0)
				corpse->is_valid = false;

			delete[] bone_physes;
		}
Esempio n. 14
0
bool  Skeleton::loadSkeletonFromXML(const std::string &skeletonFileName)
{
	TiXmlDocument* skelDoc = new TiXmlDocument();
	if(!skelDoc->LoadFile(skeletonFileName))
		throw std::logic_error("Load Skeleton File Failed !!");

	TiXmlElement *rootEle = skelDoc->FirstChildElement();
		
	TiXmlElement *bonesElement = static_cast<TiXmlElement*>(rootEle->FirstChild("bones")); 

	if(!bonesElement)
	 	throw std::invalid_argument("Invalid Skeleton File !!!");

	TiXmlElement *boneElement = bonesElement->FirstChildElement();

	while(boneElement)
	{
		TiXmlAttribute *attributes = boneElement->FirstAttribute();

		int parentID, boneID;
 		std::string boneName;
		float length = 0.0;

		boneElement->QueryIntAttribute(    "id", &boneID);
		boneElement->QueryIntAttribute("parent", &parentID);
		boneElement->QueryFloatAttribute( "length", &length);
		boneElement->QueryStringAttribute(  "name", &boneName);
  
		Bone *bone = NULL;
		if(parentID == boneID)
		 	bone = createBone();
		else
		{
			Bone *parent = getBone(parentID);
			bone = createBone(parent);
		}
		bone->name() = boneName;
		mBoneNameToID[boneName] = bone->id();
		
		bone->boneLength() = length;
		//transition
		
 		TiXmlElement *sibElement = boneElement->FirstChildElement();
		sibElement->QueryFloatAttribute("x", &bone->initPose().T.x);
		sibElement->QueryFloatAttribute("y", &bone->initPose().T.y);
		sibElement->QueryFloatAttribute("z", &bone->initPose().T.z);

	 	//orientation
 	 	sibElement = sibElement->NextSiblingElement();
		sibElement->QueryFloatAttribute("w", &bone->initPose().Q.w);
		sibElement->QueryFloatAttribute("x", &bone->initPose().Q.x);
		sibElement->QueryFloatAttribute("y", &bone->initPose().Q.y);
		sibElement->QueryFloatAttribute("z", &bone->initPose().Q.z);
  
	 	//mesh ori
 		sibElement = sibElement->NextSiblingElement();
		sibElement->QueryStringAttribute("name", &bone->meshName());
		sibElement->QueryFloatAttribute("w", &bone->meshOrientation().w);
		sibElement->QueryFloatAttribute("x", &bone->meshOrientation().x);
		sibElement->QueryFloatAttribute("y", &bone->meshOrientation().y);
		sibElement->QueryFloatAttribute("z", &bone->meshOrientation().z);

  		sibElement = sibElement->NextSiblingElement();
		int jointtype;
		sibElement->QueryIntAttribute("jointtype",&jointtype);

		bone->type() = static_cast<Bone::JointType>(jointtype);
		TiXmlElement *limitsElement = NULL;
	
		if(jointtype &0x01)
		{
			limitsElement = sibElement->FirstChildElement();
			limitsElement->QueryFloatAttribute("low", &bone->limitsBox(0).x);
			limitsElement->QueryFloatAttribute("up",  &bone->limitsBox(0).y);
			
			/*bone->limitsBox(0).x = -bone->limitsBox(0).x;
			bone->limitsBox(0).y = -bone->limitsBox(0).y;*/
 		}

		if(jointtype &0x02)
		{
			if(!limitsElement)
				limitsElement = sibElement->FirstChildElement();
			else
				limitsElement = limitsElement->NextSiblingElement();
			limitsElement->QueryFloatAttribute("low", &bone->limitsBox(1).x);
			limitsElement->QueryFloatAttribute( "up", &bone->limitsBox(1).y);

		/*	bone->limitsBox(1).x = -bone->limitsBox(1).x;
			bone->limitsBox(1).y = -bone->limitsBox(1).y;*/

		}

		if(jointtype &0x04)
		{
			if(!limitsElement)
				limitsElement = sibElement->FirstChildElement();
			else
				limitsElement = limitsElement->NextSiblingElement();
			limitsElement->QueryFloatAttribute("low", &bone->limitsBox(2).x);
			limitsElement->QueryFloatAttribute( "up", &bone->limitsBox(2).y);

		/*	bone->limitsBox(2).x = -bone->limitsBox(2).x;
			bone->limitsBox(2).y = -bone->limitsBox(2).y;*/


		}
		
		boneElement = boneElement->NextSiblingElement();
 	}

 	delete skelDoc;
  	mIsBoneCreated = true;
	return true;
}
Esempio n. 15
0
void LeapListener::onFrame(const Controller &controller){
	const Frame frame = controller.frame();
	HandList hands = frame.hands();

	std::vector<HandModel> myhands;
	BoneModel mybone;
	HandModel myhand;
	//printf("frame: %d\n", frame.id());
	float scale = 0.025;
	float disp = 3.5f;
	bool record = true;
	for (int i = 0; i < hands.count(); i++){
		FingerList fingers = hands[i].fingers();
		myhand.bones.clear();
		myhand.references.clear();

		Vector handPosition = hands[i].palmPosition();
		handPosition *= scale;
		handPosition.y -= disp;

		Vector wrist = hands[i].wristPosition();
		wrist *= scale;
		wrist.y -= disp;

		myhand.palmPosition = handPosition;
		myhand.palmNormal = hands[i].palmNormal();
		myhand.direction = hands[i].direction();

		for (int j = 0; j < fingers.count(); j++){
			Bone bone;
			Bone::Type boneType;

			Vector currentPosition = fingers[j].tipPosition();
			currentPosition *= scale;
			currentPosition.y -= disp;

			Vector lastPosition = m_lastFrame.finger(fingers[j].id()).tipPosition();
			lastPosition *= scale;
			lastPosition.y -= disp;

			Vector diff = currentPosition - lastPosition;

			//printf("%f, %f, %f\n", abs(diff.x), abs(diff.y), abs(diff.z));
			
			//if (abs(diff.x) > 0.2 || abs(diff.y) > 0.2 || abs(diff.z) > 0.2) record = false;
			if (abs(diff.x) < 0.0001 || abs(diff.y) < 0.0001 || abs(diff.z) < 0.0001) record = false;

			for (int k = 0; k < 4; k++){
				boneType = static_cast<Bone::Type>(k);
				bone = fingers[j].bone(boneType);

				if (fingers[j].type() == Finger::Type::TYPE_THUMB && k == 0) continue;

				Vector prevPos = bone.prevJoint();
				prevPos *= scale;
				prevPos.y -= disp;

				Vector nextPos = bone.nextJoint();
				nextPos *= scale;
				nextPos.y -= disp;

				mybone.direction = nextPos - prevPos;
				mybone.position = (prevPos + nextPos) / 2;
				mybone.prevJoint = prevPos;
				mybone.nextJoint = nextPos;
				mybone.length = bone.length() * scale;

				if (boneType == Bone::Type::TYPE_PROXIMAL){
					if (fingers[j].type() == Finger::Type::TYPE_THUMB){
						myhand.thumb = prevPos;
						//myhand.references.push_back(nextPos);
					}
					else{
						myhand.references.push_back(prevPos);
					}
				}
				
				myhand.bones.push_back(mybone);
			}
		}

		myhand.base = myhand.references[3] - myhand.references[0];
		Vector disp = myhand.base.normalized() * 0.3f;
		myhand.references[3] += disp;
		myhand.references[0] -= disp;

		//for (int i = 1; i < 5; i++){
		//	printf("%f\n", myhand.references[i - 1].distanceTo(myhand.references[i]));
		//}

		myhands.push_back(myhand);
	}

	if (record){
		m_hands.clear();
		for (int i = 0; i < myhands.size(); i++){
			m_hands.push_back(myhands[i]);
		}
	}

	m_lastFrame = frame;
}
Esempio n. 16
0
Skeleton MoCapManager::createDefaultSkeleton() const
{
    Skeleton skeleton;

    Bone boneData;
    // hip - this will be the anchor of the skeleton
    boneData.setStartPos(Vector3(0.0, 1.0, 0.0));
    boneData.setLength(0.001);
    boneData.setDefaultOrientation(Quaternion(Vector3(0.0, 0.0, 1.0), M_PI*90.0/180.0) * Quaternion(Vector3(-1.0, 0.0, 0.0), M_PI*90.0/180.0));
    boneData.setName("hip");
    int hip = skeleton.createBone(boneData);

    // hip - neck (i.e. the body)
    boneData.setLength(0.55);
    boneData.setDefaultOrientation(Quaternion(Vector3(0.0, 0.0, 1.0), M_PI*90.0/180.0) * Quaternion(Vector3(-1.0, 0.0, 0.0), M_PI*90.0/180.0));
    boneData.setName("body");
    int neck = skeleton.createBone(boneData, hip);

    // neck - head
    boneData.setLength(0.30);
    boneData.setDefaultOrientation(Quaternion(Vector3(0.0, 0.0, 1.0), M_PI*90.0/180.0) * Quaternion(Vector3(-1.0, 0.0, 0.0), M_PI*90.0/180.0));
    boneData.setName("head");
    skeleton.createBone(boneData, neck);

    // neck - right shoulder
    boneData.setLength(0.20);
    boneData.setDefaultOrientation(Quaternion(1.0, 0.0, 0.0, 0.0));
    boneData.setName("rShoulder");
    int rShoulder = skeleton.createBone(boneData, neck);

    // right upper arm
    boneData.setLength(0.35);
    boneData.setDefaultOrientation(Quaternion(1.0, 0.0, 0.0, 0.0));
    boneData.setName("rUpperArm");
    int rUpperArm = skeleton.createBone(boneData, rShoulder);

    // right lower arm
    boneData.setLength(0.30);
    boneData.setDefaultOrientation(Quaternion(1.0, 0.0, 0.0, 0.0));
    boneData.setName("rLowerArm");
    skeleton.createBone(boneData, rUpperArm);

/*
    // right upper arm sensor
    boneData.setLength(0.35);
    boneData.setDefaultOrientation(Quaternion(1.0, 0.0, 0.0, 0.0));
    boneData.setName("rUpperArmSensor");
    int rUpperArmS = skeleton.createBone(boneData, rShoulder);

    // right lower arm sensor
    boneData.setLength(0.30);
    boneData.setDefaultOrientation(Quaternion(1.0, 0.0, 0.0, 0.0));
    boneData.setName("rLowerArmSensor");
    skeleton.createBone(boneData, rUpperArmS);
*/

    // neck - left shoulder
    boneData.setLength(0.20);
    boneData.setDefaultOrientation(Quaternion(Vector3(0.0, 1.0, 0.0), M_PI*180.0/180.0));
    boneData.setName("lShoulder");
    int lShoulder = skeleton.createBone(boneData, neck);

    // left upper arm
    boneData.setLength(0.35);
    boneData.setDefaultOrientation(Quaternion(Vector3(0.0, 1.0, 0.0), M_PI*180.0/180.0));
    boneData.setName("lUpperArm");
    int lUpperArm = skeleton.createBone(boneData, lShoulder);

    // left lower arm
    boneData.setLength(0.30);
    boneData.setDefaultOrientation(Quaternion(Vector3(0.0, 1.0, 0.0), M_PI*180.0/180.0));
    boneData.setName("lLowerArm");
    skeleton.createBone(boneData, lUpperArm);

/*
    // left upper arm sensor
    boneData.setLength(0.35);
    boneData.setDefaultOrientation(Quaternion(Vector3(0.0, 1.0, 0.0), M_PI*180.0/180.0));
    boneData.setName("lUpperArmSensor");
    int lUpperArmS = skeleton.createBone(boneData, lShoulder);

    // left lower arm sensor
    boneData.setLength(0.30);
    boneData.setDefaultOrientation(Quaternion(Vector3(0.0, 1.0, 0.0), M_PI*180.0/180.0));
    boneData.setName("lLowerArmSensor");
    skeleton.createBone(boneData, lUpperArmS);
*/

    // hip - right leg
    boneData.setLength(0.20);
    boneData.setDefaultOrientation(Quaternion(1.0, 0.0, 0.0, 0.0));
    boneData.setName("rHip");
    int rHip = skeleton.createBone(boneData, hip);

    // right upper leg
    boneData.setLength(0.45);
    boneData.setDefaultOrientation(Quaternion(Vector3(0.0, 0.0, -1.0), M_PI*90.0/180.0) * Quaternion(Vector3(-1.0, 0.0, 0.0), M_PI*90.0/180.0));
    boneData.setName("rUpperLeg");
    int rUpperLeg = skeleton.createBone(boneData, rHip);

    // right lower leg
    boneData.setLength(0.45);
    boneData.setDefaultOrientation(Quaternion(Vector3(0.0, 0.0, -1.0), M_PI*90.0/180.0) * Quaternion(Vector3(-1.0, 0.0, 0.0), M_PI*90.0/180.0));
    boneData.setName("rLowerLeg");
    skeleton.createBone(boneData, rUpperLeg);

    // hip - left leg
    boneData.setLength(0.20);
    boneData.setDefaultOrientation(Quaternion(Vector3(0.0, 1.0, 0.0), M_PI*180.0/180.0));
    boneData.setName("lHip");
    int lHip = skeleton.createBone(boneData, hip);

    // left upper leg
    boneData.setLength(0.45);
    boneData.setDefaultOrientation(Quaternion(Vector3(0.0, 0.0, -1.0), M_PI*90.0/180.0) * Quaternion(Vector3(1.0, 0.0, 0.0), M_PI*90.0/180.0));
    boneData.setName("lUpperLeg");
    int lUpperLeg = skeleton.createBone(boneData, lHip);

    // left lower leg
    boneData.setLength(0.45);
    boneData.setDefaultOrientation(Quaternion(Vector3(0.0, 0.0, -1.0), M_PI*90.0/180.0) * Quaternion(Vector3(1.0, 0.0, 0.0), M_PI*90.0/180.0));
    boneData.setName("lLowerLeg");
    skeleton.createBone(boneData, lUpperLeg);

    skeleton.setName("Default Skeleton");
    skeleton.update();
    //skeleton.setCurrentAsDefault();
    return skeleton;
}
Esempio n. 17
0
void RagDoll::InitRagdoll() {
    Release();

    InitBones((Bone*)m_pRootBone);

    Bone* Head_End = (Bone*)D3DXFrameFind(m_pRootBone, "Head_End");
    Bone* Head = (Bone*)D3DXFrameFind(m_pRootBone, "Head");
    Bone* Neck = (Bone*)D3DXFrameFind(m_pRootBone, "Neck");
    Bone* Pelvis = (Bone*)D3DXFrameFind(m_pRootBone, "Pelvis");
    Bone* Spine = (Bone*)D3DXFrameFind(m_pRootBone, "Spine");
    Bone* R_Shoulder = (Bone*)D3DXFrameFind(m_pRootBone, "Shoulder_Right");
    Bone* L_Shoulder = (Bone*)D3DXFrameFind(m_pRootBone, "Shoulder_Left");
    Bone* U_R_Arm = (Bone*)D3DXFrameFind(m_pRootBone, "Upper_Arm_Right");
    Bone* U_L_Arm = (Bone*)D3DXFrameFind(m_pRootBone, "Upper_Arm_Left");
    Bone* L_R_Arm = (Bone*)D3DXFrameFind(m_pRootBone, "Lower_Arm_Right");
    Bone* L_L_Arm = (Bone*)D3DXFrameFind(m_pRootBone, "Lower_Arm_Left");
    Bone* R_Hand = (Bone*)D3DXFrameFind(m_pRootBone, "Hand_Right");
    Bone* L_Hand = (Bone*)D3DXFrameFind(m_pRootBone, "Hand_Left");

    Bone* R_Pelvis = (Bone*)D3DXFrameFind(m_pRootBone, "Pelvis_Right");
    Bone* L_Pelvis = (Bone*)D3DXFrameFind(m_pRootBone, "Pelvis_Left");
    Bone* R_Thigh = (Bone*)D3DXFrameFind(m_pRootBone, "Thigh_Right");
    Bone* L_Thigh = (Bone*)D3DXFrameFind(m_pRootBone, "Thigh_Left");
    Bone* R_Calf = (Bone*)D3DXFrameFind(m_pRootBone, "Calf_Right");
    Bone* L_Calf = (Bone*)D3DXFrameFind(m_pRootBone, "Calf_Left");
    Bone* R_Foot = (Bone*)D3DXFrameFind(m_pRootBone, "Foot_Right");
    Bone* L_Foot = (Bone*)D3DXFrameFind(m_pRootBone, "Foot_Left");
    Bone* R_Foot_End = (Bone*)D3DXFrameFind(m_pRootBone, "Foot_Right_End");
    Bone* L_Foot_End = (Bone*)D3DXFrameFind(m_pRootBone, "Foot_Left_End");

    D3DXQUATERNION q, q2;
    D3DXQuaternionIdentity(&q);
    D3DXQuaternionRotationAxis(&q2, &D3DXVECTOR3(0.0f, 0.0f, 1.0f), D3DX_PI * -0.5f);

    //Head
    OBB* o01 = CreateBoneBox(Head, Head_End, D3DXVECTOR3(0.3f, 0.2f, 0.2f), q);

    //Right Arm
    OBB* o03 = CreateBoneBox(U_R_Arm, L_R_Arm, D3DXVECTOR3(0.3f, 0.12f, 0.12f), q);
    OBB* o04 = CreateBoneBox(L_R_Arm, R_Hand, D3DXVECTOR3(0.3f, 0.12f, 0.12f), q);

    //Left Arm
    OBB* o06 = CreateBoneBox(U_L_Arm, L_L_Arm, D3DXVECTOR3(0.3f, 0.12f, 0.12f), q);
    OBB* o07 = CreateBoneBox(L_L_Arm, L_Hand, D3DXVECTOR3(0.3f, 0.12f, 0.12f), q);

    //Spine
    OBB* o08 = CreateBoneBox(Spine, Neck, D3DXVECTOR3(0.5f, 0.35f, 0.15f), q);

    //Right Leg
    OBB* o09 = CreateBoneBox(R_Thigh, R_Calf, D3DXVECTOR3(0.4f, 0.15f, 0.15f), q);
    OBB* o10 = CreateBoneBox(R_Calf, R_Foot, D3DXVECTOR3(0.4f, 0.14f, 0.14f), q);
    OBB* o11 = CreateBoneBox(R_Foot, R_Foot_End, D3DXVECTOR3(0.15f, 0.06f, 0.12f), q2);

    //Left Leg
    OBB* o12 = CreateBoneBox(L_Thigh, L_Calf, D3DXVECTOR3(0.4f, 0.15f, 0.15f), q);
    OBB* o13 = CreateBoneBox(L_Calf, L_Foot, D3DXVECTOR3(0.4f, 0.14f, 0.14f), q);
    OBB* o14 = CreateBoneBox(L_Foot, L_Foot_End, D3DXVECTOR3(0.15f, 0.06f, 0.12f), q2);

    //Constraints
    CreateTwistCone(Neck, o01, o08, D3DX_PI * 0.15f, D3DXVECTOR3(0.0f, 0.0f, D3DX_PI * 0.5f), D3DXVECTOR3(0.0f, 0.0f, D3DX_PI * 0.5f));

    CreateTwistCone(L_Pelvis, o08, o12, D3DX_PI * 0.5f, D3DXVECTOR3(0.3f, D3DX_PI * 0.5f, -D3DX_PI * 0.5f), D3DXVECTOR3(0.0f, 0.0f, D3DX_PI * 0.5f));
    CreateHinge(L_Calf, o12, o13, 0.0f, -2.0f, D3DXVECTOR3(0.0f, 0.0f, D3DX_PI * 0.5f), D3DXVECTOR3(0.0f, 0.0f, D3DX_PI * 0.5f));
    CreateHinge(L_Foot, o13, o14, 1.5f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, D3DX_PI * 0.5f), D3DXVECTOR3(0.0f, 0.0f, D3DX_PI * 0.5f));

    CreateTwistCone(R_Pelvis, o08, o09, D3DX_PI * 0.5f, D3DXVECTOR3(0.3f, D3DX_PI * 0.5f, -D3DX_PI * 0.5f), D3DXVECTOR3(0.0f, 0.0f, D3DX_PI * 0.5f));
    CreateHinge(R_Calf, o09, o10, 0.0f, -2.0f, D3DXVECTOR3(0.0f, 0.0f, D3DX_PI * 0.5f), D3DXVECTOR3(0.0f, 0.0f, D3DX_PI * 0.5f));
    CreateHinge(R_Foot, o10, o11, 1.5f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, D3DX_PI * 0.5f), D3DXVECTOR3(0.0f, 0.0f, D3DX_PI * 0.5f));

    CreateTwistCone(U_L_Arm, o08, o06, D3DX_PI * 0.6f, D3DXVECTOR3(0.0f, D3DX_PI * 0.75f, D3DX_PI * 0.0f), D3DXVECTOR3(0.0f, 0.0f, 0.0f));
    CreateHinge(L_L_Arm, o06, o07, 2.0f, 0.0f, D3DXVECTOR3(0.0f, 0.0f, D3DX_PI * 0.5f), D3DXVECTOR3(0.0f, 0.0f, D3DX_PI * 0.5f));

    CreateTwistCone(U_R_Arm, o08, o03, D3DX_PI * 0.6f, D3DXVECTOR3(0.0f, D3DX_PI * 0.75f, D3DX_PI * 0.0f), D3DXVECTOR3(D3DX_PI, 0.0f, 0.0f));
    CreateHinge(L_R_Arm, o03, o04, 2.0f, 0.0f, D3DXVECTOR3(0.0f, 0.0f, -D3DX_PI * 0.5f), D3DXVECTOR3(0.0f, 0.0f, -D3DX_PI * 0.5f));

    //Assign Pelvis bone to Spine OBB
    Pelvis->m_pObb = o08;
    D3DXVECTOR3 PelvisPos(Pelvis->CombinedTransformationMatrix(3, 0),
                          Pelvis->CombinedTransformationMatrix(3, 1),
                          Pelvis->CombinedTransformationMatrix(3, 2));

    Pelvis->m_pivot = o08->SetPivot(PelvisPos);
}
Esempio n. 18
0
    //---------------------------------------------------------------------
    void Skeleton::_dumpContents(const String& filename)
    {
        std::ofstream of;

        Quaternion q;
        Radian angle;
        Vector3 axis;
        of.open(filename.c_str());

        of << "-= Debug output of skeleton " << mName << " =-" << std::endl << std::endl;
        of << "== Bones ==" << std::endl;
        of << "Number of bones: " << (unsigned int)mBoneList.size() << std::endl;
        
        BoneList::iterator bi;
        for (bi = mBoneList.begin(); bi != mBoneList.end(); ++bi)
        {
            Bone* bone = *bi;

            of << "-- Bone " << bone->getHandle() << " --" << std::endl;
            of << "Position: " << bone->getPosition();
            q = bone->getOrientation();
            of << "Rotation: " << q;
            q.ToAngleAxis(angle, axis);
            of << " = " << angle.valueRadians() << " radians around axis " << axis << std::endl << std::endl;
        }

        of << "== Animations ==" << std::endl;
        of << "Number of animations: " << (unsigned int)mAnimationsList.size() << std::endl;

        AnimationList::iterator ai;
        for (ai = mAnimationsList.begin(); ai != mAnimationsList.end(); ++ai)
        {
            Animation* anim = ai->second;

            of << "-- Animation '" << anim->getName() << "' (length " << anim->getLength() << ") --" << std::endl;
            of << "Number of tracks: " << anim->getNumNodeTracks() << std::endl;

            int ti;
            for (ti = 0; ti < anim->getNumNodeTracks(); ++ti)
            {
                NodeAnimationTrack* track = anim->getNodeTrack(ti);
                of << "  -- AnimationTrack " << ti << " --" << std::endl;
                of << "  Affects bone: " << ((Bone*)track->getAssociatedNode())->getHandle() << std::endl;
                of << "  Number of keyframes: " << track->getNumKeyFrames() << std::endl;

                int ki;
                
                for (ki = 0; ki < track->getNumKeyFrames(); ++ki)
                {
                    TransformKeyFrame* key = track->getNodeKeyFrame(ki);
                    of << "    -- KeyFrame " << ki << " --" << std::endl;
                    of << "    Time index: " << key->getTime(); 
                    of << "    Translation: " << key->getTranslate() << std::endl;
                    q = key->getRotation();
                    of << "    Rotation: " << q;
                    q.ToAngleAxis(angle, axis);
                    of << " = " << angle.valueRadians() << " radians around axis " << axis << std::endl;
                }

            }



        }

    }
Esempio n. 19
0
void NormalEnemy::setArmatureBody()
{
    //armature->getAnimation()->setMovementEventCallFunc(this, movementEvent_selector(NormalEnemy::animationEvent));
    
    armature->getAnimation()->playWithIndex(0);
    
    Vector<Node*> bonearr = armature->getChildren();
    //printf("bonearr size = %zd\n",bonearr.size());
    //Skin *dump = (Skin*)((Bone*)(bonearr.at(2)))->getDisplayRenderNode();
    //batch = SpriteBatchNode::createWithTexture(dump->getTexture());
    //gameScene->addChild(batch, armature->getZOrder());
    

    
    // int z = bonearr->count();
    for(int i = 0; i< bonearr.size();i++)
    {
        Bone *bone = (Bone*)bonearr.at(i);
        string boneName = bone->getName();
        Skin *skin = (Skin*)bone->getDisplayRenderNode();
        
        
        if (skin !=NULL) {
            skin->isphysicsObject = true;
            skin->parentScale = armature->getScale();
            Rect a = skin->getTextureRect();
            
            Point partpos = skin->getWorldPosition();
            float partrotation = skin->getWorldRotation();
            float bodyrotation = partrotation*M_PI/180.0;
            Size partSize = Size((a.getMaxX()-a.getMinX())/PTM_RATIO*armature->getScale(), (a.getMaxY()-a.getMinY())/PTM_RATIO*armature->getScale());
            
            b2BodyDef bodyDef;
            bodyDef.type = b2_staticBody;
            
            bodyDef.position.Set(partpos.x/PTM_RATIO, partpos.y/PTM_RATIO);
            b2Body *body_ = gameWorld->CreateBody(&bodyDef);
            
            b2PolygonShape dynamicBox;
            dynamicBox.SetAsBox(partSize.width/2.0, partSize.height/2.0);//These are mid points for our 1m box
            b2CircleShape circleShape;
            circleShape.m_radius = 0.45;
            
            b2FixtureDef fixtureDef;
            if (boneName.compare("headbone") == 0||boneName.compare("head")==0) {
                fixtureDef.shape = &circleShape;

            }
            else if (boneName.compare("left_feet") == 0||boneName.compare("foot_right")==0)
            {
                circleShape.m_radius = 0.2;
                fixtureDef.shape = &circleShape;

            }
            else {
                fixtureDef.shape = &dynamicBox;
            }
            fixtureDef.density = 0.2f;
            fixtureDef.restitution = 0.8;
            fixtureDef.friction = 0.2f;
            fixtureDef.filter.categoryBits = ZOMBIE;
            
           // printf("bonename = %s\n", boneName.c_str());
            
            if (boneName.compare("headbone") == 0||boneName.compare("head")==0) {
                fixtureDef.fixturetype = f_zbody_body;
                fixtureDef.filter.maskBits = BASE_GROUND | UPPER_GROUND | ARROW | BULLET;
            }
            else if (boneName.compare("bodybone") == 0||boneName.compare("body")==0) {
                fixtureDef.fixturetype = f_zbody_body;
                fixtureDef.filter.maskBits = BASE_GROUND | UPPER_GROUND | ARROW | BULLET;
            }
            else if (boneName.compare("leglbone") == 0 || boneName.compare("right_leg") == 0|| boneName.compare("right_foreleg")|| boneName.compare("foreleg_rbone") ||boneName.compare("left_leg")==0||boneName.compare("left_foreleg")==0) {
                fixtureDef.fixturetype = f_zbody_body;
                fixtureDef.filter.maskBits = BASE_GROUND | UPPER_GROUND | ARROW | BULLET;
            }
            else {
                fixtureDef.fixturetype = f_zbody_body;
                fixtureDef.filter.maskBits = BASE_GROUND | UPPER_GROUND;
            }
            
            
            
            body_->CreateFixture(&fixtureDef);
            body_->SetUserData(this);
            body_->SetTransform(b2Vec2(partpos.x/PTM_RATIO, partpos.y/PTM_RATIO), bodyrotation);
            body_->SetAngularDamping(1.2);
            
            skin->body = body_;
            
            
            PhysicsSprite *dumpSprite = (PhysicsSprite*)PhysicsSprite::createWithTexture(skin->getTexture(), skin->getTextureRect());
            dumpSprite->setPosition(skin->getWorldPosition());
            dumpSprite->setScale(armature->getScale());
            dumpSprite->setVisible(false);
            dumpSprite->body = body_;
            // printf("zorder = %i\n", bone->getZOrder());
           // dumpSprite->setZOrder(bone->getZOrder()+3);
            gameScene->addChild(dumpSprite,bone->getZOrder()+20);
            //deadSpriteArray->addObject((Ref*)dumpSprite);
            deadSpriteArray.pushBack(dumpSprite);
            
            
            bodies.push_back(body_);
        }
        /*Bone *bone = armature->getBone(key->getCString());
         printf("boneX = %f\n", bone->getPositionX());
         printf("boneY = %f\n", bone->getPositionY());*/
    }
    
}
Esempio n. 20
0
    void Skeleton::_mergeSkeletonAnimations(const Skeleton* src,
        const BoneHandleMap& boneHandleMap,
        const StringVector& animations)
    {
        ushort handle;

        ushort numSrcBones = src->getNumBones();
        ushort numDstBones = this->getNumBones();

        if (boneHandleMap.size() != numSrcBones)
        {
            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
                "Number of bones in the bone handle map must equal to "
                "number of bones in the source skeleton.",
                "Skeleton::_mergeSkeletonAnimations");
        }

        bool existsMissingBone = false;

        // Check source skeleton structures compatible with ourself (that means
        // identically bones with identical handles, and with same hierarchy, but
        // not necessary to have same number of bones and bone names).
        for (handle = 0; handle < numSrcBones; ++handle)
        {
            const Bone* srcBone = src->getBone(handle);
            ushort dstHandle = boneHandleMap[handle];

            // Does it exists in target skeleton?
            if (dstHandle < numDstBones)
            {
                Bone* destBone = this->getBone(dstHandle);

                // Check both bones have identical parent, or both are root bone.
                const Bone* srcParent = static_cast<Bone*>(srcBone->getParent());
                Bone* destParent = static_cast<Bone*>(destBone->getParent());
                if ((srcParent || destParent) &&
                    (!srcParent || !destParent ||
                     boneHandleMap[srcParent->getHandle()] != destParent->getHandle()))
                {
                    OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
                        "Source skeleton incompatible with this skeleton: "
                        "difference hierarchy between bone '" + srcBone->getName() +
                        "' and '" + destBone->getName() + "'.",
                        "Skeleton::_mergeSkeletonAnimations");
                }
            }
            else
            {
                existsMissingBone = true;
            }
        }

        // Clone bones if need
        if (existsMissingBone)
        {
            // Create missing bones
            for (handle = 0; handle < numSrcBones; ++handle)
            {
                const Bone* srcBone = src->getBone(handle);
                ushort dstHandle = boneHandleMap[handle];

                // The bone is missing in target skeleton?
                if (dstHandle >= numDstBones)
                {
                    Bone* dstBone = this->createBone(srcBone->getName(), dstHandle);
                    // Sets initial transform
                    dstBone->setPosition(srcBone->getInitialPosition());
                    dstBone->setOrientation(srcBone->getInitialOrientation());
                    dstBone->setScale(srcBone->getInitialScale());
                    dstBone->setInitialState();
                }
            }

            // Link new bones to parent
            for (handle = 0; handle < numSrcBones; ++handle)
            {
                const Bone* srcBone = src->getBone(handle);
                ushort dstHandle = boneHandleMap[handle];

                // Is new bone?
                if (dstHandle >= numDstBones)
                {
                    const Bone* srcParent = static_cast<Bone*>(srcBone->getParent());
                    if (srcParent)
                    {
                        Bone* destParent = this->getBone(boneHandleMap[srcParent->getHandle()]);
                        Bone* dstBone = this->getBone(dstHandle);
                        destParent->addChild(dstBone);
                    }
                }
            }

            // Derive root bones in case it was changed
            this->deriveRootBone();

            // Reset binding pose for new bones
            this->reset(true);
            this->setBindingPose();
        }

        //
        // We need to adapt animations from source to target skeleton, but since source
        // and target skeleton bones bind transform might difference, so we need to alter
        // keyframes in source to suit to target skeleton.
        //
        // For any given animation time, formula:
        //
        //      LocalTransform = BindTransform * KeyFrame;
        //      DerivedTransform = ParentDerivedTransform * LocalTransform
        //
        // And all derived transforms should be keep identically after adapt to
        // target skeleton, Then:
        //
        //      DestDerivedTransform == SrcDerivedTransform
        //      DestParentDerivedTransform == SrcParentDerivedTransform
        // ==>
        //      DestLocalTransform = SrcLocalTransform
        // ==>
        //      DestBindTransform * DestKeyFrame = SrcBindTransform * SrcKeyFrame
        // ==>
        //      DestKeyFrame = inverse(DestBindTransform) * SrcBindTransform * SrcKeyFrame
        //
        // We define (inverse(DestBindTransform) * SrcBindTransform) as 'delta-transform' here.
        //

        // Calculate delta-transforms for all source bones.
        std::vector<DeltaTransform> deltaTransforms(numSrcBones);
        for (handle = 0; handle < numSrcBones; ++handle)
        {
            const Bone* srcBone = src->getBone(handle);
            DeltaTransform& deltaTransform = deltaTransforms[handle];
            ushort dstHandle = boneHandleMap[handle];

            if (dstHandle < numDstBones)
            {
                // Common bone, calculate delta-transform

                Bone* dstBone = this->getBone(dstHandle);

                deltaTransform.translate = srcBone->getInitialPosition() - dstBone->getInitialPosition();
                deltaTransform.rotate = dstBone->getInitialOrientation().Inverse() * srcBone->getInitialOrientation();
                deltaTransform.scale = srcBone->getInitialScale() / dstBone->getInitialScale();

                // Check whether or not delta-transform is identity
                const Real tolerance = 1e-3f;
                Vector3 axis;
                Radian angle;
                deltaTransform.rotate.ToAngleAxis(angle, axis);
                deltaTransform.isIdentity =
                    deltaTransform.translate.positionEquals(Vector3::ZERO, tolerance) &&
                    deltaTransform.scale.positionEquals(Vector3::UNIT_SCALE, tolerance) &&
                    Math::RealEqual(angle.valueRadians(), 0.0f, tolerance);
            }
            else
            {
                // New bone, the delta-transform is identity

                deltaTransform.translate = Vector3::ZERO;
                deltaTransform.rotate = Quaternion::IDENTITY;
                deltaTransform.scale = Vector3::UNIT_SCALE;
                deltaTransform.isIdentity = true;
            }
        }

        // Now copy animations

        ushort numAnimations;
        if (animations.empty())
            numAnimations = src->getNumAnimations();
        else
            numAnimations = static_cast<ushort>(animations.size());
        for (ushort i = 0; i < numAnimations; ++i)
        {
            const Animation* srcAnimation;
            if (animations.empty())
            {
                // Get animation of source skeleton by the given index
                srcAnimation = src->getAnimation(i);
            }
            else
            {
                // Get animation of source skeleton by the given name
                const LinkedSkeletonAnimationSource* linker;
                srcAnimation = src->_getAnimationImpl(animations[i], &linker);
                if (!srcAnimation || linker)
                {
                    OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
                        "No animation entry found named " + animations[i],
                        "Skeleton::_mergeSkeletonAnimations");
                }
            }

            // Create target animation
            Animation* dstAnimation = this->createAnimation(srcAnimation->getName(), srcAnimation->getLength());

            // Copy interpolation modes
            dstAnimation->setInterpolationMode(srcAnimation->getInterpolationMode());
            dstAnimation->setRotationInterpolationMode(srcAnimation->getRotationInterpolationMode());

            // Copy track for each bone
            for (handle = 0; handle < numSrcBones; ++handle)
            {
                const DeltaTransform& deltaTransform = deltaTransforms[handle];
                ushort dstHandle = boneHandleMap[handle];

                if (srcAnimation->hasNodeTrack(handle))
                {
                    // Clone track from source animation

                    const NodeAnimationTrack* srcTrack = srcAnimation->getNodeTrack(handle);
                    NodeAnimationTrack* dstTrack = dstAnimation->createNodeTrack(dstHandle, this->getBone(dstHandle));
                    dstTrack->setUseShortestRotationPath(srcTrack->getUseShortestRotationPath());

                    ushort numKeyFrames = srcTrack->getNumKeyFrames();
                    for (ushort k = 0; k < numKeyFrames; ++k)
                    {
                        const TransformKeyFrame* srcKeyFrame = srcTrack->getNodeKeyFrame(k);
                        TransformKeyFrame* dstKeyFrame = dstTrack->createNodeKeyFrame(srcKeyFrame->getTime());

                        // Adjust keyframes to match target binding pose
                        if (deltaTransform.isIdentity)
                        {
                            dstKeyFrame->setTranslate(srcKeyFrame->getTranslate());
                            dstKeyFrame->setRotation(srcKeyFrame->getRotation());
                            dstKeyFrame->setScale(srcKeyFrame->getScale());
                        }
                        else
                        {
                            dstKeyFrame->setTranslate(deltaTransform.translate + srcKeyFrame->getTranslate());
                            dstKeyFrame->setRotation(deltaTransform.rotate * srcKeyFrame->getRotation());
                            dstKeyFrame->setScale(deltaTransform.scale * srcKeyFrame->getScale());
                        }
                    }
                }
                else if (!deltaTransform.isIdentity)
                {
                    // Create 'static' track for this bone

                    NodeAnimationTrack* dstTrack = dstAnimation->createNodeTrack(dstHandle, this->getBone(dstHandle));
                    TransformKeyFrame* dstKeyFrame;

                    dstKeyFrame = dstTrack->createNodeKeyFrame(0);
                    dstKeyFrame->setTranslate(deltaTransform.translate);
                    dstKeyFrame->setRotation(deltaTransform.rotate);
                    dstKeyFrame->setScale(deltaTransform.scale);

                    dstKeyFrame = dstTrack->createNodeKeyFrame(dstAnimation->getLength());
                    dstKeyFrame->setTranslate(deltaTransform.translate);
                    dstKeyFrame->setRotation(deltaTransform.rotate);
                    dstKeyFrame->setScale(deltaTransform.scale);
                }
            }
        }
    }
Esempio n. 21
0
void Skeleton::loadSkeleton(const String& fileName) {
	OSFILE *inFile = OSBasics::open(fileName.c_str(), "rb");
	if(!inFile) {
		return;
	}
	
	bonesEntity	= new SceneEntity();
	bonesEntity->visible = false;
	addChild(bonesEntity);
	
	unsigned int numBones;
	float t[3],rq[4],s[3];
	
	OSBasics::read(&numBones, sizeof(unsigned int), 1, inFile);
	unsigned int namelen;
	char buffer[1024];
	
	Matrix4 mat;
	unsigned int hasParent, boneID;
	for(int i=0; i < numBones; i++) {
		
		OSBasics::read(&namelen, sizeof(unsigned int), 1, inFile);
		memset(buffer, 0, 1024);
		OSBasics::read(buffer, 1, namelen, inFile);
		
		Bone *newBone = new Bone(String(buffer));
		
		OSBasics::read(&hasParent, sizeof(unsigned int), 1, inFile);
		if(hasParent == 1) {
			OSBasics::read(&boneID, sizeof(unsigned int), 1, inFile);
			newBone->parentBoneId = boneID;
		} else {
			newBone->parentBoneId = -1;
		}

		OSBasics::read(t, sizeof(float), 3, inFile);
		OSBasics::read(s, sizeof(float), 3, inFile);
		OSBasics::read(rq, sizeof(float), 4, inFile);
		
		bones.push_back(newBone);
		
		newBone->setPosition(t[0], t[1], t[2]);
		newBone->setRotationQuat(rq[0], rq[1], rq[2], rq[3]);
		newBone->setScale(s[0], s[1], s[2]);
		newBone->rebuildTransformMatrix();
		
		newBone->setBaseMatrix(newBone->getTransformMatrix());
		newBone->setBoneMatrix(newBone->getTransformMatrix());

		OSBasics::read(t, sizeof(float), 3, inFile);
		OSBasics::read(s, sizeof(float), 3, inFile);
		OSBasics::read(rq, sizeof(float), 4, inFile);
		
		Quaternion q;
		q.set(rq[0], rq[1], rq[2], rq[3]);
		Matrix4 m = q.createMatrix();
		m.setPosition(t[0], t[1], t[2]);
		
		newBone->setRestMatrix(m);
		
	}

	Bone *parentBone;
//	SceneEntity *bProxy;
	
	for(int i=0; i < bones.size(); i++) {
		if(bones[i]->parentBoneId != -1) {
			parentBone = bones[bones[i]->parentBoneId];
			parentBone->addChildBone(bones[i]);
			bones[i]->setParentBone(parentBone);
			parentBone->addEntity(bones[i]);			
//			addEntity(bones[i]);										
			
			SceneLine *connector = new SceneLine(bones[i], parentBone);
			connector->depthTest = false;
			bonesEntity->addEntity(connector);				
			connector->setColor(((Number)(rand() % RAND_MAX)/(Number)RAND_MAX),((Number)(rand() % RAND_MAX)/(Number)RAND_MAX),((Number)(rand() % RAND_MAX)/(Number)RAND_MAX),1.0f);
		} else {
//			bProxy = new SceneEntity();
//			addEntity(bProxy);			
//			bProxy->addEntity(bones[i]);
			bonesEntity->addChild(bones[i]);
		}
	//	bones[i]->visible = false;			
	}
	OSBasics::close(inFile);
}
Esempio n. 22
0
bool BoneAlphaSort(const Bone &lhs, const Bone &rhs)
{
	return (lhs.getName() < rhs.getName());
}
Esempio n. 23
0
	void Scale( float scaling_factor ) { m_RootBone.Scale_r( scaling_factor ); }
Esempio n. 24
0
void Skeleton::loadSkeleton(const String& fileName) {
	OSFILE *inFile = OSBasics::open(fileName.c_str(), "rb");
	if(!inFile) {
		return;
	}
	
	bonesEntity	= new SceneEntity();
	bonesEntity->visible = false;
	addChild(bonesEntity);
	
	unsigned int numBones;
	float t[3],rq[4],s[3];
	
	OSBasics::read(&numBones, sizeof(unsigned int), 1, inFile);
	unsigned int namelen;
	char buffer[1024];
	
	Matrix4 mat;
	unsigned int hasParent, boneID;
	for(int i=0; i < numBones; i++) {
		
		OSBasics::read(&namelen, sizeof(unsigned int), 1, inFile);
		memset(buffer, 0, 1024);
		OSBasics::read(buffer, 1, namelen, inFile);
		
		Bone *newBone = new Bone(String(buffer));
		
		OSBasics::read(&hasParent, sizeof(unsigned int), 1, inFile);
		if(hasParent == 1) {
			OSBasics::read(&boneID, sizeof(unsigned int), 1, inFile);
			newBone->parentBoneId = boneID;
		} else {
			newBone->parentBoneId = -1;
		}

		OSBasics::read(t, sizeof(float), 3, inFile);
		OSBasics::read(s, sizeof(float), 3, inFile);
		OSBasics::read(rq, sizeof(float), 4, inFile);
		
		bones.push_back(newBone);
		
		newBone->setPosition(t[0], t[1], t[2]);
		newBone->setRotationQuat(rq[0], rq[1], rq[2], rq[3]);
		newBone->setScale(s[0], s[1], s[2]);
		newBone->rebuildTransformMatrix();
		
		newBone->setBaseMatrix(newBone->getTransformMatrix());
		newBone->setBoneMatrix(newBone->getTransformMatrix());

		OSBasics::read(t, sizeof(float), 3, inFile);
		OSBasics::read(s, sizeof(float), 3, inFile);
		OSBasics::read(rq, sizeof(float), 4, inFile);
		
		Quaternion q;
		q.set(rq[0], rq[1], rq[2], rq[3]);
		Matrix4 m = q.createMatrix();
		m.setPosition(t[0], t[1], t[2]);
		
		newBone->setRestMatrix(m);
		
	}

	Bone *parentBone;
//	SceneEntity *bProxy;
	
	for(int i=0; i < bones.size(); i++) {
		if(bones[i]->parentBoneId != -1) {
			parentBone = bones[bones[i]->parentBoneId];
			parentBone->addChildBone(bones[i]);
			bones[i]->setParentBone(parentBone);
			parentBone->addEntity(bones[i]);			
//			addEntity(bones[i]);										
			
			SceneLine *connector = new SceneLine(bones[i], parentBone);
			connector->depthTest = false;
			bonesEntity->addEntity(connector);				
			connector->setColor(((Number)(rand() % RAND_MAX)/(Number)RAND_MAX),((Number)(rand() % RAND_MAX)/(Number)RAND_MAX),((Number)(rand() % RAND_MAX)/(Number)RAND_MAX),1.0f);
		} else {
//			bProxy = new SceneEntity();
//			addEntity(bProxy);			
//			bProxy->addEntity(bones[i]);
			bonesEntity->addChild(bones[i]);
		}
	//	bones[i]->visible = false;			
	}
	/*
	unsigned int numAnimations, activeBones,boneIndex,numPoints,numCurves, curveType;
	OSBasics::read(&numAnimations, sizeof(unsigned int), 1, inFile);
	//Logger::log("numAnimations: %d\n", numAnimations);
	for(int i=0; i < numAnimations; i++) {
		OSBasics::read(&namelen, sizeof(unsigned int), 1, inFile);
		memset(buffer, 0, 1024);
		OSBasics::read(buffer, 1, namelen, inFile);
		float length;
		OSBasics::read(&length, 1, sizeof(float), inFile);
		SkeletonAnimation *newAnimation = new SkeletonAnimation(buffer, length);
		
		OSBasics::read(&activeBones, sizeof(unsigned int), 1, inFile);

	//	Logger::log("activeBones: %d\n", activeBones);		
		for(int j=0; j < activeBones; j++) {
			OSBasics::read(&boneIndex, sizeof(unsigned int), 1, inFile);
			BoneTrack *newTrack = new BoneTrack(bones[boneIndex], length);
			
			BezierCurve *curve;
			float vec1[2]; //,vec2[2],vec3[2];

			OSBasics::read(&numCurves, sizeof(unsigned int), 1, inFile);
//			Logger::log("numCurves: %d\n", numCurves);					
			for(int l=0; l < numCurves; l++) {
				curve = new BezierCurve();
				OSBasics::read(&curveType, sizeof(unsigned int), 1, inFile);
				OSBasics::read(&numPoints, sizeof(unsigned int), 1, inFile);
				for(int k=0; k < numPoints; k++) {					
					OSBasics::read(vec1, sizeof(float), 2, inFile);					
					curve->addControlPoint2d(vec1[1], vec1[0]);
//					curve->addControlPoint(vec1[1]-10, vec1[0], 0, vec1[1], vec1[0], 0, vec1[1]+10, vec1[0], 0);
				}
				switch(curveType) {
					case 0:
						newTrack->scaleX = curve;
					break;
					case 1:
						newTrack->scaleY = curve;
					break;
					case 2:
						newTrack->scaleZ = curve;					
					break;
					case 3:
						newTrack->QuatW = curve;					
					break;
					case 4:
						newTrack->QuatX = curve;					
					break;
					case 5:
						newTrack->QuatY = curve;					
					break;
					case 6:
						newTrack->QuatZ = curve;					
					break;
					case 7:;
						newTrack->LocX = curve;					
					break;
					case 8:
						newTrack->LocY = curve;					
					break;
					case 9:
						newTrack->LocZ = curve;					
					break;
				}
			}
			
			newAnimation->addBoneTrack(newTrack);
		}
		animations.push_back(newAnimation);
	}
	*/
	OSBasics::close(inFile);
}
Esempio n. 25
0
string IESoRWorld::worldDrawList()
{
	//This will be written to json
	//fastwriter is not human readable --> compact
	//exactly what we want to sent to our html files
	Json::FastWriter* writer = new Json::FastWriter();

	//root is what we'll be sending back with all the shapes (stored in shapes!)
	Json::Value root;
	Json::Value shapes;

	//we'll loop through all required body information
	b2Body * B = this->world->GetBodyList();

	while(B != NULL)
	{
		if(B->GetUserData())
		{
			//fetch our body identifier
			PhysicsID* pid = static_cast<PhysicsID*>(B->GetUserData());//*((string*)B->GetUserData());
			std::string bodyID = pid->ID();
			
			//we must get all our shapes
			b2Fixture* F = B->GetFixtureList();

			//cycle through the shapes
			while(F != NULL)
			{
				//Hold our shape drawing information
				Json::Value singleShape;

				switch (F->GetType())
				{
				case b2Shape::e_circle:
					{
						b2CircleShape* circle = (b2CircleShape*) F->GetShape();
						/* Do stuff with a circle shape */
						Json::Value center = positionToJSONValue(circle->m_p);
						Json::Value radius = circle->m_radius;
						singleShape["type"] = "Circle";
						singleShape["bodyOffset"] = positionToJSONValue(B->GetPosition());
						singleShape["rotation"] = B->GetAngle();
						singleShape["center"] = center;
						singleShape["radius"] = circle->m_radius;
						singleShape["color"] = "#369";
					}
					break;
				case b2Shape::e_polygon:
					{
						b2PolygonShape* poly = (b2PolygonShape*) F->GetShape();
						/* Do stuff with a polygon shape */

						Json::Value points = listOfPoints(poly->m_vertices, poly->m_count);
						singleShape["type"] = "Polygon";
						singleShape["points"] = points;
						singleShape["bodyOffset"] = positionToJSONValue(B->GetPosition());
						singleShape["rotation"] = B->GetAngle();
						singleShape["color"] = "#38F";

					}
					break;
				}

				//Each shape is the unique combination of 
				pid = static_cast<PhysicsID*>(F->GetUserData());//*((string*)B->GetUserData());
				
				string shapeID = pid->ID();// *((string*)F->GetUserData());

				string fullID = bodyID + "_" + shapeID;
				shapes[fullID] = singleShape;

				F = F->GetNext();
			}
		}

		B = B->GetNext();
	}
	//we set our shapes using the body loops
	root["shapes"] = shapes;

	//we now need to process all of our joints as well
	b2Joint * J = this->world->GetJointList();

	Json::Value joints;

	while(J != NULL)
	{
		if(J->GetUserData())
		{
			//fetch our body identifier
			Bone* jid = static_cast<Bone*>(J->GetUserData());

			//we grab the joint identifier
			std::string bodyID = jid->ID();
		
			//Hold our joint drawing information
			Json::Value singleJoint;

			//we should use the body identifiers
			//but they both need to exist for this to be valid
			if(J->GetBodyA()->GetUserData() && J->GetBodyB()->GetUserData())
			{
				//we need to know what bodies are connected
				PhysicsID* pid = static_cast<PhysicsID*>(J->GetBodyA()->GetUserData());
				singleJoint["sourceID"] =  pid->ID();

				pid = static_cast<PhysicsID*>( J->GetBodyB()->GetUserData());
				singleJoint["targetID"] =  pid->ID();

				//now we need more drawing informtion regarding offsets on the body
				
				//get the anchor relative to body A
				//set that in our json object
				singleJoint["sourceOffset"] = positionToJSONValue(J->GetAnchorA());
				//set the same for our second object
				singleJoint["targetOffset"] = positionToJSONValue(J->GetAnchorB());
			}
			
			//set our joint object using the id, and json values
			joints[bodyID] = singleJoint;
		}

		//loop to the next object
		J = J->GetNext();
	}

	root["joints"] = joints;

	return writer->write(root);
}
Esempio n. 26
0
void IkConstraint::apply(Bone &parent, Bone &child, float targetX, float targetY, int bendDir, bool stretch, float alpha) {
	float px, py, psx, sx, psy;
	float cx, cy, csx, cwx, cwy;
	int o1, o2, s2, u;
	Bone *pp = parent.getParent();
	float tx, ty, dx, dy, dd, l1, l2, a1, a2, r;
	float id, x, y;
	if (alpha == 0) {
		child.updateWorldTransform();
		return;
	}
	if (!parent._appliedValid) parent.updateAppliedTransform();
	if (!child._appliedValid) child.updateAppliedTransform();
	px = parent._ax;
	py = parent._ay;
	psx = parent._ascaleX;
	sx = psx;
	psy = parent._ascaleY;
	csx = child._ascaleX;
	if (psx < 0) {
		psx = -psx;
		o1 = 180;
		s2 = -1;
	} else {
		o1 = 0;
		s2 = 1;
	}
	if (psy < 0) {
		psy = -psy;
		s2 = -s2;
	}
	if (csx < 0) {
		csx = -csx;
		o2 = 180;
	} else
		o2 = 0;
	r = psx - psy;
	cx = child._ax;
	u = (r < 0 ? -r : r) <= 0.0001f;
	if (!u) {
		cy = 0;
		cwx = parent._a * cx + parent._worldX;
		cwy = parent._c * cx + parent._worldY;
	} else {
		cy = child._ay;
		cwx = parent._a * cx + parent._b * cy + parent._worldX;
		cwy = parent._c * cx + parent._d * cy + parent._worldY;
	}
	id = 1 / (pp->_a * pp->_d - pp->_b * pp->_c);
	x = targetX - pp->_worldX;
	y = targetY - pp->_worldY;
	tx = (x * pp->_d - y * pp->_b) * id - px;
	ty = (y * pp->_a - x * pp->_c) * id - py;
	dd = tx * tx + ty * ty;
	x = cwx - pp->_worldX;
	y = cwy - pp->_worldY;
	dx = (x * pp->_d - y * pp->_b) * id - px;
	dy = (y * pp->_a - x * pp->_c) * id - py;
	l1 = MathUtil::sqrt(dx * dx + dy * dy);
	l2 = child.getData().getLength() * csx;
	if (u) {
		float cosine, a, b;
		l2 *= psx;
		cosine = (dd - l1 * l1 - l2 * l2) / (2 * l1 * l2);
		if (cosine < -1) cosine = -1;
		else if (cosine > 1) {
			cosine = 1;
			if (stretch && l1 + l2 > 0.0001f) sx *= (MathUtil::sqrt(dd) / (l1 + l2) - 1) * alpha + 1;
		}
		a2 = MathUtil::acos(cosine) * bendDir;
		a = l1 + l2 * cosine;
		b = l2 * MathUtil::sin(a2);
		a1 = MathUtil::atan2(ty * a - tx * b, tx * a + ty * b);
	} else {
		float a = psx * l2, b = psy * l2;
		float aa = a * a, bb = b * b, ll = l1 * l1, ta = MathUtil::atan2(ty, tx);
		float c0 = bb * ll + aa * dd - aa * bb, c1 = -2 * bb * l1, c2 = bb - aa;
		float d = c1 * c1 - 4 * c2 * c0;
		if (d >= 0) {
			float q = MathUtil::sqrt(d), r0, r1;
			if (c1 < 0) q = -q;
			q = -(c1 + q) / 2;
			r0 = q / c2;
			r1 = c0 / q;
			r = MathUtil::abs(r0) < MathUtil::abs(r1) ? r0 : r1;
			if (r * r <= dd) {
				y = MathUtil::sqrt(dd - r * r) * bendDir;
				a1 = ta - MathUtil::atan2(y, r);
				a2 = MathUtil::atan2(y / psy, (r - l1) / psx);
				goto break_outer;
			}
		}
		{
			float minAngle = MathUtil::Pi, minX = l1 - a, minDist = minX * minX, minY = 0;
			float maxAngle = 0, maxX = l1 + a, maxDist = maxX * maxX, maxY = 0;
			c0 = -a * l1 / (aa - bb);
			if (c0 >= -1 && c0 <= 1) {
				c0 = MathUtil::acos(c0);
				x = a * MathUtil::cos(c0) + l1;
				y = b * MathUtil::sin(c0);
				d = x * x + y * y;
				if (d < minDist) {
					minAngle = c0;
					minDist = d;
					minX = x;
					minY = y;
				}
				if (d > maxDist) {
					maxAngle = c0;
					maxDist = d;
					maxX = x;
					maxY = y;
				}
			}
			if (dd <= (minDist + maxDist) / 2) {
				a1 = ta - MathUtil::atan2(minY * bendDir, minX);
				a2 = minAngle * bendDir;
			} else {
				a1 = ta - MathUtil::atan2(maxY * bendDir, maxX);
				a2 = maxAngle * bendDir;
			}
		}
	}
	break_outer:
	{
		float os = MathUtil::atan2(cy, cx) * s2;
		a1 = (a1 - os) * MathUtil::Rad_Deg + o1 - parent._arotation;
		if (a1 > 180) a1 -= 360;
		else if (a1 < -180) a1 += 360;
		parent.updateWorldTransform(px, py, parent._rotation + a1 * alpha, sx, parent._ascaleY, 0, 0);
		a2 = ((a2 + os) * MathUtil::Rad_Deg - child._ashearX) * s2 + o2 - child._arotation;
		if (a2 > 180) a2 -= 360;
		else if (a2 < -180) a2 += 360;
		child.updateWorldTransform(cx, cy, child._arotation + a2 * alpha, child._ascaleX, child._ascaleY,
								   child._ashearX, child._ashearY);
	}
}
void ArmatureAnimation::play(const std::string& animationName, int durationTo,  int loop)
{
    if (animationName.empty() || _animationData == nullptr)
    {
        CCLOG("_animationData can not be null");
        return;
    }

    _movementData = _animationData->getMovement(animationName);
    if (nullptr == _movementData)
    {
        CCLOG("_movementData can not be null");
        return;
    }

    //! Get key frame count
    _rawDuration = _movementData->duration;

    _movementID = animationName;

    _processScale = _speedScale * _movementData->scale;

    //! Further processing parameters
    durationTo = (durationTo == -1) ? _movementData->durationTo : durationTo;

    int durationTween = _movementData->durationTween == 0 ? _rawDuration : _movementData->durationTween;

    cocos2d::tweenfunc::TweenType tweenEasing = _movementData->tweenEasing;
    loop = (loop < 0) ? _movementData->loop : loop;

    _onMovementList = false;

    ProcessBase::play(durationTo, durationTween, loop, tweenEasing);


    if (_rawDuration == 0)
    {
        _loopType = SINGLE_FRAME;
    }
    else
    {
        if (loop)
        {
            _loopType = ANIMATION_TO_LOOP_FRONT;
        }
        else
        {
            _loopType = ANIMATION_NO_LOOP;
        }
        _durationTween = durationTween;
    }

    MovementBoneData *movementBoneData = nullptr;
    _tweenList.clear();

    const Map<std::string, Bone*>& map = _armature->getBoneDic();
    for(auto& element : map)
    {
        Bone *bone = element.second;
        movementBoneData = static_cast<MovementBoneData *>(_movementData->movBoneDataDic.at(bone->getName()));

        Tween *tween = bone->getTween();
        if (tween == nullptr) {
            continue;
        }
        if(movementBoneData && !movementBoneData->frameList.empty())
        {
            _tweenList.push_back(tween);
            movementBoneData->duration = _movementData->duration;
            tween->play(movementBoneData, durationTo, durationTween, loop, tweenEasing);

            tween->setProcessScale(_processScale);

            if (bone->getChildArmature())
            {
                bone->getChildArmature()->getAnimation()->setSpeedScale(_processScale);
            }
        }
        else
        {
            if(!bone->isIgnoreMovementBoneData())
            {
                //! this bone is not include in this movement, so hide it
                bone->getDisplayManager()->changeDisplayWithIndex(-1, false);
                tween->stop();
            }

        }
    }

    _armature->update(0);
}
Esempio n. 28
0
bool operator==(Bone b1, Bone b2) //Returns true if the two given bones are the same
{
	return (b1.getValue1() == b2.getValue1() && b1.getValue2() == b2.getValue2());
}
bool Armature::init(const std::string& name)
{
    bool bRet = false;
    do
    {
        removeAllChildren();

        CC_SAFE_DELETE(_animation);
        _animation = new ArmatureAnimation();
        _animation->init(this);

        _boneDic.clear();
        _topBoneList.clear();

        _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;

        _name = name;

        ArmatureDataManager *armatureDataManager = ArmatureDataManager::getInstance();

        if(!_name.empty())
        {
            AnimationData *animationData = armatureDataManager->getAnimationData(name);
            CCASSERT(animationData, "AnimationData not exist! ");

            _animation->setAnimationData(animationData);


            ArmatureData *armatureData = armatureDataManager->getArmatureData(name);
            CCASSERT(armatureData, "");

            _armatureData = armatureData;

            for (auto& element : armatureData->boneDataDic)
            {
                Bone *bone = createBone(element.first.c_str());

                //! init bone's  Tween to 1st movement's 1st frame
                do
                {
                    MovementData *movData = animationData->getMovement(animationData->movementNames.at(0).c_str());
                    CC_BREAK_IF(!movData);

                    MovementBoneData *movBoneData = movData->getMovementBoneData(bone->getName().c_str());
                    CC_BREAK_IF(!movBoneData || movBoneData->frameList.size() <= 0);

                    FrameData *frameData = movBoneData->getFrameData(0);
                    CC_BREAK_IF(!frameData);

                    bone->getTweenData()->copy(frameData);
                    bone->changeDisplayWithIndex(frameData->displayIndex, false);
                }
                while (0);
            }

            update(0);
            updateOffsetPoint();
        }
        else
        {
            _name = "new_armature";
            _armatureData = ArmatureData::create();
            _armatureData->name = _name;

            AnimationData *animationData = AnimationData::create();
            animationData->name = _name;

            armatureDataManager->addArmatureData(_name.c_str(), _armatureData);
            armatureDataManager->addAnimationData(_name.c_str(), animationData);

            _animation->setAnimationData(animationData);

        }

        setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));

        setCascadeOpacityEnabled(true);
        setCascadeColorEnabled(true);

        bRet = true;
    }
    while (0);

    return bRet;
}
Esempio n. 30
0
void BaseFactory::buildSlots(Armature *armature, const ArmatureData *armatureData, const SkinData *skinData, const SkinData *skinDataCopy) const
{
    for (size_t i = 0, l = skinData->slotDataList.size(); i < l; ++i)
    {
        SlotData *slotData = skinData->slotDataList[i];
        Bone *bone = armature->getBone(slotData->parent);
        
        if (!bone)
        {
            continue;
        }
        
        Slot *slot = generateSlot(slotData);
        slot->name = slotData->name;
        slot->_originZOrder = slotData->zOrder;
        slot->_slotData = slotData;
        std::vector<std::pair<void*, DisplayType>> displayList;
        void *frameDisplay = nullptr;
        
        for (size_t j = 0, l = slotData->displayDataList.size(); j < l; ++j)
        {
            const DisplayData *displayData = slotData->displayDataList[j];
            
            switch (displayData->type)
            {
                case DisplayType::DT_ARMATURE:
                {
                    DisplayData *displayDataCopy = nullptr;
                    
                    if (skinDataCopy)
                    {
                        const SlotData *slotDataCopy = skinDataCopy->getSlotData(slotData->name);
                        
                        if (slotDataCopy)
                        {
                            displayDataCopy = slotDataCopy->displayDataList[i];
                        }
                    }
                    std::string currentDragonBonesDataName = _currentDragonBonesDataName;
                    std::string currentTextureAtlasName = _currentTextureAtlasName;
                    Armature *childArmature = buildArmature(displayData->name, "", displayDataCopy ? displayDataCopy->name : "", currentDragonBonesDataName, currentTextureAtlasName);
                    displayList.push_back(std::make_pair(childArmature, DisplayType::DT_ARMATURE));
                    _currentDragonBonesDataName = currentDragonBonesDataName;
                    _currentTextureAtlasName = currentTextureAtlasName;
                    break;
                }
                
                case DisplayType::DT_IMAGE:
                {
                    void *display = getTextureDisplay(displayData->name, _currentTextureAtlasName, displayData);
                    displayList.push_back(std::make_pair(display, DisplayType::DT_IMAGE));
                    break;
                }
                
                case DisplayType::DT_FRAME:
                {
                    //j
                    //frameDisplay = ;
                    break;
                }
                
                default:
                    break;
            }
        }
        
        bone->addChild(slot);
        
        if (!displayList.empty())
        {
            slot->setDisplayList(displayList, false);
        }
    }
}