コード例 #1
0
void CTripmineGrenade::AttachToEntity(CBaseEntity *pOther)
{
	if (!pOther)
		return;

	if ( !VPhysicsGetObject() )
		return;

	m_bAttached			= true;
	m_hAttachEntity		= pOther;

	SetMoveType			( MOVETYPE_NONE );

	if (pOther->GetSolid() == SOLID_VPHYSICS && pOther->VPhysicsGetObject() != NULL )
	{
		SetSolid(SOLID_BBOX); //Tony; switch to bbox solid instead of vphysics, because we've made the physics object non-solid
		MakeConstraint(pOther);
		SetMoveType		( MOVETYPE_VPHYSICS ); // use vphysics while constrained!!
	}
	//if it isnt vphysics or bsp, use SetParent to follow it.
	else if (pOther->GetSolid() != SOLID_BSP)
	{
		SetSolid(SOLID_BBOX); //Tony; switch to bbox solid instead of vphysics, because we've made the physics object non-solid
		SetParent( m_hAttachEntity.Get() );
	}
}
コード例 #2
0
bool Physics_Cloth::ResetCloth()
{
	// Clear Memory
	ReleaseCloth();
	ReleaseSelected();
	m_contraints.clear();
	m_nextIndex = 0;

	if (m_initialisedParticles == false)
	{
		ReleasePtr(m_pMesh);
		ReleasePtrArray(m_pParticles);

		// Create memory for all the particles
		m_particleCount = m_particlesWidthCount * m_particlesHeightCount;
		m_pParticles = new Physics_Particle[m_particleCount];
		m_pVertices = new TVertexColor[m_particleCount];		

		// Calculate how many indices there are with be based on how many particles there are using line list
		int immediateConstraintCount = (m_particlesWidthCount - 1) * (m_particlesHeightCount - 1) * 4 + (m_particlesWidthCount - 1) + (m_particlesHeightCount - 1);
		int secondaryConstraintCount = 0;
		if (m_complexWeave == true)
		{
			// Calculate the secondary indices count only if the weave is set to complex
			secondaryConstraintCount = (m_particlesWidthCount - 2) * (m_particlesHeightCount - 2) * 4 + ((m_particlesWidthCount - 2) * 2) + ((m_particlesHeightCount - 2) * 2);
		}
		
		// Create the indices buffer with the amount of calculated constraints
		m_indexCount = (immediateConstraintCount + secondaryConstraintCount) * 2;
		m_pIndices = new DWORD[m_indexCount];
	}
	
	// Cycle through all the particles
	for (int col = 0; col < m_particlesWidthCount; col++)
	{
		for (int row = 0; row < m_particlesHeightCount; row++)
		{
			// Calculate the position based on the particles row and column
			v3float pos;
			pos.x = m_width * (col / (float)m_width) - ((float)m_width / 2.0f);
			pos.y = -m_height * (row / (float)m_height) + ((float)m_height / 2.0f);
			pos.z = 0.0f;
			int index = row * m_particlesWidthCount + col;

			if (m_initialisedParticles == false)
			{
				// First time. Initialise			
				m_pVertices[index] = { { pos.x, pos.y, pos.z }, d3dxColors::White };
				VALIDATE(m_pParticles[index].Initialise(index, &m_pVertices[index], pos, m_timeStep, m_damping));
			}
			else
			{
				// Particle has already been initialized so just reset the position
				m_pParticles[index].Reset();
				m_pParticles[index].SetPosition(pos, true);
				m_pVertices[index].color = d3dxColors::White;
			}
		}
	}

	// Connect Particles that are immediately to the right and below (include diagonals)
	for (int col = 0; col < m_particlesWidthCount; col++)
	{
		for (int row = 0; row < m_particlesHeightCount; row++)
		{
			// Particle to the Right exists
			if (col < m_particlesWidthCount - 1)
			{
				VALIDATE(MakeConstraint(GetParticleIndex(col, row), GetParticleIndex(col + 1, row), true));
				
				// Add the constraint index to each attached particle
				GetParticle(col, row)->AddContraintIndex(m_contraints.size() - 1);
				GetParticle(col + 1, row)->AddContraintIndex(m_contraints.size() - 1);
			}
	
			// Particle below exists
			if (row < m_particlesHeightCount - 1)
			{
				VALIDATE(MakeConstraint(GetParticleIndex(col, row), GetParticleIndex(col, row + 1), true));

				// Add the constraint index to each attached particle
				GetParticle(col, row)->AddContraintIndex(m_contraints.size() - 1);
				GetParticle(col, row + 1)->AddContraintIndex(m_contraints.size() - 1);
			}
	
			// Particle to the right and below exists
			if ((col < m_particlesWidthCount - 1) && (row < m_particlesHeightCount - 1))
			{
				VALIDATE(MakeConstraint(GetParticleIndex(col, row), GetParticleIndex(col + 1, row + 1), true));

				// Add the constraint index to each attached particle
				GetParticle(col, row)->AddContraintIndex(m_contraints.size() - 1);
				GetParticle(col + 1, row + 1)->AddContraintIndex(m_contraints.size() - 1);

				VALIDATE(MakeConstraint(GetParticleIndex(col + 1, row), GetParticleIndex(col, row + 1), true));

				// Add the constraint index to each attached particle
				GetParticle(col + 1, row)->AddContraintIndex(m_contraints.size() - 1);
				GetParticle(col, row + 1)->AddContraintIndex(m_contraints.size() - 1);
			}
		}
	}

	if (m_complexWeave == true)
	{
		// Connect Particles the are one step further away than previous loop
		for (int col = 0; col < m_particlesWidthCount; col++)
		{
			for (int row = 0; row < m_particlesHeightCount; row++)
			{
				// Particle to the Right exists
				if (col < m_particlesWidthCount - 2)
				{
					VALIDATE(MakeConstraint(GetParticleIndex(col, row), GetParticleIndex(col + 2, row), false));

					// Add the constraint index to each attached particle
					GetParticle(col, row)->AddContraintIndex(m_contraints.size() - 1);
					GetParticle(col + 2, row)->AddContraintIndex(m_contraints.size() - 1);
				}

				// Particle below exists
				if (row < m_particlesHeightCount - 2)
				{
					VALIDATE(MakeConstraint(GetParticleIndex(col, row), GetParticleIndex(col, row + 2), false));

					// Add the constraint index to each attached particle
					GetParticle(col, row)->AddContraintIndex(m_contraints.size() - 1);
					GetParticle(col, row + 2)->AddContraintIndex(m_contraints.size() - 1);
				}

				// Particle to the right and below exists
				if ((col < m_particlesWidthCount - 2) && (row < m_particlesHeightCount - 2))
				{
					VALIDATE(MakeConstraint(GetParticleIndex(col, row), GetParticleIndex(col + 2, row + 2), false));

					// Add the constraint index to each attached particle
					GetParticle(col, row)->AddContraintIndex(m_contraints.size() - 1);
					GetParticle(col + 2, row + 2)->AddContraintIndex(m_contraints.size() - 1);

					VALIDATE(MakeConstraint(GetParticleIndex(col + 2, row), GetParticleIndex(col, row + 2), false));

					// Add the constraint index to each attached particle
					GetParticle(col + 2, row)->AddContraintIndex(m_contraints.size() - 1);
					GetParticle(col, row + 2)->AddContraintIndex(m_contraints.size() - 1);
				}
			}
		}
	}
	
	if (m_initialisedParticles == false)
	{
		// Create a new Cloth Mesh
		m_pMesh = new DX10_Mesh();
		VALIDATE(m_pMesh->InitialiseCloth(m_pRenderer, m_pVertices, m_pIndices, m_particleCount, m_indexCount, sizeof(TVertexColor), D3D10_PRIMITIVE_TOPOLOGY_LINELIST, D3D10_USAGE_DYNAMIC, D3D10_USAGE_DYNAMIC));
	}

	// Create the hooks and pin the cloth
	CreateHooks();

	// Add a wind force of 1 down the Z axis to settle the cloth
	AddForce({ 0.0f, 0.0f, 1.0f }, FT_GENERIC, false);
	Process(CT_NONE);

	m_initialisedParticles = true;
	return true;
}