00:00
00:00
View Profile shaymus22

Age 91

Location

Joined on 11/1/08

Level:
1
Exp Points:
10 / 20
Exp Rank:
> 100,000
Vote Power:
1.50 votes
Rank:
Civilian
Global Rank:
> 100,000
Blams:
0
Saves:
4
B/P Bonus:
0%
Whistle:
Normal
Medals:
6

Gravity is very very fun

Posted by shaymus22 - December 14th, 2008


But it's the pain in the ass to figure out, though! Once it's been figured out though, the same formulas can be used for multiple projects. So far using gravity I've made a game (Space Collector) and a toy (Graviton).

In terms of programming, this isn't the hard part, so I don't feel the need to necessarily keep this code to myself, so here you all go. For those of you who understand, this is the gravity for Graviton in the class for the balls (not all of this code is in the same place, but it's there to make it easier to understand what I'm talking about):

var Vx:Number = 0;
var Vy:Number = 0;
var acceleration:Number = 6.6742e-11;
var normalGravity:Number = 9.81e-5 * 0;
var xDistance:Number = 0;
var yDistance:Number = 0;
var xPull:Number = 0;
var yPull:Number = 0;

//Newgrounds: This code is within a for() loop, but I didn't include that when pasting this code.
//Suffice it to say that it looks at every item in the generatorArray.

if( MovieClip(parent).generatorArray[i].on ButtonEnabled )
{
xDistance = Math.sqrt( Math.pow( ( this.x - MovieClip(parent).generatorArray[i].x ) , 2 ) ) + 1;
yDistance = Math.sqrt( Math.pow( ( this.y - MovieClip(parent).generatorArray[i].y ) , 2 ) ) + 1;

if( this.x <= MovieClip(parent).generatorArray[i].x )
{
xPull = acceleration * ( 1 / Math.pow( xDistance , 2 ) );
}
else
{
xPull = acceleration * ( -1 / Math.pow( xDistance , 2 ) );
}

if( this.y <= MovieClip(parent).generatorArray[i].y )
{
yPull = acceleration * ( 1 / Math.pow( yDistance , 2 ) );
}
else
{
yPull = acceleration * ( -1 / Math.pow( yDistance , 2 ) );
}

Vx += xPull * Math.pow( 10 , MovieClip(parent).generatorArray[i].po wer ) * (xDistance / yDistance) * 10e4;
Vy += yPull * Math.pow( 10 , MovieClip(parent).generatorArray[i].po wer ) * (yDistance / xDistance) * 10e4;
}

//separate code:

this.x += Vx;
this.y += Vy;

It's a little broken up but if you know what you're doing you should probably get this. If you think you're partway there, don't hesitate to message me and ask about any of this, or more. If you're completely lost, I'll explain this in a nutshell:

These pieces of code calculate the pull on a ball (all balls at once, actually...I won't explain that here, but it has to do with the fact that each ball is an instance of a class that has this code in it) for each generator in the array generatorArray. An array is basically a list, and generatorArray is a list of every generator that has been created.

This code has code before it that looks at every item in generatorArray. If the generator it is currently looking at has the on button pressed, it will then calculate the pull on the ball using the math you see in the code, and then add it to the velocity. Eventually, the velocity will get added to the position, making the ball move.


Comments

This is great! You should really make this topic in the NG flash forum!