A Flash Developer Resource Site

# Thread: Incredibly Easy Physics and Gravity!!!!

1. For all of you with questions pertaining to Physics and Gravity, here is a bug free solution to your problem!
Here are the Steps:

-> Open a new movie and set its dimensions to 550x400
-> Make a ball movie clip.
-> Go into actions
-> Type this in
Code:
```onClipEvent(enterFrame)
{
// this keeps track of the ball speed.
_x = xspeed + _x;
_y = yspeed + _y;
// this slows down the ball
xspeed = xspeed*0.9;
yspeed = yspeed*0.9;
// makes dat ball bounce
if (_y>400)
{
yspeed = -yspeed;
}
if (_x>550)
{
xspeed = -xspeed;
}
else if (_x<0)
{
xspeed = -xspeed;
}
}
{
xspeed = 5;
yspeed = -7;
}```
The solution to all physics problems!

2. There is more to physics than bouncing off walls... unless I'm not getting somthing here.

3. bug free solution
I did as you suggest, and make a ball _y=5; Copy your codes into the ball. Then test movie.

How come the ball goes off the screen straightly without bounce ?! I think there must be a bug in your code.

4. aaaah! all my problems are solved!

5. Darn, it seems like your site's out of business now, eh bit?

6. are you kidding? with this new information, i'll be able to do crazy stuff!

7. oh you make me laugh

More seriously, DarkMagicHacker, this sort of code is unsuitable for Flashkit, due to our rating. This really could blow some of the younger 13-15yr olds.

8. blow what?

how is it unsuitable

it may not be that great cause the bounces are not true bounces they just hit something and reverse direction at a constant speed, but how is it unsuitable

9. Originally posted by mouse111
blow what?

how is it unsuitable

it may not be that great cause the bounces are not true bounces they just hit something and reverse direction at a constant speed, but how is it unsuitable
Swwwoooooossshhhh!

10. Originally posted by mouse111
blow what?

how is it unsuitable

it may not be that great cause the bounces are not true bounces they just hit something and reverse direction at a constant speed, but how is it unsuitable
You see, it's unfortunitly too late for some of us. Please, any parents out there, ban this thread on your computer.

11. Whaaa...*Tries to piece together with 10th grade Geometry knowledge. 9Th grade Algerbra Knowledge.*

Brain Crashes....

12. Originally posted by muckyMuckMan
Swwwoooooossshhhh!

ugh i understand now

its a pretty simplistic way of making a cheasy effect

o well

13. heres' an example, with air resistance and draggable throwable ball

http://www.geocities.com/nohuntersweb/heheagain.html

just the unnatural stop and the tendency to keep on bouncing, which is caused by the imperfect gravity script

this was in the arena's actionscript battle

14. Originally posted by Ed Mack
oh you make me laugh

More seriously, DarkMagicHacker, this sort of code is unsuitable for Flashkit, due to our rating. This really could blow some of the younger 13-15yr olds.
omg, im in 8th grader, heres the code:

onClipEvent (enterFrame) {
if (_x>300-_width/2) {
_x = 300-_width/2-1;
xv = xv*-0.9;
}
if (_x<0+_width/2) {
_x = 0+_width/2+1;
xv = xv*-0.9;
}
if (_y<0+_width/2) {
_y = 0+_width/2+1;
yv = yv*-0.9;
}
if (_y>300-_width/2) {
_y = 300-_width/2-.1;
yv = yv*-0.9;
}
if (!drag) {
xv = xv*.99;
yv = yv*.99 +.5;
_x = _x += xv;
_y = _y += yv;
} else {
xv = _x-x;
yv = _y-y;
x = _x;
y = _y;
}
}

Thats the best i can do (it should work)..

Ed Mack, come on man stop picking on the little kids (me)

15. Originally posted by mouse111
blow what?

how is it unsuitable

it may not be that great cause the bounces are not true bounces they just hit something and reverse direction at a constant speed, but how is it unsuitable
When something bounces off of a wall does it come back off the ball at the same speed as it hit the wall? no you instead of * the velocity by -1 multiply it by -0.9 that way it slows the ball down a little... making a more relistic script..

16. Originally posted by xDrunkCowx

Ed Mack, come on man stop picking on the little kids (me)
I think you completly missed the jokes.... they were with Bit101 mainly.

Anyway, I'm not quite sure of how the school system works in other countries, but I'm probably about the same age as you.

17. well im 14 and in 8th grader... i dont get what u ment by they are with bit101??

18. Originally posted by mouse111
ugh i understand now

its a pretty simplistic way of making a cheasy effect

o well
cool i guess.. but its wierd how when the ball starts moving at a slow speed it just stops... i take it ur using a if statment to do change the _x velocity?

Can you post the same code for me with an explanation of the different parts.

thanx bud

20. onClipEvent (enterFrame) {
// checking if the wall if hitting the right wall.. To find this out you take the _x - _width/2 and if that is greater than or equal to the _x of the wall then the ball is touching. _width/2 is the radius,
if (_x>300-_width/2) {
// if the ball is touching the wall then you move it back so its not touching the ball. Then the ball would just keep on hitting that one wall then mving back and doing this for ever. So now we need to change the xv, the angel a object hits a wall is the same angel the object leaves the wall but reversd. For example if the ball xv is 5 when it hits the wall then it needs to be changed to -5. to do that we would just take the xv and multiply it by -1 so if its a positive number then it turns into a negitive number and if its a negitive number then it turns into a positive number. But when a object hits a wall (remember we are doing elastic collision so the ball bounces) it looses speed so we multiply it by -0.9 so if the xv is 2 then now its -1.8.
_x = 300-_width/2-1;
xv = xv*-0.9;
}
if (_x<0+_width/2) {
_x = 0+_width/2+1;
xv = xv*-0.9;
}
if (_y<0+_width/2) {
_y = 0+_width/2+1;
yv = yv*-0.9;
}
if (_y>300-_width/2) {
_y = 300-_width/2-.1;
yv = yv*-0.9;
}
// now that we have checked to see if the ball is hitting any of the walls, and if it is we change the velocitie. Now what about gravity and friction? Well to add friction just multiply the xv and yv by .99 couse .99 makes the most relistic friction. Then to add gravity just add .5 to the yv so that way no matter what the bal is going to want to move downword.
// if the ball is not being draged then the script calculates friction and gravity and then moves the ball(as seen above).
if (!drag) {
xv = xv*.99;
yv = yv*.99 +.5;
_x = _x += xv;
_y = _y += yv;
} else {
// if the user is dragging the ball then we need to find the throwing power, so you take the oldx and y and subtract it from the new x and y and the distance between is the new xv and yv.
xv = _x-x;
yv = _y-y;
x = _x;
y = _y;
}
}
Now just open the circle mc and make the circle a button and put:
on (press) {
startDrag (this);
drag = true;
}
on (release, releaseOutside) {
stopDrag ();
drag = false;
}

This is from a tut i wrote earlyer on my site http://xdrunkcowx.tripod.com/ other tuts thier to..
i know thier are errors on the tut BUT im doing this with my time for free so as long as u can understand it... dont tell me about spelling or grammer errors

#### 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