Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Romans_I_XVI
Roku Guru

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 😛

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 12
Komag
Roku Guru

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
Roku Guru

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
Roku Guru

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
Channel Surfer

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
Roku Guru

Re: Elastic Collision Ball Physics

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

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-) :oops:
0 Kudos
NewManLiving
Visitor

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
Roku Guru

Re: Elastic Collision Ball Physics

Lol, and thanks 🙂
0 Kudos
renojim
Community Streaming Expert

Re: Elastic Collision Ball Physics

Thanks for sharing!

-JT
Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.