Beispiel #1
0
// tanAcceleration returns the tangential acceleration of the frame in
// world space.
Vector Physics::tanAcceleration() const{
	return cross(angularAcceleration(),position())-tanAccelerationReference();
}
Beispiel #2
0
void Item::UpdatePhysics(float dt)
{
	vec3 acceleration = (m_gravityDirection * 9.81f) * 4.0f;

	// Integrate velocity
	m_velocity += acceleration * dt;

	// Integrate angular velocity and rotation
	vec3 angularAcceleration(0.0f, 0.0f, 0.0f);
	m_angularVelocity += angularAcceleration * dt;
	m_rotation += m_angularVelocity * dt;

	if (m_worldCollide)
	{
		int blockX, blockY, blockZ;
		vec3 blockPos;

		// Check collision
		{
			vec3 velocityToUse = m_velocity;
			vec3 velAmount = velocityToUse*dt;
			vec3 pNormal;
			int numberDivision = 1;
			while (length(velAmount) >= 1.0f)
			{
				numberDivision++;
				velAmount = velocityToUse*(dt / numberDivision);
			}
			for (int i = 0; i < numberDivision; i++)
			{
				float dtToUse = (dt / numberDivision) + ((dt / numberDivision) * i);
				vec3 posToCheck = GetCenter() + velocityToUse*dtToUse;
				bool stepUp = false;
				if (CheckCollisions(posToCheck, m_previousPosition, &pNormal, &velAmount))
				{
					// Reset velocity, we don't have any bounce
					m_velocity = vec3(0.0f, 0.0f, 0.0f);
					velocityToUse = vec3(0.0f, 0.0f, 0.0f);
				}
			}

			// Integrate position
			m_position += velocityToUse * dt;
		}

		// Owning chunks
		if (m_pOwningChunk != NULL && m_pOwningChunk->IsSetup() && m_pOwningChunk->IsInsideChunk(m_position))
		{
			Chunk* pChunk = GetCachedGridChunkOrFromPosition(m_position);
			bool active = m_pChunkManager->GetBlockActiveFrom3DPosition(m_position.x, m_position.y, m_position.z, &blockPos, &blockX, &blockY, &blockZ, &pChunk);

			if (active == true)
			{
				// Roll back the integration, since we will intersect the block otherwise
				m_position -= m_velocity * dt;

				m_velocity = vec3(0.0f, 0.0f, 0.0f);
			}
		}
		else
		{
			if (m_pOwningChunk != NULL)
			{
				m_pOwningChunk->RemoveItem(this);
			}

			m_pOwningChunk = m_pChunkManager->GetChunkFromPosition(m_position.x, m_position.y, m_position.z);

			if (m_pOwningChunk != NULL)
			{
				m_pOwningChunk->AddItem(this);
			}

			if (m_pOwningChunk == NULL)
			{
				m_position -= m_velocity * dt;
				m_velocity = vec3(0.0f, 0.0f, 0.0f);
			}
		}
	}
	else
	{
		// Integrate position
		m_position += m_velocity * dt;
	}

	m_previousPosition = GetCenter();
}