Game Programming and Development Tools

collision physics??.... – dXter

dXter

Member

Posts: 59
From: Texas, the US of A
Registered: 09-26-2006
I've been trying to figure out how to simulate circle collision responses, but all I find when I look for it on the internet is a bunch of complicated physics equations, and I can't understand any of it. Is there any "simple" way to respond to two circles with mass colliding in many possible ways, for example billiard balls, without getting into calculus equations and a ton of other complicated math?? I already know how to detect circle collisions and how to move objects with acceleration and velocity, but the physics part about collisions is what's giving me trouble...

EDIT: I forgot to mention that I'm trying to do this in 2d and not 3d.

------------------
Jesus looked at them and said, "With man this is impossible, but with God all things are possible."
--Matt. 19:26

"Time is an excellent teacher, but eventually it kills all of its students."

[This message has been edited by dXter (edited November 20, 2006).]

Ereon

Member

Posts: 1018
From: Ohio, United States
Registered: 04-12-2005
Hrm, I would suggest possible taking a 2D approach to the operation, looking at the pool table, for instance, as a top down operation. That eliminates some of the complications and makes it possible, I think, to do with Trigonometry and basic common sense. I might be wrong on that, but that's the approach that I'd most likely take to at least start with.

------------------

How far that little candle throws its beams; So shines a good deed in a naughty world.

Portia The Merchant of Venice

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
I'll write up a demo for you tonight (currently at work). It'll be in C++, but should be easy enough understood.

Pretty much it's all just using Vector Math Been the running topic it seems for the past week for me. It's like introducing a new language to ppl on here lol.. some seem to accept it, others are happy in their old ways (Polar Coord guys :P haha)...

BTW, no offence to anyone, just speaking the truth, in a fun sort of way. I've had a pretty good day today (so far) so I'm in a good/happy mood.

Oh, BTW: Do you need rotation? If so it gets just a little bit more interesting. Not too much harder, just more code really.

------------------
www.auran.com

[This message has been edited by dartsman (edited November 20, 2006).]

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
heres a simple snippet which I just wrote up in Notepad. This handles the collision and the response (basic, just movement). If you had mass then you'd have to use a slightly different method.

Vector2 ballPosA, ballVelA, ballPosB, ballVelB;
float ballRadiusA = 2.0f, ballRadiusB = 2.0f;

// ballPosA, ballVelA, ballPosB, ballVelB all set somewhere

// in the update code
void update()
{
ballPosA += ballVelA;
ballPosB += ballVelB;

float dist = Vector2: istanceSq(ballPosA, ballPosB);

if (dist <= ((ballRadiusA * ballRadiusA) + (ballRadiusB * ballRadiusB)))
{
// they are colliding, so swap the velocities.
Vector2 temp = ballVelA;
ballVelA = ballVelB;
ballVelB = temp;
}

// etc..
}

I believe that will do it, only typed it up in Notepad (in my lunch break), so no hard time on syntax (if theres something wrong, let me know however). DistanceSq is the distance squared, in other words the distance, without the squareroot. It speeds it up a bit, and the sqrt is not required.

------------------
www.auran.com

TwoBrothersSoftware

Member

Posts: 141
From: Janesville, Wi USA`
Registered: 08-05-2006
I have a question does this formula allow a ball to tap another ball and tansfer only part of it's energy. I had trouble doing something like that at one point - never got it to look right.

Mike

dXter

Member

Posts: 59
From: Texas, the US of A
Registered: 09-26-2006
Wow, I didn't know it was so simple without mass... but I wanted to use mass, and I found a really good demo at the BlitzMax forums:

http://www.blitzbasic.com/Community/posts.php?topic=55823

(it's the one near the bottom of the page. I'm not putting the code here because it's too big.)

I don't understand a lot of it, but hey it works

------------------
Jesus looked at them and said, "With man this is impossible, but with God all things are possible."
--Matt. 19:26

"Time is an excellent teacher, but eventually it kills all of its students."

dXter

Member

Posts: 59
From: Texas, the US of A
Registered: 09-26-2006
Now that I got the "bouncing" part figured out (sort of), how do I do rotation? I don't really need it for what I want to do, but it might come in handy sometime. Can I use the same collision response method if I use rotation or do I have to do a completely different approach?

------------------
Jesus looked at them and said, "With man this is impossible, but with God all things are possible."
--Matt. 19:26

"Time is an excellent teacher, but eventually it kills all of its students."

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
Rotation will require an angular velocity and torque to be added to the objects. It can be thought of something similar to the position and velocity but for the rotation of the object, rather then location. Torque is the current object rotation (to which you render the rotated object), and Angular Velocity is the rotation which you add to the torque at each update (in the similar way which velocity is added to the position each update).

Also, you need to figure out the contact point. This is where the two spheres have intersected (collided). Hmmm... I do have my code at home which does 2D Box(OBB) to Circle Collision detection, and response (also a heck of a lot more, Box (AABB) to Circle, Circle to Line, Circle to Circle, etc.

I'll try and post some snippets whenever I can (real busy atm with the 2 jobs)...

------------------
www.auran.com

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
hey, finally had some free time to post those snippets...

Ok, heres just a sort of code dump from my simple 2D engine (I got the formulas from somewhere, could maybe dig them up again but that'd take some time, I believe it was from the 'Game Programing All in One' book...).

D3DXVECTOR2 m_position;
D3DXVECTOR2 m_velocity;

float m_torque;
float m_angularVelocity;

float m_friction;
float m_mass;

// update function (non time based movement..)
if (m_friction != 0.0f)
{
m_torque += (m_angularVelocity * (m_mass * m_friction));
m_angularVelocity *= (m_friction);

m_position += (m_velocity * (m_mass * m_friction));
m_velocity *= (m_friction);
}

then later on when you 'collide' with something you'd need to know where it collided and to what force.

// AddForce function
void AddForce(D3DXVECTOR2 pointOfContact, D3DXVECTOR2 force)
{
if (m_friction != 0.0f)
{
D3DXVECTOR2 arm = (pointOfContact - m_position);
m_angularVelocity += (CCollisionToolkit: erpDotProduct(arm, force) * 0.1f);

m_velocity += force;
}
}

I did have to do some tweaking with the actual force, and notice the "* 0.1f" part, it was due to the object rotating way too much.

The 'perpDotProduct' function just returns the dot product of the perpendicular vector.

(rougly like the following)

float CCollisionToolkit: erpDotProduct(D3DXVECTOR2 &a, D3DXVECTOR2 &b)
{
a = D3DXVECTOR2(-a.y, a.x);
return (float)D3DXVec2Dot(&a, &b);
}

Hope that helps.

------------------
www.auran.com

CapnStank

Member

Posts: 214
From: Sask, Canada
Registered: 12-16-2004
I can throw you as many physics equations you need which require no calculus. Just ask.... Basically what you'll want here is The Conservation of Momentum.

mv²=mv²

Or basically that momentum at the beginning must equal the momentum at the end. I'll just take the 2D case into example here. If a billiard ball is rolling straight at another its horizontal momentum equals a certain number, and its vertical momentum is actually 0. Assuming it's moving along your horizontal axis. So after the collision the vertical momentum of both balls MUST = 0. Likewise with the horizontal....

I doubt this was of any help. But if you really want to code it it should work for you.

------------------
"The only people on Earth who do not see Christ and His teachings as nonviolent are Christians". - Mahatma Gandhi