🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

No rotations in my 3d rigidbody system???

Started by
2 comments, last by isu diss 4 years, 3 months ago

In my 3d rigid-body simulation, when the ball hits an object it doesn't get rotated. I'm using AABB for collision detection

this is how i update the objects

void RigidBody::Update(float h)
{
	x += h*v;
	v += h*(F_Total/Mass);
	q += 0.5f*h*Omegaq;
	L += h*Tau_Total;
	q = XMQuaternionNormalize(q);
	R = XMMatrixRotationQuaternion(q);
	IInverse = XMMatrixMultiply(XMMatrixTranspose(R), IBodyInverse);
	IInverse = XMMatrixMultiply(IInverse, R);
	Omega = XMVector4Transform(L, IInverse);
	F_Total = XMVectorSet(0, 0, 0, 0);
	Tau_Total = XMVectorSet(0, 0, 0, 0);
	UpdateAABB();
}

This is how add impulsive torque and torque to the system

void RigidBody::AddTorque(XMVECTOR Torque)
{
	Tau_Total += Torque;
}

void RigidBody::AddImpulsiveTorque(XMVECTOR ImpulsiveTorque)
{
	Omega += XMVector4Transform(ImpulsiveTorque, IInverse);
}

This is how I apply impulses.

void Arbiter::ApplyImpulse()
{
	XMVECTOR tangent[2];

	for (int i = 0; i < numContacts; ++i)
	{
		Contact* c = contacts + i;

	 XMVECTOR p = c->position;

	XMVECTOR padot = body1->GetVelocityAtPoint(p);
	XMVECTOR pbdot = body2->GetVelocityAtPoint(p);

	XMVECTOR ra = (p - body1->GetPosition());
	XMVECTOR rb = (p - body2->GetPosition());

	XMVECTOR n = c->normal;
	
	XMVECTOR dv = -padot + pbdot;
	float Cdot = XMVector3Dot(dv, n).m128_f32[0];
	float dPn = c->massNormal * (-Cdot + c->bias);

	float Pn0 = c->Pn;
	c->Pn = max(Pn0 + dPn, 0.0f);
	dPn = c->Pn - Pn0;

	XMVECTOR P = dPn * n;
	
	body1->AddImpulse(-P);
	body1->AddTorque(-XMVector3Cross(ra, P));
	body2->AddImpulse(P);
	body2->AddTorque(XMVector3Cross(rb, P));
	
	p = c->position;

	padot = body1->GetVelocityAtPoint(p);
	pbdot = body2->GetVelocityAtPoint(p);

	n = c->normal;
	ra = (p - body1->GetPosition());
	rb = (p - body2->GetPosition());

	dv = -padot +pbdot;

	tangent[0] = XMVector3Orthogonal(n);
	tangent[1] = XMVector3Cross(tangent[0], n);

	for (int z=0;z<2;z++)
	{
		float vt = XMVector3Dot(dv, tangent[z]).m128_f32[0];
		float dPt = c->massTangent[z] * (-vt);

		// Compute friction impulse
		float maxPt = friction * c->Pn;

		// Clamp friction
		float oldTangentImpulse = c->Pt[z];
		c->Pt[z] = Clamp(oldTangentImpulse + dPt, -maxPt, maxPt);
		dPt = c->Pt[z] - oldTangentImpulse;

			// Apply contact impulse
		XMVECTOR Pt = dPt * tangent[z];

		body1->AddImpulse(-Pt);
		body1->AddTorque(-XMVector3Cross(ra, Pt));
		body2->AddImpulse(Pt);
		body2->AddTorque(XMVector3Cross(rb, Pt));
	}
	}
}
Advertisement

Despite the dact you use some physic framework, i dont see that you apply torque at specific point?

vec3 cog2cob_vec = vectorAB(pos + ROTATION_MAT * CENTEROFGRAVITY, FORCECONTACTPOINT);

vec3 FORCE_force = //SOME FORCE VECTOR ACTING AT THAT POINT vec3(0.0, 1.0, 0.0) * (GForce * SUBMERGED_VOLUME * 999.1026);

vec3 ETorque = cog2cob_vec * FORCE_force; //vector cross

vec3 elements_torque = ETorque;

mat4 inv_rot = ROTATION_MAT;

inv_rot.Inverse();

vec3 local_torque = inv_rot * elements_torque;

vec3 EAngAcc = vec3(0.0, 0.0, 0.0);

//If its too small value you dont apply the impules cause you get jittering and sometimes nan values which happen to spawn object some infinite distance from last pos

Ixx etc you calculate by yourself

if (!betweenorequal(-epsilona,epsilona, Ixx)) EAngAcc.x = local_torque.x / Ixx;

if (!betweenorequal(-epsilona,epsilona, Iyy)) EAngAcc.y = local_torque.y / Iyy;

if (!betweenorequal(-epsilona,epsilona, Izz)) EAngAcc.z = local_torque.z / Izz;

EAngVel = EAngAcc*dt;

AngVel = AngVel + EAngVel;

vec3 vLocalAngularMoment = (AngVel*dt)*RAD_TO_DEG;

You add vLocalAngularMoment to some rotation rotation velocity vector so it stays rotating after a time (remember to apply resistance force or a damping function)

I found rotations by modifying the addimpulsivetorque function. instead of modifying omega I modfied L(angular momentum). I used addimpulsivetorque function instead of addtorque

This topic is closed to new replies.

Advertisement