Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Help me to extend my physics engine

Avatar of Asif_Saadat
Asif_Saadat asked on
Game Programming
21 Comments1 Solution1172 ViewsLast Modified:
i am very bad at maths and physics stuff , although i had managed to develop a 3d engine on my own in opengl now i am trying to add some physics capabilities into my engine for this i have written a small rigid body class with some basic functionality because i want to understand before i implement ,i have read many books , physics for game programmers/developer etc and more, but due to my bad math background i cannot pick what they say and the sourcecode they provide its really big and tightly coupled with their own engine and it is hard for me to understand and pick code from there and add into my engine, after you see my rigidbody class i have impletemented integrator and how to applyforces, it simulate correctly , tell me how to add some collision detection and collission response thing with my own engine not some external examples i have seen all those and dont get them ,i know collision is hard but for simplicity all i want is for sphere and cube and a plane but if anybody wanna help me more they are welcome and thanks in advanced
typedef struct RB
{
	float Position[3],LinearVelocity[3],AngularVelocity[3],Force[3],Torque[3],Mass,Orientation[4];
	float MT[16];
    void Init(void)
	{
		VECTOR3_SetZero(Position);
		VECTOR3_SetZero(LinearVelocity);
		VECTOR3_SetZero(AngularVelocity);
		VECTOR3_SetZero(Force);
		VECTOR3_SetZero(Torque);
		Quaternion_Identity(Orientation);
		Mass=1.0f;
	}
	void Integrate(float dt) 
	{
		
		VECTOR3_Set(LinearVelocity,LinearVelocity[0]+Force[0]/Mass*dt,LinearVelocity[1]+Force[1]/Mass*dt,LinearVelocity[2]+Force[2]/Mass*dt);
		VECTOR3_SetZero(Force);
		VECTOR3_Set(AngularVelocity,AngularVelocity[0]+Torque[0]/Mass*dt,AngularVelocity[1]+Torque[1]/Mass*dt,AngularVelocity[2]+Torque[2]/Mass*dt);
		VECTOR3_SetZero(Torque);
		VECTOR3_Set(Position,Position[0]+LinearVelocity[0]*dt,Position[1]+LinearVelocity[1]*dt,Position[2]+LinearVelocity[2]*dt);

		float Q[4];
		Quaternion_Set(Q,AngularVelocity[0]*dt,AngularVelocity[1]*dt,AngularVelocity[2]*dt,0.0f);
		Quaternion::Concate(Q,Orientation);		
		Quaternion_Set(Orientation,Orientation[0]+Q[0],Orientation[1]+Q[1],Orientation[2]+Q[2],Orientation[3]+Q[3]);
		Quaternion::Normalize(Orientation);

		Quaternion::ToMatrix(Orientation,MT);	  
    }

	
	void ApplyForce(float *forcePosition, float *directionMagnitude) 
	{
    
	  float lengthSquared =VECTOR3_SQLength(directionMagnitude);
	  if (lengthSquared < 1e-8f) return; 

	  VECTOR3_Add(Force,directionMagnitude);	  
	  float distance[3];
	  VECTOR3_Subtract(distance,forcePosition,Position);
	  VECTOR3_CrossProduct(forcePosition,directionMagnitude,distance);
	  VECTOR3_Add(Torque,forcePosition);
	
		
    }

	void ApplyForce(float f_x,float f_y,float f_z, float d_x, float d_y, float d_z) 
	{
		float fv[3],dv[3];
		VECTOR3_Set(fv,f_x,f_y,f_z);
		VECTOR3_Set(dv,d_x,d_y,d_z);
		ApplyForce(fv,dv);

		
	}
	void Draw(void)
	{
		glPushMatrix();
		glTranslatef(Position[0],Position[1],Position[2]);
		glMultMatrixf(MT);
		glutWireSphere(1,12,12);
		glPopMatrix();
	}


}RB;
ASKER CERTIFIED SOLUTION
Avatar of MatthewOsosky
MatthewOsoskyFlag of United States of America image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 21 Comments.
See Answers