A Flash Developer Resource Site

1. ## ball physics

Can someone - in simple words - explain to me the physics of a ball? (Or point me towards a tutorial.)
Especially when two balls collide. How can I calculate the change of movement according to speed, direction and the point of impact - and implement it into actionscript.

2. What kind of ball? Billiards? Basketball?

Do you need to take gravity into account?

Are you using an overhead or sideways view?

Are all your balls the same size?

3. Nope. To study the actual physics of it can get kind of in depth when the balls motion is not along the line of impact. However, there are some pre-made scripts out there ready to go. Some are damn good too, commented explaining each variable so the user can tweak it to their personal need. I will try and find one.

4. ## ball physics

First - minger and jbum - thanks for your replies.

I am not thinking of anything special like a ball game. I try to build something very simple. Just two - same sized - ball shaped -objects moving around in a confined space. They collide from time to time. The bounce back from the walls etc.. I use a overhead view and there is no gravity or any other distracting force.

It is working all right if you have walls at top-sides-bottom and when I treat them as if they were only moving points without any shape. Still my objects are doing strange things and I can't think of a solution.

5. What sort of wierd things, can you point us at a .swf, or something? We can't help without knowledge of the problem.

6. The following script demonstrates colliding superballs. It could also be modified to resemble billiard balls (by setting gravity to zero, and adjusting some of the other constants).

Super Balls

You can model a simple ball as having a center, given by the movie clips's _x and_y, and a radius r (which would typically be close to or identical to the movieclips's _width/2.

The following code will give you a very simple 'pong' style model.

Code:
```mc._x;  // ball center x
mc._y;  // ball center y
mc.vx = 0; // used to store the ball's velocity X
mc.vy = 0; // used to store the ball's velocity y```

You start a ball in motion by modifying it's velocity, like so:

mc.vx = 5;
mc.vy = 2;

The ball can update it's movement each frame like so:

Code:
```mc.onEnterFrame = function()
{
// Perform modifications to this.vx, this.vy here - collision checks, wall checks etc.
// ...

this._x += this.vx;
this._y += this.vy;
}```
When the ball hits a left or right wall, reverse it's x velocity.

mc.vx *= -1;

When it hits a top or bottom wall, reverse its y velocity.

mc.vy *= -1;

For collision with other balls, you can use hitTest to determine collision, or you can do it manually, like in the following script (taken from the 'superballs' script above), which handles collisions for an array of balls:

Code:
```dampCollision = .95;
ballWidth = 100; // standard ball width in this example

MovieClip.prototype.handleCollisions = function()
{
for (var i = mcs.length-1; i >= 1; --i) {
var x1 = mcs[i]._x;
var y1 = mcs[i]._y;
for (j = i-1; j >= 0; --j) {
var dx = mcs[j]._x - x1;
var dy = mcs[j]._y - y1;
var dist = Math.sqrt(dx*dx+dy*dy);
if (dist < ballWidth) {
// collide - trade energy (mag1,mag2), and travel in opposite direction of collision
var mag1 = Math.sqrt(mcs[i].vx*mcs[i].vx+mcs[i].vy*mcs[i].vy);
var mag2 = Math.sqrt(mcs[j].vx*mcs[j].vx+mcs[j].vy*mcs[j].vy);
mcs[j].vx = (mag1*dx/dist)*dampCollision;
mcs[j].vy = (mag1*dy/dist)*dampCollision;
mcs[i].vx = -(mag2*dx/dist)*dampCollision;
mcs[i].vy = -(mag2*dy/dist)*dampCollision;
}
}
}
}```

- Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap