METAL: Circle collisions

Member
Posts: 509
Joined: 2002.05
Post: #1
I am programming a pool game in METAL basic and I have figured out this code to detect if 2 circles collide

if r1+r2 > sqr((x2-x1)^2+(y2-y1)^2) then
'Collision detected
end if

Now that I know they hit each other, how do I calculate the new velocitys of the circles? Can you tell me the equation if their old velocitys were vx1,vy1,vx2,vy2.

Thanks. Also please don't make it in C code, just use BASIC code or psuedocode.
Quote this message in a reply
Member
Posts: 156
Joined: 2002.11
Post: #2
there is a good article on Gamasutra about collisions for a pool game. It's really worth a read.

Time is running out!
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #3
you can make this run a little faster if you want. Square root operations are very slow. So you could use:

if r1^2+r2^2 > (x2-x1)^2+(y2-y1)^2 then

that way you avoid the sqr() and you can precompute r1^2 and r2^2 if you want.

A quick way to handle the collision is to swap velocities. After the collision r1 is moving in the direction that r2 was and visa versa. This isnt physically correct of course - but its very easy to do, and usually looks ok.

But - if you want to be accurate...

Finding out that two spheres currently intersect isnt enough - you need the find the time and point of contact. If the previous frame they didnt overlap but this frame they do overlap - that means at some time in between frames they first came in contact - and thats the time you want to look at.

If you wish to be more accurate you need to learn some vector math - especially what the dot product is and what it is used for. Then read the article that FCCovett talked about here (free registration required):

<http://www.gamasutra.com/features/200201...el_pfv.htm>

It explains everything in detail, has well documented Java code examples (sorry no METAL, but its pretty easy to follow). I use these algorithms for colliding spheres and circles and it works very well. But learn about vectors and dot product first - or you will be lost.

good luck, hth,
Codemattic
Quote this message in a reply
Member
Posts: 509
Joined: 2002.05
Post: #4
Im lost, I am asking alot here but can somebody write the code, eather in METAL or BASIC or pseudocode or whatever. I am only 15 so I don't know what a dot product is or stuff like that
Quote this message in a reply
Member
Posts: 164
Joined: 2002.04
Post: #5
You will continue to not know what they are until you put some effort into learning it Smile Don't use age as an excuse.
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #6
Quote:r1^2+r2^2 > (x2-x1)^2+(y2-y1)^2
is wrong.

(r1 + r2)^2 = r1^2 + 2*r1*r2 + r2^2
this is basic algebra man.
shame on you.

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Member
Posts: 142
Joined: 2002.11
Post: #7
Yes, he is wrong.

(r1+r2)^2 = r1^2+r1*r2+r2^2 and not r1^2+r2^2, but its not exactly a reason for shame.

The basic idea is right. Square roots take a long time to compute and are pointless if you're just checking relationships like >, <, or = since you don't need to know the ACTUAL distances anyway, just how they relate to one another.

And swapping velocities is in perfect accordance with physics assuming that

1. The objects are of equal mass
2. Its an elastic collision (no crunching or energy lost due to heat or whatever)

A good way to configure the velocities if the objects are not of equal mass then remember conservation of momentum. The momentum of the system (the sum of each objects mass * velocity) will be the same both before and after the collision (again, assuming an ellastic collision). You can derrive a system from there.
Quote this message in a reply
Member
Posts: 509
Joined: 2002.05
Post: #8
This is way over my head, I will learn it eventualy, but I need some time. Can somebody please help me here.
Quote this message in a reply
Member
Posts: 164
Joined: 2002.04
Post: #9
Didn't someone post an open-source physics engine thingy somewhere that included this?
Quote this message in a reply
Member
Posts: 509
Joined: 2002.05
Post: #10
They posted the executable, not source.
Quote this message in a reply
Member
Posts: 509
Joined: 2002.05
Post: #11
I just re-read the tutorial and I am still confused. Not understanding Java code doesn't help much eather.

I may just have to figure this out on my own Sad
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #12
Quote:Originally posted by kelvin
this is basic algebra man.
shame on you.


I am so friggin *busted* :oops: - thanks for spotting my error.

-Codemattic
Quote this message in a reply
Member
Posts: 156
Joined: 2002.11
Post: #13
Jake, it really doesn't get any easier than that article, and I don't have the code in BASIC.

Basically, this is a problem that I just got to study in the second year of the engineering school. "Afraid you will be; very afraid." Smile

Time is running out!
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #14
Quote:Originally posted by Jake
I am only 15 so I don't know what a dot product is or stuff like that

Im not sure what one has to do with the other. It is all within your abilities - its mostly arithmetic. Everything looks strange and unlearnable when you first look at it. Besides I would imagine being 15 is a benefit to learning new ideas.

Start with the code you have. When balls collide have them react by switching the velocities. Build the rest of the game using that. There still are lots of things for you to figure out. Dont give up just because one article looks intimidating. When you get the rest working - maybe you will go back and write better collision code - or maybe it will be fun as is.

There are sims that try and be as accurate as possible. Taking english (spin) into account is going to be very tricky also. But then there are arcade type games - that arent such in depth simulation. (like flying games - some model the physics of flying and are 'faithful' - while some are arcade-like and have very simple physics, but you can get flying and blowing things up quickly!) So make what you can.

Maybe you can play on different shaped tables instead of just the standard one? What would a 3d pool 'table' play like - that is if instead of playing on a surface you played in a volume (a cube or a box) - like thats how you can play pool in zero-g.

cheers,
Codemattic
Quote this message in a reply
Member
Posts: 509
Joined: 2002.05
Post: #15
Quote:Originally posted by codemattic

Maybe you can play on different shaped tables instead of just the standard one? What would a 3d pool 'table' play like - that is if instead of playing on a surface you played in a volume (a cube or a box) - like thats how you can play pool in zero-g.

cheers,
Codemattic


I was thinking about that 3D pool, but was waiting for METAL 2.0 to come out with open GL.

I will just switch velocties for now, and then work from there.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  How would you do *real* collisions? leRiCl 3 3,071 Mar 19, 2007 05:29 PM
Last Post: Aressera
  METAL - Graphics with OSX / LCDs wardd13 4 3,099 Oct 26, 2006 02:39 PM
Last Post: bronxbomber92
  multiple "done:'s" in Metal BASIC? john8520 2 3,001 Feb 6, 2006 03:37 PM
Last Post: Leisure Suit Lurie
  METAL and tiger mika 4 3,547 Jun 9, 2005 12:05 PM
Last Post: mika
  METAL: Grabbing jpegs DudetheCreator 4 4,326 Mar 12, 2005 03:48 PM
Last Post: hazelden