Physics and gravity...
- Glaiel-Gamer
-
Glaiel-Gamer
- Member since: Dec. 28, 2004
- Offline.
-
- Forum Stats
- Member
- Level 28
- Game Developer
I have 2 symbols, a symbol named "ship" and a nonamed one. They are both spheres. Does anyone know the code so that the nonamed symbol has "Gravity" so the ship always gets pulled toward the nonamed one? (I'm not talking about "falling straight down" I'm talking about an objet getting pulled toward another one)
- Idoru
-
Idoru
- Member since: Jan. 11, 2002
- Offline.
-
- Forum Stats
- Member
- Level 02
- Blank Slate
will the ship have thrusters that allow it escape the gravity pull?
- Glaiel-Gamer
-
Glaiel-Gamer
- Member since: Dec. 28, 2004
- Offline.
-
- Forum Stats
- Member
- Level 28
- Game Developer
no, actually, it is more of a meteor. It just moves in a line untill changed by gravity.
- Idoru
-
Idoru
- Member since: Jan. 11, 2002
- Offline.
-
- Forum Stats
- Member
- Level 02
- Blank Slate
make a movieclip on _root and name is star, make also your meteor;
put in the following:
onClipEvent(load) {
x_velocity = 0;
y_velocity = 0;
k_constant = 1.2;
starmass = 5000;
}
onClipEvent(enterFrame) {
xdiff = this._x - _root.star._x;
ydiff = this._y - _root.star._y;
k_constant = 3
deg_diff = Math.atan2(ydiff,xdiff);
radius = Math.sqrt(Math.pow(xdiff,2)+Math.pow(ydiff,2))
force_div_mass = k_constant*((starmass)/(Math.pow(radius,2)));
// deriving velocity from gravitation equation
x_ac = force_div_mass*Math.cos(deg_diff);
y_ac = force_div_mass*Math.sin(deg_diff);
// new velocities
x_velocity-=x_ac;
y_velocity-=y_ac;
// new positions
_x+=x_velocity;
_y+=y_velocity;
}
This doesn't have hittest but I'm sure you'll figure that out. initial x and y velocity is set to zero. I did this in a rush, so I'm sure the script can be shortened, the script basically came from a translation of the gravitation law from physics. Tweak the mass of the star and the k_constant to get desired effect. Have fun.
- Idoru
-
Idoru
- Member since: Jan. 11, 2002
- Offline.
-
- Forum Stats
- Member
- Level 02
- Blank Slate
oops, should really add in hittest, since classically if it were to approach the sun without collision it the radius would go off to infinity and force becomes infinite. also there's a weird slingshot affect, I guess that's physically possible.
- Glaiel-Gamer
-
Glaiel-Gamer
- Member since: Dec. 28, 2004
- Offline.
-
- Forum Stats
- Member
- Level 28
- Game Developer
- Idoru
-
Idoru
- Member since: Jan. 11, 2002
- Offline.
-
- Forum Stats
- Member
- Level 02
- Blank Slate
- Begoner
-
Begoner
- Member since: Oct. 10, 2004
- Offline.
-
- Forum Stats
- Member
- Level 10
- Blank Slate
I'll thank you for him. That was a nice script, Idoru!
- Glaiel-Gamer
-
Glaiel-Gamer
- Member since: Dec. 28, 2004
- Offline.
-
- Forum Stats
- Member
- Level 28
- Game Developer
http://img34.exs.cx/my.php?loc=img34&image=gravity5sv.swf
Now I say thanks, cause I know it works now!
THANX! :)
After tweaking it a bit, this is what I made. I'll somehow turn it into a game.
- Idoru
-
Idoru
- Member since: Jan. 11, 2002
- Offline.
-
- Forum Stats
- Member
- Level 02
- Blank Slate
you're very welcome. Gladto know I'm still appreciated around here.
- Glaiel-Gamer
-
Glaiel-Gamer
- Member since: Dec. 28, 2004
- Offline.
-
- Forum Stats
- Member
- Level 28
- Game Developer
Yeah, I'm only a freshman in High School, so I havent studied physics yet. Thanks for the script again.

