Cover
 This is the archive Discourse for the Processing (ALPHA) software. Please visit the new Processing forum for current information.
 Processing 1.0 _ALPHA_    Programming Questions & Help    Programs (Moderators: fry, REAS)    beginner: collision detection « No topic | Next topic »

 Pages: 1
 Author Topic: beginner: collision detection  (Read 8576 times)
roland_g

 beginner: collision detection « on: Apr 17th, 2005, 10:35pm »

so I made this little app some time ago that let's a bunch of balls bounce off the edges and let's them change the size once they do hit the edge.

This is the first time I've used a class and it seems to be working, but sometimes a ball gets stuck and I don't quite know why.

If anybody could tell me how to optimize the code I'd welcome it.

Now the next thing I want to do is make the balls bounce off of each other, but I haven't yet found anything that would help on my way to getting there.

Could anybody point me to a link that explains basic object collision or give me some tips?

thanks

Roland
Code:
mflux

 Re: beginner: collision detection « Reply #1 on: Apr 18th, 2005, 3:28am »

where does it get stuck? I haven't seen it get stuck yet.

Nothing to optimize here really, besides silly things like *.5 instead of /2. I don't think that'll speed it up noticibly here, though.

As far as ball to ball collision, simply make a function (call it "collide()" or something) inside the ball class, then have it loop through all the other instances of that class (in your case.. Ball[]). Have it test each ball's distance against itself, minus their radius.

If they are within radius-length of each other, swap their velocities (direction of motion).
Fish-face

 Re: beginner: collision detection « Reply #2 on: Apr 18th, 2005, 5:02pm »

mflux's collision method is slightly inaccurate; it only works for head on collisions. If two balls 'glance' each other then their momentum is shared between them according to the angle at which they strike. To do collision checking simply:

Step 1: Iterate through Ball classes, check if this one is touching/covering another one
Step 2: Move the two balls apart so that they are edge to edge
Step 3: Work out the momentum to give to each ball. This can be worked out thus:

Each ball's momentum is comprised of two separate, equal momentums each at 45 degrees to its direction of motion. If the balls collide head on (collision angle of 0 degrees) then all of both momentums is given to the other. If the balls are of equal masses and velocities, this causes them both to halt.
If they collide at 45 degrees, then all of one momentum is given, but the momentum that is in a direction perpendicular to the angle of collision is retained. This causes the balls to glance of each other, at (again, only if equal masses + velocities, opposite directions) 45 degree angles compared to their original  directions.
At a collision angle of 90 degrees, there has not actually been a collision, so all momentum is retained.

This is difficult to explain, but you can have great fun sliding coins across the table to work stuff out.

--

The C@ S@ on the M@
=Fish-Face=
~#Chris#~
 Pages: 1

 Forum Jump: ----------------------------- Discussion -----------------------------  - Community, Collaboration, Status   - Events, Publications, Propaganda   - General Processing Discussion ----------------------------- Programming Questions & Help -----------------------------  - Syntax => Programs   - Integration ----------------------------- Topics & Contributions -----------------------------  - Tools   - Responsive Form, Games   - Information Visualization   - Simulation, Artificial Life   - Tangible Computing   - Automated Systems   - Sound   - Video, Camera   - Beyond Categories ----------------------------- Suggestions -----------------------------  - Software Suggestions   - Website, Reference, Example Suggestions ----------------------------- Bugs -----------------------------  - Software Bugs   - Website, Reference, Example Bugs   - Bug Fixes, Implemented Suggestions ----------------------------- Teaching -----------------------------  - Course Blueprints   - Theory and Practice « No topic | Next topic »