bounce ball with collide?

Problems with Canvas, OpenGL, etc...

bounce ball with collide?

Postby ibtmubarak » Thu May 19, 2011 8:30 am

Hi

How to make two or more circle to collide inside the view or frame and bounce back?
Like colorballz apps.


Thanks
Mubarak
mubarak
ibtmubarak
Developer
Developer
 
Posts: 44
Joined: Thu Feb 28, 2008 12:26 pm
Location: Bangalore

Top

Re: bounce ball with collide?

Postby MichaelEGR » Thu May 19, 2011 9:48 am

Check these great articles out:
http://www.wildbunny.co.uk/blog/2011/04/20/collision-detection-for-dummies/
http://www.wildbunny.co.uk/blog/2011/04/06/physics-engines-for-dummies/

There is source code available for purchase and you'll have to translate it to Java, but these two articles cover what you are asking for and a lot more!
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Re: bounce ball with collide?

Postby ibtmubarak » Thu May 19, 2011 10:27 am

Thanks MichaelEGR for your quick reply.Very interesting.
Now i m going to study these articles before that, is there any sample code for bounce ball with collide in android?


Thanks
Mubarak
mubarak
ibtmubarak
Developer
Developer
 
Posts: 44
Joined: Thu Feb 28, 2008 12:26 pm
Location: Bangalore

Re: bounce ball with collide?

Postby MichaelEGR » Thu May 19, 2011 11:08 am

I'll be releasing a bunch of tutorials w/ source code when I launch my real time app / game dev middleware called TyphonRT. I'll be including a bunch of essential physics examples like those in the linked articles. It's not hard to translate that article code though and is a good exercise for you. All the info you need is found in the articles and you can purchase full source code from the article author if the snippets in the articles are not complete enough to reconstruct the concepts covered. So it will be a minor ActionScript to Java translation. I recommend implementing things in a custom View by overriding onDraw and simply drawing circles or bitmaps in the 2D graphics API as that is easiest to get going. At this point a little patience and learning to code with Android will get the results you are attempting to achieve.
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Re: bounce ball with collide?

Postby ibtmubarak » Thu May 19, 2011 12:34 pm

Ok MichaelEGR. Thanks a lot for your valuable information.


Thanks
Mubarak
mubarak
ibtmubarak
Developer
Developer
 
Posts: 44
Joined: Thu Feb 28, 2008 12:26 pm
Location: Bangalore

Re: bounce ball with collide?

Postby ibtmubarak » Sat May 21, 2011 11:14 am

Hi MichaelEGR,

I tried for ball bounce with box for collision - done. But i m still trying for collision between two balls.. can you give me any sample code for collision between two balls?.So that i can do further learning process easily.


Thanks
Mubarak
mubarak
ibtmubarak
Developer
Developer
 
Posts: 44
Joined: Thu Feb 28, 2008 12:26 pm
Location: Bangalore

Top

Re: bounce ball with collide?

Postby ibtmubarak » Sat May 21, 2011 11:49 am

for (int i = 0; i < numBall1; i++) {
final Ball1 ball1 = mBall1.get(i);
for (int j = 0; j < numBall2; j++) {
final Ball2 ball2 = mBall2.get(j);
if (ball1.collidesWith(ball2)) {
// Collision happened
}
}
}


i m trying to use the above sample for collision between two balls....
mubarak
ibtmubarak
Developer
Developer
 
Posts: 44
Joined: Thu Feb 28, 2008 12:26 pm
Location: Bangalore

Re: bounce ball with collide?

Postby MichaelEGR » Sat May 21, 2011 11:43 pm

What you are posting is basic framing / conditional code for detecting a collision without the collision detection code. You haven't tried to write it yet.

Again the info you seek is found in the articles I mentioned. When two circles or even spheres collide all you have to do is see if the distance between the center points is less than the sum of the radii of the circles. Read this article again and you'll see this info in "figure 1" about a 1/4 of the way down the page; there is a graphic image that clearly shows what you need to do:

http://www.wildbunny.co.uk/blog/2011/04/06/physics-engines-for-dummies/

So do study those articles and as I mentioned it's probably worth buying the full source code so you can simply translate what you need to Java, etc.

Because there are additional shortcuts / efficiency calculations I'll be nice and give you some pseudocode..

Code: Select all
float combinedRadius = ball1.radius + ball2.radius

// Get the distance between the balls and check if there is a collision; note this is a short cut insofar
// that we are removing a square root calculation which is very expensive and are checking with the
// distances squared.
float dX, dY;

dX = ball1.x - ball2.x;
dY = ball1.y - ball2.y;

if (combinedRadius * combinedRadius > dX * dX + dY * dY)
    <COLLISION OCCURRED>

Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 1 guest