This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2010-10-20 15:04:40

N3ohunt3r
New Scratcher
Registered: 2010-10-20
Posts: 3

need help with collision of 2 bouncing balls

Hi, I'm new at scratch and we have to programm a new project about 2 bouncing balls, which should bounce and collide with real physics. The problem is, I have no idea, how to make such a difficult programm and need your help. Another problem is, we can't use the "step"-order, but the "go to -x,y position" stuff. Here's the link of my programm. Please help me, thank you  smile

--> http://scratch.mit.edu/projects/N3ohunt3r/1364933

Offline

 

#2 2010-10-21 15:14:29

developdood
Scratcher
Registered: 2009-04-16
Posts: 63

Re: need help with collision of 2 bouncing balls

Have a look at my project, physics marble cannon. This might help you a bit. It can be very complicated making a project like that.


Have a look at some of my games. I made a cool physics game called "Physics Marble Cannon" which is really cool. I let anyone remix any of my projects without permission or credit necessary.

Offline

 

#3 2010-10-21 15:40:19

N3ohunt3r
New Scratcher
Registered: 2010-10-20
Posts: 3

Re: need help with collision of 2 bouncing balls

Ty for the answer. But our problem is about a 2 dimensional elastic collision, that means thats more an vectorial problem. A link to a nice picture of our problem is --> http://www.gymnasium-walldorf.de/mathematik/elastischerstoss/elastischerstoss.htm
another problem is the gravity, it has to be a horizontal throw after the collision and we don't know how to solve this problem of calculating with scratch, because we dont know really know the celerity of the 2 balls. Have a look at the our project/ script to get further information, what we mean.

Offline

 

#4 2011-02-05 19:12:32

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: need help with collision of 2 bouncing balls

Look at my project "elastic bounce multiple."

http://scratch.mit.edu/projects/amcerbu/1576239

If you download, the equations are at the very end of the script.

a is obj 1's x position
b is obj 1's y position
c is 1's x velocity
d is 1's y vel.
e is 2's x pos.
f is 2's y pos.
g is 2's x vel.
h is 2's y vel.
i, j, k, and l are the temporary variables for new x, y velocities for objs 1 and 2 respectively.  If we use c, d and g, h, there are recursive problems, as the equations near the end will be solving with new values.

Please tell me if this helps.

Last edited by amcerbu (2011-02-05 19:12:58)

Offline

 

#5 2011-02-06 04:11:59

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: need help with collision of 2 bouncing balls

Here's how I got around the problem a while back:

firstly you need to variables (e.g. XV and YV). These can represent and store values for your X and Y velocity. You can now get the ball to constantly change it's X position by XV and it's Y position by YV. Setting the variables to positive or negative values moves the ball around the stage at different speeds. Imagine that the first ball is moving along with an XV of 10 and a YV of 3 and it then collides with the second ball. What you need is an if statement script like this one:

if: touching "ball 2"
   set XV to XV * -1
   set YV to YV * -1
endIf

By multiplying both variables by minus one, the direction will change as doing this will either change the variable from negative to positive or positive to negative. Once this conversion has occured the ball will be have an XV of -10 and a YV of -3. If it had a velocity in the negative direction already it will be converted to a positive.

Using this you can easily make your ball collide off anything you want (albeit not amazingly accurately as you'll always end up bouncing directly off of the object, angles are not considered.

You might also like to deduct a small amount of velocity after the collision too, so that the collision slows the ball:

if: touching "ball2"
   set XV to XV * -1
   set YV to YV * -1
   if: XV > 0
      change XV by -2
   else
      change YV by 2
   endIfElse
   if: YV > 0
      change YV by -2
   else
      change YV by 2
   endIfElse
endIf

This way the collision will slow the ball down, as you would expect.

Hope this helps  smile


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#6 2011-02-08 09:08:47

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: need help with collision of 2 bouncing balls

sparks wrote:

Here's how I got around the problem a while back:

firstly you need to variables (e.g. XV and YV). These can represent and store values for your X and Y velocity. You can now get the ball to constantly change it's X position by XV and it's Y position by YV. Setting the variables to positive or negative values moves the ball around the stage at different speeds. Imagine that the first ball is moving along with an XV of 10 and a YV of 3 and it then collides with the second ball. What you need is an if statement script like this one:

if: touching "ball 2"
   set XV to XV * -1
   set YV to YV * -1
endIf

By multiplying both variables by minus one, the direction will change as doing this will either change the variable from negative to positive or positive to negative. Once this conversion has occured the ball will be have an XV of -10 and a YV of -3. If it had a velocity in the negative direction already it will be converted to a positive.

Using this you can easily make your ball collide off anything you want (albeit not amazingly accurately as you'll always end up bouncing directly off of the object, angles are not considered.

You might also like to deduct a small amount of velocity after the collision too, so that the collision slows the ball:

if: touching "ball2"
   set XV to XV * -1
   set YV to YV * -1
   if: XV > 0
      change XV by -2
   else
      change YV by 2
   endIfElse
   if: YV > 0
      change YV by -2
   else
      change YV by 2
   endIfElse
endIf

This way the collision will slow the ball down, as you would expect.

Hope this helps  smile

Sparks, I think the question is asking about elastic collisions between circles.  Try this document, second and third pages:

http://www.vobarian.com/collisions/2dcollisions2.pdf

For an example of this, look at:

http://scratch.mit.edu/projects/amcerbu/1580333

Offline

 

Board footer