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;
```

Do more with

EXPERT OFFICE^{®} is a registered trademark of EXPERTS EXCHANGE^{®}

but spheres collide when the distance between their centres is less than the sum of their radii,

a sphere collides with a plane when the distance between the centre and the plane is less than its radius.

If cubes are oriented the same way, then in a coordinate system aligned with the cubes,

they collide if the difference between each coordinate of their centres is less than half the sum of the sizes of the cubes.

If the cubes can have different orientations than it gets a little more tricky to explain, and I'd prefer to see how cubes are represented in your code before trying to describe it

You need to decide, fundamentally, what sort of volumes will collide.

For each type of fundamental object, you'll need to support a collision between it and all the other fundamental objects.

For instance, if you support SPHERE and PLANE as your fundamental types, then you need:

DetectCollisionBetweenSphe

DetectCollisionBetweenSphe

DetectCollisionBetweenPlan

DetectCollisionBetweenPlan

The more of these fundamental types you support, the more you'll need to implement...

These routines don't determine the collision response, usually... they are simply culling routines, used to identify which objects in the scene need to have collisions applied.

Once you have a list of objects that need collision response, then you need to "apply" the response.

Over time, I've found it's easier to understand this math if you simplify it to two dimensions, and then collide objects using simple 2D Vector math.

Each object has a current location, and a "desired location". You'll need to maintain these as separate positions.

These locations are then the endpoints of a line segment. This line segment has some magnitude, and a heading. If we have an orientation and a length, you can treat it as a vector, current->desired.

The "result" of the collision (CollisionResponse) is then the product of the two vectors, just like a high school physics text would teach. The order of the objects that you're updating matters, so be sure to not set both objects to the same result (if both are moving). You'll need to apply each separately.

You can then update the object's current location to the result you obtained; do this for every object in your list, and then re-render the scene.

This is the absolute simplest version of collision detection and response, but it might help to get you started.

You can get much fancier; you can do the math with 3D vectors, you can model objects with different masses and velocities, you can add the effects of friction or whatever you can think of.

But all of those fancy things have their roots in this sort of implementation.

Hope this helps,

-john

The key to this exercise is to start simple, and then build upon and extend the model with more and more features, so that you can learn how all the parts come together.

So, keep your 3d renderer the way it is...

But keep in mind rendering is not the same as collision detection and response; the geometries used are often quite different.

I would advocate the following:

1) get it working, simply, in 2D.. allow a circle to collide with a circle... and then implement the response.

2) Once that works, add support for a circle to collide with a 2D line segment

3) Make certain that all the math is using vectors, even if it's only 2D math

This math is much simpler, and generally can be well understood and debugged without added complexity. Keep in mind, collision detection is different from collision response, and to do collision detection well, you need to integrate over the object's movement.. this is generally not the way this sort of math is taught, so make sure you keep the math as simple as possible in your initial attempts until you're certain that the collision detection is working.

Collision response in 2d is not significantly different in 3D, so it's not a waste of time to do it this way.

After you get the basics working, you can add things like mass, gravity, friction, acceleration.. Each of these is much easier to add piece by piece rather than a BIG BANG sort of implementation.

Once you have those basic operations (circle to circle, circle to line) working, then you can extend the model to 3 dimensions, and it's much easier.

I found a nice blog with example code and some very well written articles in PDFs here. They really are quite good.

http://chrishecker.com/Rigid_Body_Dynamics#Physics_Articles

Hope this helps,

-john

I'm happy to guide, to answer questions, and to point you at resources.

But it seems like you want me to write it. I'd prefer you learned the math.

-john

Detection is very complex but easy compared to response. I seriously had to read, reread several chapters on the subject from several different sources before I began to appreciate to complexity of it. Not only is responding to to initial contact complex, but for really good engines there are additional layers of algorithms required to account to penetration resolution.

My advice to you would be this, if you have a limited understanding of math and not willing to invest the time to learn in, abandon trying to write your own physics engine, it is harder than you think. You can utilized off the shelf physics engines and be much more productive.

If you are committed to doing it, I'll share a little bit of my magic code. The code implements and algebraic equation that you will find associated with collision response physics. If you learn what the code is doing, you may have a shot, if not, again, i would lean on somebody else implementation of a physics engine. It's what most commercial games do anyway.

```
#include "PhysicsFunctions.h"
using namespace GameEngine2;
DWORD PhysicsFunctions::PHYSICS_RESOLVE_COLLISION_ALPHA(KineticBody* k1, KineticBody* k2, FLOAT closing_speed, D3DXVECTOR3 collision_pt, D3DXVECTOR3 collision_normal)
{
const float e = k1->g_CoefficientOfResitution;
collision_normal = collision_normal;
D3DXVECTOR3 R1 = collision_pt - k1->g_Position;
D3DXVECTOR3 R2 = collision_pt - k2->g_Position;
D3DXVECTOR3 cross1;
D3DXVECTOR3 cross2;
D3DXVec3Cross(&cross1,&R1,&collision_normal);
D3DXVec3Cross(&cross2,&R2,&collision_normal);
D3DXVec3TransformCoord(&cross1,&cross1,&k1->g_InertiaTensorInverse);
D3DXVec3TransformCoord(&cross1,&cross1,&k2->g_InertiaTensorInverse);
D3DXVECTOR3 crossA;
D3DXVECTOR3 crossB;
D3DXVec3Cross(&crossA,&cross1,&R1);
D3DXVec3Cross(&crossB,&cross1,&R2);
D3DXVECTOR3 sum = crossA + crossB; //cross sum...
FLOAT sum_dot_normal = D3DXVec3Dot(&sum,&collision_normal);
FLOAT one_over_mass = (1.0f/k1->g_Mass + 1.0f/k2->g_Mass);
//this depends only on e1 value, might change later
float numerator = -(e + 1.0f) * closing_speed;
float denominator = one_over_mass + sum_dot_normal;
float j = numerator/denominator;
D3DXVECTOR3 j_vector = collision_normal*j;
D3DXVECTOR3 angular_add_1;
D3DXVECTOR3 angular_add_2;
D3DXVec3Cross(&angular_add_1,&R1,&j_vector);
D3DXVec3TransformCoord(&angular_add_1,&angular_add_1,&k1->g_InertiaTensorInverse);
D3DXVec3Cross(&angular_add_2,&R2,&j_vector);
D3DXVec3TransformCoord(&angular_add_2,&angular_add_2,&k2->g_InertiaTensorInverse);
D3DXVECTOR3 linear_add_1 = j_vector / k1->g_Mass;
D3DXVECTOR3 linear_add_2 = j_vector / k2->g_Mass;
k1->g_LinearVelocity += linear_add_1;
k1->g_AngularVelocity += angular_add_1;
k2->g_LinearVelocity -= linear_add_2;
k2->g_AngularVelocity += angular_add_2;
return 0;
}
```

You said yourself that you've got tons of examples...

When I'm faced with a similar set of circumstances, I generally decompose the hard problem to a set of simpler problems, and I keep doing it until I get to a point where I understand what's happening.

I've tried to guide you on how to break it down into simpiler problems, that you can then extend and make more robust for your needs, but you actively resist it.

I simply can't do what you're asking for.. I can't put knowledge into your head directly. You need to work for it, there is no other way.

I'm happy to answer any questions you have along the way, and to guide you.

But you have an existing concept and framework in mind, then you'll need to be the one putting it in.

Hope this helps,

-john

http://en.wikipedia.org/wiki/List_of_moment_of_inertia_tensors

The inertia tensors for more complex objects are harder to calculate and are usually broken down to simpler shapes. (i.e. a space ship might be simplefied as a block).

The CoefficientOfResitution variable is basically described as how much energy is lost in the collision. Think, friction or material deformation..

The kinectic body above hold various variables, th most important for the calcualtion in question are the objects position, liner and rotational velocities (as Vector3), and the objects inverse (easier calcuation) inertia tensor. You should easily be able to provide these for your spheres. Plug the numbers in and it should change to satisfactory for the sphere's.

Again, I got to say it doesnt not sound like your ready to write your own engine. It's probably one of the hardest disciplines in progamming. You're going to need to read alot more materrial than what you'll be provided on expert expert change to get any undertanding of the subject. I would look to the Morgan Kaufmann series on physics engine developement. It's probably one of the better ones and there and serveral texts that you will need to cover.

This is not something that you can put together in a day, or two, or ten. It will take several weeks to get a good feel for the code.

## Premium Content

You need an Expert Office subscription to comment.Start Free Trial