View Full Version : gravity
zylum
June 24th, 2003, 12:26 AM
i'm trying to recreate gravity. this is what i have so far:
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 += (G*m1*m2)/this.diffx;
this.dy += (G*m1*m2)/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 += (G*m1*m2)/this.diffx;
this.dy += (G*m1*m2)/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
};
the script looks alright to me but when run, the effect is wrong...
lostinbeta
June 24th, 2003, 01:19 AM
Is this of any help?
http://www.bit-101.com/tutorials/gravity.html
blah-de-blah
June 24th, 2003, 01:21 AM
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
lostinbeta
June 24th, 2003, 01:23 AM
Hehe, I forgot all about that tutorial :)
zylum
June 24th, 2003, 02:20 PM
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?
pom
June 25th, 2003, 10:30 AM
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:
zylum
June 25th, 2003, 05:07 PM
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... :
G = 3
m1 = 1
m2 = 1
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;
};
zylum
June 26th, 2003, 06:21 PM
anybody???
soup
June 26th, 2003, 09:40 PM
i played with it a bit
just to see
http://www.route108.com/soup/portfolio/zylum_Gtest_1.swf
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
onClipEvent (load) {
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
zylum
June 27th, 2003, 02:23 PM
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
soup
June 27th, 2003, 07:34 PM
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
Clown Staples
June 27th, 2003, 10:06 PM
I got it working !! this is fuuuun stuff. look at this..
gravitate= function(ball1,ball2) {
var diffx = (ball1._x-ball2._x);
var diffy = (ball1._y-ball2._y);
var dist = Math.sqrt(diffx*diffx+diffy*diffy);
var d = (G*ball1.m)/(dist*dist); //force
var a = Math.atan2(diffy,diffx);
var ax = d*Math.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();
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
zylum
June 28th, 2003, 12:16 AM
wow!!! thanks clownstaples!!! someone has finally figured it out!!! thanks again... time to study the script:)
-zylum
Clown Staples
June 28th, 2003, 12:28 AM
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.
soup
June 29th, 2003, 03:47 PM
messed with it some more
somewhat based on the gravity tutorial here
flash5
on the stickman
onClipEvent (load) {
// 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
onClipEvent (load) {
// 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
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.