Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
Romans_I_XVI
Level 7

Elastic Collision Ball Physics

Hi everyone, I just thought I'd share something I've been working on. Aside from game developers I doubt many will have much of a use for it.

So for one of my upcomming games I needed to be able to handle collisions between two circular sprites, which was umm... harder than I hoped. In the end though I was able to get it going and I have a working sample channel if anyone is interested.
https://github.com/Romans-I-XVI/Roku-El ... on-Physics

If you just want to see what the channel does without installing you can see here...
https://www.youtube.com/watch?v=httCGL_XwLA

So in this sample we are essentially spawning 20 balls with random sizes that also correlate to their mass, tell them to start moving at a random speed, and then let the ManageBounce() function do its magic Smiley Tongue

If you look at my commit history on github you might see I've been banging my head on the desk trying to make the code *perfect*. Because, although everything works great, there are special circumstances where occassionally a ball will pass through another ball. I know what the cause is, but I don't know the solution. Once two balls have collided I have to mark that the collision has happened because if I don't, and the collision is still occuring in the next frame even though speeds have already been altered, the collision will be processed again and the balls get stuck together. The problem that creates though is that if say another ball comes and hits the ball, while it is still registering that previous collision, it won't register it another time (again because it doesn't want to get stuck together) and will pass right through the ball it originally had a collision with. I've tried getting really crazy with it from about every angle I can think of, handling multiple collisions at a time, disabling the first collision if a second collision occurs ( this just leads back to the problem of balls getting stuck together sometimes ), manually altering speeds. In the end I reverted to the code I had originally that seemed to work just as good ( it's hard to tell for sure since it's so random ) and is a lot less complicated.

So if anyone has any thoughts on getting this to work 100% with balls never passing through each other, even if there were say 40+ on the screen, let me know. Otherwise enjoy the code. Hopefully somebody can use this in a game or something.
0 Kudos
12 Replies
Komag
Level 9

Re: Elastic Collision Ball Physics

That's pretty impressive really. It makes you appreciate a little bit what has been accomplished in general gaming and physics programming in 3D.
0 Kudos
Romans_I_XVI
Level 7

Re: Elastic Collision Ball Physics

Thanks, and yeah I totally agree. So much math just to make two balls bounce against each other, modern physics engines really are a marvel.
0 Kudos
Romans_I_XVI
Level 7

Re: Elastic Collision Ball Physics

I did it!!!

I fixed the problem of balls passing through each other! Here's a video with a very crowded space, and yet are collisions working properly 8-) .
https://youtu.be/2YFNhNFcnkM

Essentially I added an entire new function that checks if the total distance between two balls is less than it should be ( i.e. One is inside of the other, even just slightly ). If so I simply adjust the x and y positions ( without touching the speeds at all, since that is what the other function does ) so that it is at the point where the edges are just barely touching, essentially where it would be in a real world collision where balls passing through each other is physically impossible lol.

The solution wasn't necessarily easy, but it was much easier than some of the other madness I was trying, and it is also a much more elegant solution.

:mrgreen:
0 Kudos
TheEndless
Level 7

Re: Elastic Collision Ball Physics

What Roku model are you running that on? Looks like you're achieving an impressive framerate for having so many sprites on screen at once.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
Romans_I_XVI
Level 7

Re: Elastic Collision Ball Physics

Roku 3, if I do that on Roku 1 it's very laggy.
0 Kudos
Komag
Level 9

Re: Elastic Collision Ball Physics

"Romans_I_XVI" wrote:
I did it!!!

The solution wasn't necessarily easy, but it was much easier than some of the other madness I was trying, and it is also a much more elegant solution.

:mrgreen:


Congrats - I love little breakthroughs, always make me feel so smart and satisfied and extra-nerdy! 8-) Smiley Surprisedops:
0 Kudos
NewManLiving
Level 7

Re: Elastic Collision Ball Physics

Thanks for sharing. Good example of Sprite collision
Couldn't believe it - :shock: you even got the desk to shake when they collided
My Channels: 2D API Framework Presentation: https://owner.roku.com/add/2M9LCVC
Updated: 11-11-2015 - Completed Keyboard interface
The Joel Channel ( Final Beta )
0 Kudos
Romans_I_XVI
Level 7

Re: Elastic Collision Ball Physics

Lol, and thanks Smiley Happy
0 Kudos
renojim
Level 8

Re: Elastic Collision Ball Physics

Thanks for sharing!

-JT
0 Kudos