# Gravity

i’m trying to recreate gravity. this is what i have so far:

[AS]G = 0.005;
m1 = ball1._width;
m2 = ball2._width;
scrnW = 550
scrnH = 400
ball1.onEnterFrame = function() {
this.diffx = ball2._x-this._x;
this.diffy = ball2._y-this._y;
this.dx += (Gm1m2)/this.diffx;
this.dy += (Gm1m2)/this.diffy;
this._x += this.dx;
this._y += this.dy;
this._x = this._x > scrnW ? 0 : this._x < 0 ? scrnW : this._x
this._y = this._y > scrnH ? 0 : this._y < 0 ? scrnH : this._y
};
ball2.onEnterFrame = function() {
this.diffx = ball1._x-this._x;
this.diffy = ball1._y-this._y;
this.dx += (Gm1m2)/this.diffx;
this.dy += (Gm1m2)/this.diffy;
this._x += this.dx;
this._y += this.dy;
this._x = this._x > scrnW ? 0 : this._x < 0 ? scrnW : this._x
this._y = this._y > scrnH ? 0 : this._y < 0 ? scrnH : this._y
};[/AS]

the script looks alright to me but when run, the effect is wrong…

Is this of any help?

http://www.bit-101.com/tutorials/gravity.html

I’m not too sure what you’re trying to do but isn’t it something like this tutorial?
http://www.kirupa.com/developer/actionscript/gravity.htm

Hehe, I forgot all about that tutorial

sorry for not explaining to well what i was trying to achieve…

those tutorials are the basickind of gravity where the pull is only downwards. the gravity im looking for is between two objects in space with different masses. does that explain it better?

If I’m not mistaken, you’re dong a mistake with the speed.

``````ball1.onEnterFrame = function() {
this.diffx = ball2._x-this._x;
this.diffy = ball2._y-this._y;
this.dx += (G*m1*m2)/this.diffx;
this.dy += (G*m1*m2)/this.diffy;
this._x += this.dx;
this._y += this.dy;
};
``````

should be

``````ball1.onEnterFrame = function() {
this.diffx = ball2._x-this._x;
this.diffy = ball2._y-this._y;
this.vx += (G*m1*m2)/this.diffx;
this.vy += (G*m1*m2)/this.diffy; // speed
this.dx += this.vx;
this.dy += this.vy; // motion
this._x += this.dx;
this._y += this.dy;
};
``````

And the force applied to ball2 is simply the opposite

I’ve tried to do that kind of things a long time ago, and you’ll probably have to change the numbers quite a few times before you can get something that works properly.

Good luck.
pom :phil:

maybe you can make a simple fla for me?

because it just does the same thing as what i had… the ball accelerates towards the other ball but once it passes it, it just keeps accelerating rather than slowing down and returning… i hope that explains what’s happening…

right now i just want one ball (ball1) to gravitate towards ball2 which is stationary. this is the script that i have… :
[AS]G = 3
m1 = 1
m2 = 1
ball1.onEnterFrame = function() {
this.diffx = ball2._x-this._x;
this.diffy = ball2._y-this._y;
this.vx += (Gm1m2)/this.diffx;
this.vy += (Gm1m2)/this.diffy; // speed
this.dx += this.vx;
this.dy += this.vy; // motion
this._x += this.dx;
this._y += this.dy;
};[/AS]

anybody???

i played with it a bit

flash5

doesnt quite work, but more than above
it gravitates aways, and then back and over, and then sort of a anti-polarity off and away

``````
count=1;
}

onClipEvent(enterFrame) {
G = 3;
m1 = 1;
m2 = 1;

diffx = Math.abs(ball2._x-this._x);
diffy = Math.abs(ball2._y-this._y);
if(count<40 && this._x<550 && this._x>0 && this._y<400 && this._y>0) {
vx += (G*m1*m2)/diffx;
vy += (G*m1*m2)/diffy;
dx += vx;
dy += vy;
this._x += dx;
this._y += dy;
count+=1;
}
else if (count>=0 && this._x<550 && this._x>0 && this._y<400 && this._y>0) {
vx -= (G*m1*m2)/diffx;
vy -= (G*m1*m2)/diffy;
dx -= vx;
dy -= vy;
this._x -= dx;
this._y -= dy;
for(i=40; i<0; i--) {
count -=1;
}
}
}

``````

what about something to do with a circle surrounding the center clip
moving within its bounds

soup

i didn’t think this would cause me so much trouble… there must be something wrong with the formula… next year after i have taken physics i’ll know how to fix this… for now, does anybody else know any physics and see anything wrong with the forumlas?

-zylum

i was trying to adjust it so it knows which direction its going
and make it reach a point and reverse
other wise., you had no bounds for it, and were adding to its _x
and _y with each frame at an increasing velocity with increased
distance

i would like to know how to do it

http://physics.webplasma.com/physics08.html#formula

maybe this will help

soup

I got it working !! this is fuuuun stuff. look at this…
[AS]
gravitate= function(ball1,ball2) {
var diffx = (ball1._x-ball2._x);
var diffy = (ball1._y-ball2._y);
var dist = Math.sqrt(diffxdiffx+diffydiffy);
var d = (Gball1.m)/(distdist); //force
var a = Math.atan2(diffy,diffx);
var ax = dMath.cos(a);
var ay = d
Math.sin(a); // acceleration
ball2.vx += ax;
ball2.vy += ay; // motion
ball2.dx += ball2.vx;
ball2.dy += ball2.vy; //postition
ball2._x = ball2.dx;
ball2._y = ball2.dy;
}
go=function(){
for(var i=0;i<16;i++)
{
gravitate(ball1,ball2);
/comment this line (//)
gravitate(ball3,ball1);
gravitate(ball3,ball2);
gravitate(ball2,ball1);
gravitate(ball2,ball3);
gravitate(ball1,ball3);
//
/
}
}
G=1;

onMouseDown=function(){
onEnterFrame=go;

``````ball2.dx=ball2._x;
ball2.dy=ball2._y;
ball2.vx=0.09;
ball2.vy=0.0;
ball2.m=1;

ball1.dx=ball1._x;
ball1.dy=ball1._y;
ball1.vx=0;
ball1.vy=0;
ball1.m=1;

/**/ ball3._visible=false;/* comment this line (//)
ball3.dx=ball3._x;
ball3.dy=ball3._y;
ball3.vx=-0.4;
ball3.vy=0;
ball3.m=7;
//*/

delete onMouseDown;
``````

}
stop();
[/AS]
two scenarios in one thanks to commenting out code

edit: the way php displays it really messes up the comments. it will look nicer in flash

wow!!! thanks clownstaples!!! someone has finally figured it out!!! thanks again… time to study the script:)

-zylum

its a really finicky formula… one bad variable and the balls just fly off the screen! Severe tweaking proved to be the only remedy, but the end result is worth it.

messed with it some more
somewhat based on the gravity tutorial here
flash5
on the stickman

``````
// gravity is what I called g in the tutorial. The
// higher g the harder the ball will fall.
// gravity = 0 can be set, as an experiment, but
// it will in fact create a "zero gravity" effect
// gravity < 0 will create an inverted gravity effect
gravity = 3 ;

// This sets the _y position of the floor
floor = _root.ball._x ;
floorx1 = _root.ball._x-this._width ;
floorx2 = _root.ball._x+this._width ;

// Bounce is a number < 1 but close to 1
// The closer to 1, the higher the ball will bounce
bounce = 0.94 ;

// We set the speed of the ball when it is released.
speedx = 0 ;
speedy = 0 ;
}

onClipEvent (enterFrame) {
if (pressing) {

// if we are pressing
// drag the object
startDrag (this,true) ;
// calculate the speed
speedx = this._x - x0 ;
speedy = this._y - y0 ;
// set a new reference point
x0 = this._x ;
y0 = this._y ;

} else {
floor = _root.ball._x ;
floorx1 = _root.ball._x-this._width ;
floorx2 = _root.ball._x+this._width ;
stopDrag () ;
speedy = speedy + gravity ;

this._x += speedx/5 ;
this._y += speedy/5 ;

if (this._y > floor) {
this._y = floor ;
speedy *= -bounce ;

}
if (this._x < floorx1) {
this._x = floorx1 ;
speedx *= -bounce ;

}
if (this._x > floorx2) {
this._x = floorx2 ;
speedx *= -bounce ;

}
// soup squish ball on walls
// squish is relative to speed along coresponding axis
if (this._y == floor) {
this._yscale = 15-Math.abs(speedy/10);
} else if (this._y<floor) {
this._yscale = 15;
}
if (this._x == floorx1 or this._x == floorx2) {
this._xscale = 15-Math.abs(speedx/10);
} else if (!this._x == floorx1 or !this._x == floorx2) {
this._xscale = 15;
}
// end soup
}
}

``````

on the red ball

``````

// gravity is what I called g in the tutorial. The
// higher g the harder the ball will fall.
// gravity = 0 can be set, as an experiment, but
// it will in fact create a "zero gravity" effect
// gravity < 0 will create an inverted gravity effect
this.gravity = 2 ;

// This sets the _y position of the floor
this.floor = 375 ;
this.floorx1 = 0 ;
this.floorx2 = 525 ;

// Bounce is a number < 1 but close to 1
// The closer to 1, the higher the ball will bounce
this.bounce = 0.99 ;

// We set the speed of the ball when it is released.
this.speedx = 0 ;
this.speedy = 0 ;
}

onClipEvent (enterFrame) {
if (this.pressing) {

// if we are pressing
// drag the object
startDrag (this,true) ;
// calculate the speed
this.speedx = this._x - this.x0 ;
this.speedy = this._y - this.y0 ;
// set a new reference point
this.x0 = this._x ;
this.y0 = this._y ;

} else {
stopDrag () ;
this.speedy = this.speedy + this.gravity ;

this._x += this.speedx/5 ;
this._y += this.speedy/5 ;

if (this._y > this.floor) {
this._y = this.floor ;
this.speedy *= -this.bounce ;

}
if (this._x < this.floorx1) {
this._x = this.floorx1 ;
speedx *= -this.bounce ;

}
if (this._x > this.floorx2) {
this._x = this.floorx2 ;
speedx *= -this.bounce ;

}
// soup squish ball on walls
// squish is relative to speed along coresponding axis
if (this._y == this.floor) {
this._yscale = speedy/2;
} else if (this._y<this.floor) {
this._yscale = 100;
}
if (this._x == this.floorx1 or this._x == this.floorx2) {
this._xscale = speedx/2;
} else if (!this._x == this.floorx1 or !this._x == this.floorx2) {
this._xscale = 100;
}
// end soup
}
}

``````

http://www.route108.com/soup/portfolio/zylum_Gtest_3.swf

soup